How do I release an object allocated in a different AutoReleasePool ?

Posted by ajcaruana on Stack Overflow See other posts from Stack Overflow or by ajcaruana
Published on 2010-03-22T11:58:03Z Indexed on 2010/03/22 12:11 UTC
Read the original article Hit count: 214

Hi,

I have a problem with the memory management in Objective-C. Say I have a method that allocates an object and stores the reference to this object as a member of the class. If I run through the same function a second time, I need to release this first object before creating a new one to replace it. Supposing that the first line of the function is:

NSAutoreleasePool *pool = [[NSAutoreleasePool alloc] init];

This means that a different auto-release pool will be in place. The code to allocate the object is as follows:

if (m_object != nil)
    [m_object release];

m_object = [[MyClass alloc] init];
[m_object retain];

The problem is that the program crashes when running the last line of the method:

[pool release];

What am I doing wrong ? How can I fix this ?

Regards
Alan

© Stack Overflow or respective owner

Related posts about nsautoreleasepool

Related posts about objective-c