Wednesday, April 13, 2011

iphone memory management problem

how to check an object have been release from the memory?

i know a object have to be release manually when we use alloc|copy|retain to create that object. if use the instance class method(NSString stringwithformat:), the object will be release automactically by NSAutoRealeasePool,however, sometime there have some object used to release at the (void)dealloc function there,can anybody tell me

  1. what is the different with [object release] instantly after alloc function,[object release] at (void)dealloc,and NSAutoReleasePool to release the object?

  2. when is the (void)dealloc function being called and when is the NSAutoReleasePool release the memory, i know it is every loop cycle, how can i check it from code?

  3. when i declared a variable at hearder and set the property as retain, i still can use the alloc function to that object but not cause to memory leak, but from this post seem like once declared a retain object at header, that is no longer can alloc the object because both will retain the object,can anybody tell me why i still can code like this without causing any memory leak?

From stackoverflow
    1. [object release] releases your object immediately. After sending a release message you should not use the object again - unless you absolutely know you still have a retain on that object. If yours was the last retain the memory could be freed during the call to release. Autorelease frees an object 'sometime later' the system does not guarantee anything about the meaning of 'later' other than that it will be after the scope of the current message.

    2. See above, there is no real qay to guarantee when dealloc is called following autorelease from the point of view of your code. You should just assume it is sometime after the return of the method where you send the autorelease message.

    3. You simply need to balance retain and release. If you have one to many (as is likely i nthe situation you describe) that is a leak. Conversely if you have unbalance the other way, you will generate a more destructive error when you access freed memory.

    The correct sequence for a retained property is:

    1. alloc init // retain == 1

    2. set the property // retain == 2 due to setProperty calling retain.

    3. release the object // retain == 1

    Which leaves a retain count of one, no memory leak.

    Alex : I would modify your #1 to put a period after "you should not use the object again." You should never use an object again after you release it. You may "know" you have another retain, but that makes your code confusing when you could just as easily move the release call somewhere else.
    Roger Nolan : Generally that is true but I think it is acceptable to maintain a weak reference in many cases. Without this ability, you (can) end up with two objects mutually retaining each other and a leak. As a rule of thumb, you are right.
    Roger Nolan : ...or if you have two retains and release one but know you still have the other retain.

0 comments:

Post a Comment