NSMutableArray 上的无效摘要

发布于 2024-11-07 15:07:26 字数 1510 浏览 4 评论 0原文

我需要一些关于如何解决这个问题的专家建议。我正在为 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).

enter image description here

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:

enter image description here

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!

enter image description here

What am I missing here????

I'm on Xcode 4.0.2 and my target is iOS 4.3.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

烟花肆意 2024-11-14 15:07:26

该数组在此时被释放,因此该数组之前所在的位置有一些随机内存。

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.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文