Re: releasing an object
Re: releasing an object
- Subject: Re: releasing an object
- From: Shawn Erickson <email@hidden>
- Date: Sat, 10 Oct 2009 09:21:43 -0700
On Sat, Oct 10, 2009 at 8:43 AM, Nick Rogers <email@hidden> wrote:
> Hi, Thanks for the reply.
>
> In AppController.h I have an ivar:
> Volume *selectedPTVolume;
>
> In a method in AppController, I have the following:
> NSMutableDictionary *data = [partitionListDictArray
> objectAtIndex:[tableViewPTList selectedRow]];
>
The above is an object this code doesn't own but has gain access to "use".
> selectedPTVolume = [[Volume alloc] initWithDictionary:data];
>
The above is a new object that this code owns and hence has the
responsibility to relinquish ownership of before this code loses its
reference to the object. I presume that -[Volume initWithDictionary] is
coded to take ownership (retain) of "data", if not it needs to. Then in
-[Volume dealloc] it needs to release ownership.
> [selectedPTVolume retain];// if I comment this the GUI hangs,
> selectedPTVolume contains an ivar HDIR *dirRoot; which is passed as the root
> item of an outline view. dirRoot has an mutable array, which contains other
> HDIR objects as its children and so on a tree is formed.
>
You already have ownership of the object pointed at by selectedPTVolume, no
need to retain it again (aka no need to claim ownership again).
I rewrote the following...
In a method, where I need to release selectedPTVolume, I have:
> if (selectedPTVolume)
> {
> volumeScanType = [selectedPTVolume volumeScanType];//
> selectedPTVolume retainCount = 2 here
[selectedPTVolume release];
> [selectedPTVolume release];// selectedPTVolume retainCount = 1 here
> selectedPTVolume = nil;// selectedPTVolume retainCount = 0 here and
> sometimes still shows as 1 while debugging.
> if (outlineViewData)
> [outlineViewData reloadData];/ crashes here
> }
...as with the likely possibility of removing the "if (selectedPTVolume !=
nil)" depending on what zero means for volumeScanType...
if (selectedPTVolume != nil)
{
volumeScanType = [selectedPTVolume volumeScanType];
[outlineViewData reloadData]
}
Anyway the amount of code you have posted leave to much out to understand
all that may be wrong with your memory management. Also you haven't posted
much information about the crash to help us understand the location of it.
Look up information on how to use NSZombie or if you are on Snow Leopard use
the Zombie feature of Instruments to track down memory management issues
like this.
-Shawn
_______________________________________________
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