简体   繁体   English

iOS-重置NSMutable阵列会导致崩溃

[英]iOS - resetting NSMutable Array causes crash

I have an NSMutableArray I'm trying to reload after an async call. 我有一个NSMutableArray,我正在尝试在异步调用后重新加载。 The first time it loads like this: 第一次加载是这样的:

self.sessionProcList = [NSMutableArray arrayWithArray:[result records]];

After the user does some interaction, the same line will be reached to reload the NSMutableArray. 用户进行一些交互后,将到达同一行以重新加载NSMutableArray。 This causes the crash 这导致崩溃

Header file has: 头文件具有:

@interface...
NSMutableArray *sessionProcList;
... }

@property (nonatomic, retain) NSMutableArray *sessionProcList;

Say you do: 说您:

NSMutableArray *a = [NSMutableArray arrayWithObject: [[NSObject alloc] init]];
NSObject *o = [a objectAtIndex: 0];
[a removeAllObjects];
[o description]; // *BOOM*

The above will [generally -- sometimes not but only by coincidence] crash because o has been deallocated by the time the description method is invoked. 上面的内容将[通常-有时不是,但不是偶然,而是偶然地]崩溃,因为在调用description方法时o已被释放。

If you have a reference to an object in an array, but have not retained said reference, then said object may be deallocated out from under you when you empty the array. 如果您对数组中的对象有一个引用,但是没有保留该引用,那么在清空数组时,该对象可能会从您的下方释放。

(And nonatomic vs. atomic is irrelevant.) (并且nonatomic与原子无关。)

If I had to guess, I would say that elements in that array are being referenced from somewhere else. 如果我不得不猜测,我会说该数组中的元素是从其他地方引用的。 Resetting the array causes the items using the references to crash. 重置数组会导致使用引用的项目崩溃。

I would check your application for other variables, properties, or UI elements using those variables that have not been release before resetting it. 我将在重置应用程序之前使用尚未释放的那些变量来检查您的应用程序是否存在其他变量,属性或UI元素。

Because arrayWithArray is a convenience method it gets initialised with an autorelease flag. 由于arrayWithArray是一种便捷方法,因此会使用自动释放标志进行初始化。

You haven't mentioned what the crash / error message is but I am guessing your NSMutableArray is getting released before your second iteraction with it starts. 您没有提到崩溃/错误消息是什么,但是我想您的NSMutableArray将在第二次迭代开始之前被释放。

Try and retain the array for however long you need it with: 尝试将阵列保留很长时间,无论您需要以下哪种方法:

self.sessionProcList = [NSMutableArray arrayWithArray:[result records]];
[sessionProcList retain];

And then release it when you're done with it: 完成后将其释放:

[sessionProcList release];

I hope it helps. 希望对您有所帮助。 Rog 罗格

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM