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: Sam Stigler <email@hidden>
- Date: Tue, 29 May 2007 20:49:05 +1000
Mani: Thanks; that solved it. I'd put in the extra "retain" out of
desperation when nothing else was working. The explanation was
particularly helpful; I'm just having a lot of trouble getting my
head around how memory management works with custom classes etc.
mmalc: Thanks for your advice as well; I will definitely spend some
more time studying the memory management documentation. Even
pointers are still semi-new to me.
Sam
On May 29, 2007, at 8:06 PM, Manfred Schwind wrote:
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.
On May 29, 2007, at 8:07 PM, mmalc Crawford wrote:
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?
Because the variable has been deallocated.
Check your init method, and re-read <http://developer.apple.com/
documentation/Cocoa/Conceptual/MemoryMgmt/index.html>, notably
<http://developer.apple.com/documentation/Cocoa/Conceptual/MemoryMgmt/
Tasks/MemoryManagementRules.html> and <http://developer.apple.com/
documentation/Cocoa/Conceptual/MemoryMgmt/Articles/mmPractical.html>.
mmalc
_______________________________________________
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