Re: NSNotificationCenter possible memory leak? [for memory geek]
Re: NSNotificationCenter possible memory leak? [for memory geek]
- Subject: Re: NSNotificationCenter possible memory leak? [for memory geek]
- From: Fritz Anderson <email@hidden>
- Date: Wed, 13 Jul 2005 21:15:56 -0500
What leads you to believe that this is actually a leak, and not just
a retention of 4096 bytes for every new notification type you create?
Do you have a report from a leak-detecting utility that says memory
remained allocated after it was no longer referenced, or are you just
noticing that your curious function consumes memory?
I'd find it plausible that when it learns of a new kind of
notification (you create 20 new notifications), NSNotificationCenter
allocates facilities for that notification, and caches those
facilities even when the last observer for that notification is removed.
It is not reasonable to assume that a notification type "disappears,"
never to be heard from again, when nobody is registered for it.
NSNotificationCenter is probably not designed around that assumption.
Your code, which makes add/removeObserver look like a reference tally
like retain/release, is misleading on this point.
-- F
On 13 Jul 2005, at 4:13 PM, Giovanni Donelli wrote:
Hi,
I can't figure out how I can get rid of the following memory
leak which is about 80KB, on my PowerBook G4.
There is something connected with the fact that 'name' is randomly
generated... however I tried for few hours to understand what was
wrong.. and here I am now to you :-)
here is the function that leads to the memory leak of about 80KB
void memoryLeak()
{
int i;
NSNotificationCenter* nc = [NSNotificationCenter
defaultCenter];
for (i=0 ; i < 20; i++)
{
NSObject *obj = [NSObject new];
NSString* name = [[NSString alloc] initWithFormat:@"%
d", random()];
[nc addObserver:obj
selector:@selector(doenstMatter:)
name:name
object:nil];
[nc removeObserver:obj
name:name
object:nil];
[name release];
[obj release];
}
}
Thank you!
Giovanni Donelli
_______________________________________________
Do not post admin requests to the list. They will be ignored.
Cocoa-dev mailing list (email@hidden)
Help/Unsubscribe/Update your Subscription:
40manoverboard.org
This email sent to email@hidden
_______________________________________________
Do not post admin requests to the list. They will be ignored.
Cocoa-dev mailing list (email@hidden)
Help/Unsubscribe/Update your Subscription:
This email sent to email@hidden