autorelease pool

  • I was wondering how this works and what happens if the retain count reaches
    0 but the object wasn't part of a autorelease pool ?
    *Example:*

    NS* String Line = [NSString alloc] init];

    .. random code ..

    [Line release];

    Now, what happens if the retain count is 1 and if its 0 for NSString Line ?
    ?

    How should I use an autorelease pool to actually deallocate the memory used
    by Line ?
  • In the code you've just posted, NSAutoreleasePool is not used in any
    way for your string.

    The sequence of alloc, init, and release does not use the autorelease
    pool since no -autorelease message is ever sent. Instead it does
    exactly as written:

    1. Creates a new string object with a retain count of 1.
    2. Releases the string so it has a retain count of 0.
    3. Deallocates the memory used by the string since it has a retain
    count of 1.

    Mike.

    P.S. Normally in Cocoa you use lowercase to start a variable name. i.e.

    NSString *line

    On 23 Oct 2007, at 18:50, Erfan Aleemullah wrote:

    > I was wondering how this works and what happens if the retain count
    > reaches
    > 0 but the object wasn't part of a autorelease pool ?
    > *Example:*
    >
    > NS* String Line = [NSString alloc] init];
    >
    > .. random code ..
    >
    > [Line release];
    >
    > Now, what happens if the retain count is 1 and if its 0 for NSString
    > Line ?
    > ?
    >
    > How should I use an autorelease pool to actually deallocate the
    > memory used
    > by Line ?
  • Yes, I am trying to figure out whether or not I need to use the release pool
    and was throwing the above code out as an example - what I wanted to know is
    **do I need the autorelease pool if the retain count is 1 after the command
    [myObject release]; *
    I need to deallocate memory in my program and release alone isn't taking the
    retain count to 0
    *
    *
    On 10/23/07, Mike Abdullah <cocoadev...> wrote:
    >
    > In the code you've just posted, NSAutoreleasePool is not used in any
    > way for your string.
    >
    > The sequence of alloc, init, and release does not use the autorelease
    > pool since no -autorelease message is ever sent. Instead it does
    > exactly as written:
    >
    > 1. Creates a new string object with a retain count of 1.
    > 2. Releases the string so it has a retain count of 0.
    > 3. Deallocates the memory used by the string since it has a retain
    > count of 1.
    >
    > Mike.
    >
    > P.S. Normally in Cocoa you use lowercase to start a variable name. i.e.
    >
    > NSString *line
    >
    > On 23 Oct 2007, at 18:50, Erfan Aleemullah wrote:
    >
    >> I was wondering how this works and what happens if the retain count
    >> reaches
    >> 0 but the object wasn't part of a autorelease pool ?
    >> *Example:*
    >>
    >> NS* String Line = [NSString alloc] init];
    >>
    >> .. random code ..
    >>
    >> [Line release];
    >>
    >> Now, what happens if the retain count is 1 and if its 0 for NSString
    >> Line ?
    >> ?
    >>
    >> How should I use an autorelease pool to actually deallocate the
    >> memory used
    >> by Line ?
    >
    >
  • On 10/23/07, Erfan Aleemullah <erfan.aleem...> wrote:
    > Yes, I am trying to figure out whether or not I need to use the release pool
    > and was throwing the above code out as an example - what I wanted to know is
    > **do I need the autorelease pool if the retain count is 1 after the command
    > [myObject release]; *

    You can't know what the retain count is after the release; after the
    release (assuming you had the only reference), you've got a dead
    object, inspecting it's contents is a no-no.

    --
    Clark S. Cox III
    <clarkcox3...>
  • Dear Erfan,

    From you questions, it is clear that you don't understand how Cocoa
    memory management works. I suggest you read the Memory Management Guide:

    http://developer.apple.com/documentation/Cocoa/Conceptual/MemoryMgmt/
    index.html

    and the Cocoa Fundamentals Guide

    http://developer.apple.com/documentation/Cocoa/Conceptual/
    CocoaFundamentals/index.html

    Specifically, the chapter on cocoa objects and their life cycle.

    On Oct 23, 2007, at 2:34 PM, Erfan Aleemullah wrote:

    > Yes, I am trying to figure out whether or not I need to use the
    > release pool
    > and was throwing the above code out as an example - what I wanted
    > to know is
    > **do I need the autorelease pool if the retain count is 1 after the
    > command
    > [myObject release]; *

    You need an autorelease pool if you're using Cocoa. If you have a
    command line application you have to create one yourself, otherwise
    the system takes care of it for you.

    > I need to deallocate memory in my program and release alone isn't
    > taking the
    > retain count to 0

    Freeing up memory is done with release or autorelease, period.
    There's nothing more you should do. However, as far as I know,
    checking the retain count is not a reliable method to make sure your
    memory is freed. Check the list archives for this, I remember this
    has come up a couple of times already.

    <snip>

    Good luck,
    Hank
  • Erfan Aleemullah wrote:

    > Yes, I am trying to figure out whether or not I need to use the
    > release pool
    > and was throwing the above code out as an example - what I wanted
    > to know is
    > **do I need the autorelease pool if the retain count is 1 after the
    > command
    > [myObject release]; *
    > I need to deallocate memory in my program and release alone isn't
    > taking the
    > retain count to 0

    Ignore the retain count. It's an implementation detail that's not
    reliably useful for what you're trying to get out of it.

    Adding an autorelease pool isn't going to do anything magic, and the
    only way you should ever _need_ to add one (although they can
    optionally be introduced to improve resource utilization) is if you
    see a logged message that an object was autoreleased without a pool
    in place.
  • Hi Erfan,

    First the answer: you string will be deallocates, its
    memory released.

    Second, you should review following documents to learn
    why this is so:
    1.
    http://developer.apple.com/documentation/Cocoa/Conceptual/MemoryMgmt/Tasks/
    MemoryManagementRules.html

    2.
    http://www.stepwise.com/Articles/Technical/2001-03-11.01.html

    Regards,
    Gorazd

          Ask a question on any topic and get answers from real people. Go to Yahoo! Answers and share what you know at http://ca.answers.yahoo.com
  • thanks guys - i'm also reading the book by Kochan to get more insight on the
    issue
    but man - the docs on ADC put me to sleep =P

    On 10/23/07, Gorazd Krosl <gorazd_1957...> wrote:
    >
    > Hi Erfan,
    >
    > First the answer: you string will be deallocates, its
    > memory released.
    >
    > Second, you should review following documents to learn
    > why this is so:
    > 1.
    >
    > http://developer.apple.com/documentation/Cocoa/Conceptual/MemoryMgmt/Tasks/
    MemoryManagementRules.html

    > 2.
    > http://www.stepwise.com/Articles/Technical/2001-03-11.01.html
    >
    > Regards,
    > Gorazd
    >
    >
    > Ask a question on any topic and get answers from real people. Go to
    > Yahoo! Answers and share what you know at http://ca.answers.yahoo.com
    >
previous month october 2007 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 31        
Go to today