Re: releasing an object
On Sat, Oct 10, 2009 at 12:21 PM, Shawn Erickson wrote: > > Anyway the amount of code you have posted leave to much out to understand > all that may be wrong with your memory management. The code does show one common anti-pattern - calls to -retain and -release that should be hidden in an accessor method. Each instance variable that refers to an object should have a setVariable: setter method that properly releases the old value and retains the new value. Encapsulating the memory-management code makes it far easier to debug - scattering it throughout your code leads to torn hair and madness. It's also good OOP; when you find yourself repeating some code around access to an instance variable, it's often a good idea to factor the repetitious code into accessor methods. That makes the repeated code easier to manage, and leaves less clutter in the calling code. sherm-- -- Cocoa programming in Perl: http://camelbones.sourceforge.net ___ Cocoa-dev mailing list (Cocoa-dev@lists.apple.com) 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: http://lists.apple.com/mailman/options/cocoa-dev/archive%40mail-archive.com This email sent to arch...@mail-archive.com
Re: releasing an object
Ooops meant... if (selectedPTVolume != nil) { volumeScanType = [selectedPTVolume volumeScanType]; [selectedPTVolume release]; [outlineViewData reloadData] } On Sat, Oct 10, 2009 at 9:21 AM, Shawn Erickson wrote: if (selectedPTVolume != nil) > { > volumeScanType = [selectedPTVolume volumeScanType]; > [outlineViewData reloadData] > } > ___ Cocoa-dev mailing list (Cocoa-dev@lists.apple.com) 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: http://lists.apple.com/mailman/options/cocoa-dev/archive%40mail-archive.com This email sent to arch...@mail-archive.com
Re: releasing an object
On Sat, Oct 10, 2009 at 8:43 AM, Nick Rogers 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 (Cocoa-dev@lists.apple.com) 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: http://lists.apple.com/mailman/options/cocoa-dev/archive%40mail-archive.com This email sent to arch...@mail-archive.com
Re: releasing an object
Am 10.10.2009 um 17:43 Uhr schrieb Nick Rogers: selectedPTVolume = [[Volume alloc] initWithDictionary:data]; [selectedPTVolume retain]; Read the memory management rules again. By calling alloc the retain count of the created object is one. There is no need to send it an additional retain message. if I comment this the GUI hangs, Then you've got another problem somewhere else. [selectedPTVolume release]; selectedPTVolume = nil; if (outlineViewData) [outlineViewData reloadData];/ crashes here I guess it crashes because you just pulled the outline view's model data from under it's feet. How is the outline view connected to the model? Andreas ___ Cocoa-dev mailing list (Cocoa-dev@lists.apple.com) 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: http://lists.apple.com/mailman/options/cocoa-dev/archive%40mail-archive.com This email sent to arch...@mail-archive.com
Re: releasing an object
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]]; selectedPTVolume = [[Volume alloc] initWithDictionary:data]; [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. 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 } Releasing selectedPTVolume also releases HDIR *dirRoot. Wishes, Nick On 10-Oct-2009, at 4:06 PM, Graham Cox wrote: On 10/10/2009, at 9:31 PM, Nick Rogers wrote: Shall I get its retainCount and then release it that many times? For heavens' sake, NO! Ignore retain counts. They are not reliable evidence of anything you can use. Instead, just follow the rules: http://developer.apple.com/mac/library/documentation/Cocoa/Conceptual/MemoryMgmt/Articles/mmRules.html I'm doing alloc and initWithDictionary to get an instance of Volume. I have to do [selectedPTVolume retain]; when allocing to get it to work properly. Show your code. This sounds wrong, but without seeing the code it can't really be inferred from the description. --Graham ___ Cocoa-dev mailing list (Cocoa-dev@lists.apple.com) 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: http://lists.apple.com/mailman/options/cocoa-dev/archive%40mail-archive.com This email sent to arch...@mail-archive.com
Re: releasing an object
On 10/10/2009, at 9:31 PM, Nick Rogers wrote: Shall I get its retainCount and then release it that many times? For heavens' sake, NO! Ignore retain counts. They are not reliable evidence of anything you can use. Instead, just follow the rules: http://developer.apple.com/mac/library/documentation/Cocoa/Conceptual/MemoryMgmt/Articles/mmRules.html I'm doing alloc and initWithDictionary to get an instance of Volume. I have to do [selectedPTVolume retain]; when allocing to get it to work properly. Show your code. This sounds wrong, but without seeing the code it can't really be inferred from the description. --Graham ___ Cocoa-dev mailing list (Cocoa-dev@lists.apple.com) 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: http://lists.apple.com/mailman/options/cocoa-dev/archive%40mail-archive.com This email sent to arch...@mail-archive.com
releasing an object
Hi, There is a instance variable in my AppController class named "Volume". I'm doing alloc and initWithDictionary to get an instance of Volume. I have to do [selectedPTVolume retain]; when allocing to get it to work properly. When destroying this object to get a new one, I'm doing [selectedPTVolume release]; but then the retainCount is still one. If I release it twice, the GUI hangs (selectedPTVolume contains the root item of a outline view). How to release it properly? Shall I get its retainCount and then release it that many times? Thanks, Nick ___ Cocoa-dev mailing list (Cocoa-dev@lists.apple.com) 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: http://lists.apple.com/mailman/options/cocoa-dev/archive%40mail-archive.com This email sent to arch...@mail-archive.com