Delayed Autorelease

  • Is it common in either Cocoa or UIKit to have an autorelease run
    happen only when the user does something?

    I'm in a situation where I believe the autorelease run is happening
    only when I push a bush or otherwise fiddled with a UI item; might
    that be the case, or am I confused?

    -Patrick

    P.S.
    Is there perhaps a way to have it logged to the console whenever an
    autorelease run happens?
  • On Apr 8, 2010, at 10:36 AM, Patrick M. Rutkowski wrote:

    > Is it common in either Cocoa or UIKit to have an autorelease run
    > happen only when the user does something?

    > I'm in a situation where I believe the autorelease run is happening
    > only when I push a bush or otherwise fiddled with a UI item; might
    > that be the case, or am I confused?

    I think you may be confused here. There's no such thing as an 'autorelease run'. Autoreleased objects are collected in autorelease pools. The main autorelease pool in an application with a UI is drained every so often (every time the application goes through its main event loop).

    You shouldn't have to worry about when autoreleased objects get released: the only thing you need to know is whether or not you're responsible for the release of an object. This is explained at length in the Memory Management Programming Guide:

    http://developer.apple.com/mac/library/documentation/cocoa/conceptual/Memor
    yMgmt/MemoryMgmt.html


    Also, check the documentation for NSAutoreleasePool.

    Hope this helps,
    Hank

    >
    > -Patrick
    >
    > P.S.
    > Is there perhaps a way to have it logged to the console whenever an
    > autorelease run happens?

    I'm assuming you're referring to the draining of an autorelease pool again. Since the main autorelease pool is drained at the end of the event loop, I guess you could attach an observer to the main run loop (search the docs for runloop observer), but there may be better ways. Why would you want to know this?


    >
  • It turns out that my memory error is coming form somewhere else. If
    you don't mind changing the topic of the thread for a moment:

    I have a Foo object which has an NSMutableArray of Bar objects. The
    Bar objects each have their own NSMutableArray of Baz objects.

    The weird thing is that the Foo and it's array of Bar's are all living
    well and good, but the Bar's are all getting dealloc'd, even though
    their containing parent are still alive.

    Still digging into it; not quite sure that you guys would even have
    any advice to offer here, I think I'm on my own :-/

    -Patrick

    On Thu, Apr 8, 2010 at 10:36 AM, Patrick M. Rutkowski
    <rutski89...> wrote:
    > Is it common in either Cocoa or UIKit to have an autorelease run
    > happen only when the user does something?
    >
    > I'm in a situation where I believe the autorelease run is happening
    > only when I push a bush or otherwise fiddled with a UI item; might
    > that be the case, or am I confused?
    >
    > -Patrick
    >
    > P.S.
    > Is there perhaps a way to have it logged to the console whenever an
    > autorelease run happens?
    >
  • On Apr 8, 2010, at 10:36 AM, Patrick M. Rutkowski wrote:

    > Is it common in either Cocoa or UIKit to have an autorelease run
    > happen only when the user does something?
    >
    > I'm in a situation where I believe the autorelease run is happening
    > only when I push a bush or otherwise fiddled with a UI item; might
    > that be the case, or am I confused?

    I don't know about pushing bushes, but in general the "autorelease run" is at the end of an event loop, when your code has finished responding to an event, and the system is about ready to look for another one.

    > Is there perhaps a way to have it logged to the console whenever an
    > autorelease run happens?

    --- A log to the console on every event would swamp you in data, I think.

    ----------------------------------------------------------------
    Steve Bird
    Culverson Software - Elegant software that is a pleasure to use.
    www.Culverson.com                    (toll free) 1-877-676-8175
  • On Apr 8, 2010, at 11:03 AM, Patrick M. Rutkowski wrote:

    > It turns out that my memory error is coming form somewhere else. If
    > you don't mind changing the topic of the thread for a moment:
    >
    > I have a Foo object which has an NSMutableArray of Bar objects. The
    > Bar objects each have their own NSMutableArray of Baz objects.
    >
    > The weird thing is that the Foo and it's array of Bar's are all living
    > well and good, but the Bar's are all getting dealloc'd,

    How are Bars (no apostrophe) "living well" if they are deallocated?

    ----------------------------------------------------------------
    Steve Bird
    Culverson Software - Elegant software that is a pleasure to use.
    www.Culverson.com                    (toll free) 1-877-676-8175
  • On 8 apr 2010, at 08.03, Patrick M. Rutkowski wrote:

    > I have a Foo object which has an NSMutableArray of Bar objects. The
    > Bar objects each have their own NSMutableArray of Baz objects.
    >
    > The weird thing is that the Foo and it's array of Bar's are all living
    > well and good, but the Bar's are all getting dealloc'd, even though
    > their containing parent are still alive.
    >
    > Still digging into it; not quite sure that you guys would even have
    > any advice to offer here, I think I'm on my own :-/

    If you can reproduce the problem you can use the ObjectAlloc instrumentation in Instruments to track down the issue. It allows you to track all memory management events (alloc, retain, release, autorelease, etc.) in your application, so that you can figure out where your objects are being mismanaged.

    j o a r
  • This is a really common problem for developers coming from other platforms.  In the Windows world the only thing really analogous to retain/release is the COM addref concepts, and even those lack the equivalent of autorelease.

    The short version, in my experience, is when in doubt, manage the memory with retain and release pairs.  Garbage collection is good, but can have unexpected behaviors that become tricky to debug.  I know there are people that idol disagree, but between apple's leak finding tools, llvm's analysis tools and shark, it is often easier to hunt down a leak using old school retain/release methods than new school garbage collection.

    Sent from my iPad

    On Apr 8, 2010, at 11:09 AM, Steve Bird <SBird...> wrote:

    >
    > On Apr 8, 2010, at 10:36 AM, Patrick M. Rutkowski wrote:
    >
    >> Is it common in either Cocoa or UIKit to have an autorelease run
    >> happen only when the user does something?
    >>
    >> I'm in a situation where I believe the autorelease run is happening
    >> only when I push a bush or otherwise fiddled with a UI item; might
    >> that be the case, or am I confused?
    >
    > I don't know about pushing bushes, but in general the "autorelease run" is at the end of an event loop, when your code has finished responding to an event, and the system is about ready to look for another one.
    >
    >> Is there perhaps a way to have it logged to the console whenever an
    >> autorelease run happens?
    >
    > --- A log to the console on every event would swamp you in data, I think.
    >
    > ----------------------------------------------------------------
    > Steve Bird
    > Culverson Software - Elegant software that is a pleasure to use.
    > www.Culverson.com                    (toll free) 1-877-676-8175
    >
  • On Apr 8, 2010, at 11:03 AM, Patrick M. Rutkowski wrote:

    > It turns out that my memory error is coming form somewhere else. If
    > you don't mind changing the topic of the thread for a moment:
    >
    > I have a Foo object which has an NSMutableArray of Bar objects. The
    > Bar objects each have their own NSMutableArray of Baz objects.
    >
    > The weird thing is that the Foo and it's array of Bar's are all living
    > well and good, but the Bar's are all getting dealloc'd, even though
    > their containing parent are still alive.

    I assume that 'living well and good' means that they haven't been released? Nothing weird about Baz objects (you say Bar but I think you mean Baz) getting dealloc'd when their parents are alive: Bar retains its objects when you add them, but if you overrelease them somewhere else, being contained in an array doesn't prevent them from being deallocated.

    > Still digging into it; not quite sure that you guys would even have
    > any advice to offer here, I think I'm on my own :-/

    Posting code would be helpful if you'd like advice. Meanwhile you could search Google for NSZombieEnabled, or just run your application in Instruments with the Zombies template to find out what's wrong. If the error is obvious, you might even be able to find it with the static analyzer.

    Hank
  • I meant that I believe that the array itself was still alive, but the
    Bar objects inside of it were getting dealloc'd

    >
    > How are Bars (no apostrophe) "living well" if they are deallocated?
    >
    >
    > ----------------------------------------------------------------
    > Steve Bird
    > Culverson Software - Elegant software that is a pleasure to use.
    > www.Culverson.com                     (toll free) 1-877-676-8175
    >
    >
    >
  • Sorry for the useless thread guys, I worked it all out :-)

    It was due to silly mistakes like forgetting to initialize the array
    in -init, and then having [self.array addObject: bar] be a no-op due
    to self.array being nil.

    Another mistake was self.array being initialized as
    self.array = [[NSMutableArray alloc] init]

    when it should have been
    self.array = [NSMutableArray array]

    I'm still somewhat new to this :-)

    I totally get what's going on, I'm just not good at spotting the usual
    mistakes yet; and I'm a bit sleepy at the moment.

    Yawn,
    -Patrick

    On Thu, Apr 8, 2010 at 11:03 AM, Patrick M. Rutkowski
    <rutski89...> wrote:
    > It turns out that my memory error is coming form somewhere else. If
    > you don't mind changing the topic of the thread for a moment:
    >
    > I have a Foo object which has an NSMutableArray of Bar objects. The
    > Bar objects each have their own NSMutableArray of Baz objects.
    >
    > The weird thing is that the Foo and it's array of Bar's are all living
    > well and good, but the Bar's are all getting dealloc'd, even though
    > their containing parent are still alive.
    >
    > Still digging into it; not quite sure that you guys would even have
    > any advice to offer here, I think I'm on my own :-/
    >
    > -Patrick
    >
    > On Thu, Apr 8, 2010 at 10:36 AM, Patrick M. Rutkowski
    > <rutski89...> wrote:
    >> Is it common in either Cocoa or UIKit to have an autorelease run
    >> happen only when the user does something?
    >>
    >> I'm in a situation where I believe the autorelease run is happening
    >> only when I push a bush or otherwise fiddled with a UI item; might
    >> that be the case, or am I confused?
    >>
    >> -Patrick
    >>
    >> P.S.
    >> Is there perhaps a way to have it logged to the console whenever an
    >> autorelease run happens?
    >>
    >
  • On 8 apr 2010, at 08.37, Patrick M. Rutkowski wrote:

    > Another mistake was self.array being initialized as
    > self.array = [[NSMutableArray alloc] init]
    >
    > when it should have been
    > self.array = [NSMutableArray array]
    >
    > I'm still somewhat new to this :-)
    >
    > I totally get what's going on, I'm just not good at spotting the usual
    > mistakes yet; and I'm a bit sleepy at the moment.

    If you're using Xcode 3.2 (requires Mac OS X 10.6), you should be using the static analyzer. It is good at finding problems like these.

    j o a r
  • Ah, a very good point, I'll turn that on right away; I can't believe I
    forgot that. Thanks :-)

    On Thu, Apr 8, 2010 at 11:40 AM, Joar Wingfors <joar...> wrote:
    >
    > On 8 apr 2010, at 08.37, Patrick M. Rutkowski wrote:
    >
    >> Another mistake was self.array being initialized as
    >> self.array = [[NSMutableArray alloc] init]
    >>
    >> when it should have been
    >> self.array = [NSMutableArray array]
    >>
    >> I'm still somewhat new to this :-)
    >>
    >> I totally get what's going on, I'm just not good at spotting the usual
    >> mistakes yet; and I'm a bit sleepy at the moment.
    >
    >
    > If you're using Xcode 3.2 (requires Mac OS X 10.6), you should be using the static analyzer. It is good at finding problems like these.
    >
    > j o a r
    >
    >
    >
previous month april 2010 next month
MTWTFSS
      1 2 3 4
5 6 7 8 9 10 11
12 13 14 15 16 17 18
19 20 21 22 23 24 25
26 27 28 29 30    
Go to today