开发者

Releasing while notifying

开发者 https://www.devze.com 2023-01-06 06:41 出处:网络
I have an object id currentObject; which I want to pass through notification. The problem is I don\'t know how to release it correctly and the memory management documentation is driving me crazy.

I have an object

id currentObject;

which I want to pass through notification. The problem is I don't know how to release it correctly and the memory management documentation is driving me crazy.

I am doing it like this right now:

[[NSNotificationCenter defaultCenter] postNotificationName:@"MessageReceived" object:nil userInfo:[NSDictionary dictionaryWithObjectsAndKeys:[currentObject copy], @"key", nil]];
[currentObject release];

Should it rather be:

[[NSNotificationCenter defaultCenter] postNotificationName:@"MessageReceived" object:nil userInfo:[NSDictionary dictionaryWithObjectsAndKeys:[currentObject autorelease], @"key", nil]];

?开发者_开发问答

(it is for the iPhone, iOS4.0)

Thanks in advance!


Either way is OK. Although there is a marginal difference between those two approaches, it shouldn't make any practical difference in this case. As long as you're releasing your own ownership in one way or the other, you'll be fine. The dictionary and notification centre will deal with their own ownership issues in their own time.

EDIT: Oops, missed something. What I said at first still applies in general, BUT in the first example you are calling copy on currentObject. This creates a new bit of ownership -- which you immediately forget, and therefore produce a memory leak.

Since we don't see where you create currentObject, it's possible both versions over-release that too. But assuming you alloc it, you're sufficiently releasing that. But if you insist on calling copy -- probably unnecessary, though you'd know more about what currentObject is and what might later happen to it -- you need to release the new copy too, eg by wrapping the copy call in autorelease, like so:

[[NSNotificationCenter defaultCenter] postNotificationName:@"MessageReceived" object:nil userInfo:[NSDictionary dictionaryWithObjectsAndKeys:[[currentObject copy] autorelease], @"key", nil]];
[currentObject release];

If this is unclear, have another good read of the object ownership docs.


You can assume that objects you pass your objects to sort out their own ownership issues with the passed in objects unless the documentation says otherwise. For an example, scroll to the bottom of the overview of the NSNotificationCenter docs and there is a call out about the fact that it does not retain observers.

Your first example leaks because you pass a copy of your object to the dictionary made by -copy. So you own both the object and the copy, but you never release the copy (the dictionary also retains the copy). If you want the dictionary to contain a copy of the object rather than the object itself, do this:

[[NSNotificationCenter defaultCenter] 
    postNotificationName:@"MessageReceived" 
                  object:nil 
                userInfo:[NSDictionary dictionaryWithObjectsAndKeys:[[currentObject copy] autorelease], @"key", nil]];

The second example is fine provided that you own currentObject i.e. you obtained it with new, alloc or a method containing copy or you have previously retained it.

0

精彩评论

暂无评论...
验证码 换一张
取 消