Re: looking for a memory problem
Re: looking for a memory problem
- Subject: Re: looking for a memory problem
- From: Ken Thomases <email@hidden>
- Date: Tue, 17 Jul 2012 10:20:55 -0500
On Jul 17, 2012, at 9:39 AM, Martin Hewitson wrote:
> @property (retain) NSArray *words;
>
> NSAutoreleasePool* pool = [[NSAutoreleasePool alloc] init];
>
> NSArray *someWords = [self generateABunchOfAutoReleasedObjectsInAnAutoreleasedArray];
You may be doing something wrong in -generateABunchOfAutoReleasedObjectsInAnAutoreleasedArray.
> self.words = myAutoreleasedArray;
I assume you meant someWords here.
> So I must not be understanding something (likely) because I thought that the autoreleased objects get retained when I add them to the autoreleased array. Then when I set the array to the property, it gets retained and so draining the pool shouldn't result in releasing the objects, should it?
Well, it should result in sending -release messages to the objects. That's what the autorelease pool does, it sends release to the objects it contains when it's drained. However, those release messages are for the original ownership from when the objects were created. You have put them in a collection, which establishes a separate, additional ownership, and you've retained the collection. So, you're correct that the objects should survive the draining of the autorelease pool.
> And, in fact, with some judicious log statements I see the objects don't get released when the pool drains, only much later when the document is closed.
>
> And yet, if I don't assign the autoreleased array to the property then I never see the crash and I see the objects being deallocated when the pool drains.
Don't guess. Don't try to intuit the behavior. Use the Zombies instrument. When it tells you a deallocated object has been messaged, review the recorded retain and release history of that object and see which code is not following the memory management rules – that is, which has under-retained or over-released the object.
Regards,
Ken
_______________________________________________
Cocoa-dev mailing list (email@hidden)
Please do not post admin requests or moderator comments to the list.
Contact the moderators at cocoa-dev-admins(at)lists.apple.com
Help/Unsubscribe/Update your Subscription:
This email sent to email@hidden