• Open Menu Close Menu
  • Apple
  • Shopping Bag
  • Apple
  • Mac
  • iPad
  • iPhone
  • Watch
  • TV
  • Music
  • Support
  • Search apple.com
  • Shopping Bag

Lists

Open Menu Close Menu
  • Terms and Conditions
  • Lists hosted on this site
  • Email the Postmaster
  • Tips for posting to public mailing lists
Re: [for memory geek] NSNotificationCenter possible memory leak?
[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]

Re: [for memory geek] NSNotificationCenter possible memory leak?


  • Subject: Re: [for memory geek] NSNotificationCenter possible memory leak?
  • From: Will Mason <email@hidden>
  • Date: Thu, 14 Jul 2005 08:21:15 -0700 (PDT)

Hi, Giovanni,
I think you may be making assumptions that are incorrect. As the last
person noted, the NSNotificationCenter may be allocating a cache of
memory without caching the objects that you put into it. What if it's
just allocating memory as an optimization so that it will have an
already-allocated cache available for later use? If I were writing
NSNotificationCenter, I would not perform a fresh memory allocation
every time someone sent me an object if I could avoid it. Also, I would
not put a timer on my cache causing it to be reaped after a few
seconds.

Cheers,
Will

--- Giovanni Donelli <email@hidden> wrote:

> I see what you are saying however the memory seems not to be released
>
> by the NSNotificationCenter
>   even after few seconds.
>
> To check the memory leak I'm using an application developed
> internally in my company (which we have been using reliably for a
> while), but with the command top it is noticeable the increment of
> memory allocated too.
>
> any other idea on how to purge NSNotificationCenter?
>
> I really don't see the reason why there should be cached copies of
> notification when I explicitly asked to have them removed.
>
> thanks for your reply!
>
> Giovanni
> Donelleschi
>
> On 13 Jul 2005, at 7:15 PM, Fritz Anderson wrote:
>
> > 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
>
 _______________________________________________
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

References: 
 >Re: [for memory geek] NSNotificationCenter possible memory leak? (From: Giovanni Donelli <email@hidden>)

  • Prev by Date: NSBrowser focus question
  • Next by Date: Re: How to make my own 'dock' like menu from App Icon?
  • Previous by thread: Re: [for memory geek] NSNotificationCenter possible memory leak?
  • Next by thread: process events in tight loop?
  • Index(es):
    • Date
    • Thread