Re: Weird problem involving memory management and global variable
Re: Weird problem involving memory management and global variable
- Subject: Re: Weird problem involving memory management and global variable
- From: Manfred Schwind <email@hidden>
- Date: Tue, 29 May 2007 12:06:26 +0200
I've spent the past several hours trying to tackle a very annoying
problem relating to a global variable I have declared in my
MyDocument.h: It is declared there, but I am allocating and
initializing in the document's windowControllerDidLoadNib: method
Everything seems to be fine until another method in the same class
asks for the variable, at which point (although the variable's
retainCount is still 2) I receive an EXC_BAD_ACCESS and
KERN_PROTECTION_FAILURE. Does anyone know why this could be? I've
posted the relevant code to http://homepage.mac.com/sstigler1985/
memoryproblem.html .
[[[AmazonBrowseTree alloc] initWithItem:[AmazonBrowseItem
amazonBrowseItemWithName:@"Apparel" andID:@"1036682"]] retain]
This additional retain makes no sense. Methods with 'init' in the
name already set the retain count to 1. But this is not causing the
crash that you see (but it most probably produces a leak).
_children = [[[NSMutableArray alloc] initWithCapacity:10]
autorelease];
Here is the problem. You create an instance variable, but autorelease
it. Once returned to the main event loop, the array gets released and
(most probably) dealloced and then your _children variable points to
invalid (because already freed) memory.
Solution: remove the autorelease call. Additionally you may do a
[_children release]; in the dealloc implementation of your class, to
not to leak.
Mani
--
http://www.mani.de
iVolume - Loudness adjustment for iTunes.
LittleSecrets - The encrypted notepad.
_______________________________________________
Cocoa-dev mailing list (email@hidden)
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