NSMutableArray 上的无效摘要
我需要一些关于如何解决这个问题的专家建议。我正在为 iPad 的新应用程序进行一些粗略测试。
我通过加载在另一个应用程序中创建的 plist 文件在视图控制器的 viewDidLoad 中创建一个 NSMutableArray (ballPath)(它在我的 .h 文件中声明)。
ballPath = [[NSMutableArray alloc] initWithCapacity:1000];
NSString *path=[[NSBundle mainBundle] pathForResource:@"level_1" ofType:@"plist"];
ballPath = [NSKeyedUnarchiver unarchiveObjectWithFile:path];
该数组现在包含许多 CGPoint(由其他应用程序存储为 NSValues 并存档)。
然后我绘制路径(仍在我的 viewDidLoad 中),效果很好,所以路径应该可以正常工作。
当我稍后想要读取数组作为对加速度变化的响应时,我得到了 EXC_BAD_ACCESS。当我调试并查看我的数组时,它看起来像这样:
我测试并用此替换加载的数组(也在 viewDidLoad 中):
ballPath = [[NSMutableArray alloc] initWithObjects:
[NSValue valueWithCGPoint:CGPointMake(100.0, 100.0)],
[NSValue valueWithCGPoint:CGPointMake(100.0, 200.0)],
[NSValue valueWithCGPoint:CGPointMake(100.0, 300.0)],
[NSValue valueWithCGPoint:CGPointMake(100.0, 400.0)],
[NSValue valueWithCGPoint:CGPointMake(125.0, 450.0)],
[NSValue valueWithCGPoint:CGPointMake(150.0, 500.0)],
[NSValue valueWithCGPoint:CGPointMake(300.0, 600.0)],
[NSValue valueWithCGPoint:CGPointMake(350.0, 550.0)],nil];
然后就可以正常工作了!
我在这里缺少什么???
我使用的是 Xcode 4.0.2,我的目标是 iOS 4.3。
I would need some expert advice on how to solve this problem. I am doing some crude testing for a new app for the iPad.
I create a NSMutableArray (ballPath) in the viewDidLoad of my view controller (it is declared in my .h file) by loading a plist file created in another app.
ballPath = [[NSMutableArray alloc] initWithCapacity:1000];
NSString *path=[[NSBundle mainBundle] pathForResource:@"level_1" ofType:@"plist"];
ballPath = [NSKeyedUnarchiver unarchiveObjectWithFile:path];
This array now contains a number of CGPoints (stored by the other app as NSValues and the archived).
I then draw the path (still in my viewDidLoad), which works fine, so the path should work fine.
When I later on want to read the array as a response to changes in acceleration, I get an EXC_BAD_ACCESS. When I debug and look at my array it looks like this:
I test and replace the loaded array with this (also in viewDidLoad):
ballPath = [[NSMutableArray alloc] initWithObjects:
[NSValue valueWithCGPoint:CGPointMake(100.0, 100.0)],
[NSValue valueWithCGPoint:CGPointMake(100.0, 200.0)],
[NSValue valueWithCGPoint:CGPointMake(100.0, 300.0)],
[NSValue valueWithCGPoint:CGPointMake(100.0, 400.0)],
[NSValue valueWithCGPoint:CGPointMake(125.0, 450.0)],
[NSValue valueWithCGPoint:CGPointMake(150.0, 500.0)],
[NSValue valueWithCGPoint:CGPointMake(300.0, 600.0)],
[NSValue valueWithCGPoint:CGPointMake(350.0, 550.0)],nil];
Then it works just fine!
What am I missing here????
I'm on Xcode 4.0.2 and my target is iOS 4.3.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
该数组在此时被释放,因此该数组之前所在的位置有一些随机内存。
unarchiveObjectWithFile:
返回一个自动释放的对象,如果你想保留数组,你需要保留它(或使其成为保留属性)。前面的两行 alloc-init 是完全多余的(并且可能会泄漏内存),因为您永远不会对在那里创建的数组执行任何操作,它会被您从包中加载的数组替换。The array is deallocated at that point, so that there's some random memory where the array previously was.
unarchiveObjectWithFile:
returns an autoreleased object, if you want to keep the array around, you need to retain it (or make it a retained property). The alloc-init two lines earlier is completely superfluous (and probably leaks memory), because you're never doing anything with the array you create there, it is replaced by the array you load from the bundle.