简体   繁体   English

分配或保留在cocos2d和目标C中

[英]Assign or retain in cocos2d and objective C

Here's my current situation: I have a NSMutableArray named dictKeyArray which I assign a property with @property(nonatomic,retain)NSMutableArray *dictKeyArray 这是我目前的情况:我有一个名为dictKeyArray的NSMutableArray我用@property(nonatomic,retain)NSMutableArray *dictKeyArray分配了一个属性@property(nonatomic,retain)NSMutableArray *dictKeyArray
I synthesize my mutable array in the implementation file. 我在实现文件中合成了我的可变数组。
Later, I have a dictionary name storeDict. 后来,我有一个字典名称storeDict。 I assign all the keys of the dictionary to the dictKeyArray like so: 我将字典的所有键分配给dictKeyArray,如下所示:
dictKeyArray = [[storeDict allKeys] mutableCopy];
Now I use this dictionary later in my implementation file. 现在我稍后在我的实现文件中使用这个字典。 However, when it comes to releasing it, I release it once in my dealloc method. 但是,当它发布它时,我在dealloc方法中释放它一次。 When checking with instruments, a leak shows up! 使用仪器检查时,会出现泄漏! Why is dictKeyArray leaking? 为什么dictKeyArray漏水了? Should I be using assign instead of retain ? 我应该使用assign而不是retain吗? I'm still not clear on what the difference is exactly... thank you! 我还不清楚究竟有什么不同......谢谢!

You have to send it an 你必须发送一个

[[[storeDict allKeys] mutableCopy] autorelease];

Just to make this clear: mutableCopy does the same as alloc meaning you are claiming ownership of the object in question. 只是为了明确这一点: mutableCopyalloc相同,这意味着您声称对相关对象拥有所有权。 You have to decrease the retainCount by one. 您必须将retainCount减少一个。

By the way: You should use the accessor you wrote for it. 顺便说一下:你应该使用你为它编写的访问器。 You are just assigning it to your iVar at the moment. 您现在只是将它分配给您的iVar。 If you want to make your accessors work, you will have to use something like 如果你想让你的访问者工作,你将不得不使用类似的东西

object.dictKeyArray = ...;

in general. 一般来说。 Or here (as mentioned by dreamlax) 或者在这里(如dreamlax所述)

self.dictKeyArray = ...;

because you are referring to an object of this specific class the code is in. 因为您指的是代码所在的特定类的对象。

Only this way you are ensuring your object is properly retained by your accessor. 只有这样,您才能确保访问者正确保留您的对象。 Otherwise writing the accessor code doesn't make sense at all because it never gets called. 否则编写访问器代码根本没有意义,因为它永远不会被调用。

Please note: As Josh said in the comments, your code should be valid (at least from my point of view). 请注意:正如Josh在评论中所说,您的代码应该是有效的(至少从我的角度来看)。 What I suggested is a solution that is not as error-prone as yours because you adhere to the rules (could save you from headache in the near future). 我建议的解决方案并不像您那样容易出错,因为您遵守规则(可以在不久的将来免除您的头痛)。

You should be using self.dictKeyArray = ... . 你应该使用self.dictKeyArray = ... Without the self. 没有self. you are accessing the instance variable directly, bypassing any memory management benefits of properties, but, remember that you own the result of mutableCopy , and assigning to a property that also takes ownership will result in double-ownership, so use: 您正在直接访问实例变量,绕过属性的任何内存管理优势,但是,请记住您拥有mutableCopy的结果,并且分配给也拥有所有权的属性将导致双重所有权,因此使用:

self.dictKeyArray = [[[storeDict allKeys] mutableCopy] autorelease];

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

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