NSImage initialization question
NSImage initialization question
- Subject: NSImage initialization question
- From: Dan Huntsinger <email@hidden>
- Date: Mon, 05 Nov 2001 12:48:37 -0800
Hello all,
I'm fairly new to this, and I'm having some problems with my NSImage
objects. I have a class that I have created that contains an NSImage object
as an instance variable. I have created a custom init method, which
initializes the image. I was having a lot of problems with not being able
to reference the image after it was created, and I tracked it down to the
fact that I was releasing the title String after using it, because I thought
it was no longer needed, as in the code below:
- initObject
{
if (self = [super init])
{
/* class-specific initialization goes here */
NSString *title;
title = [[NSBundle mainBundle] pathForImageResource:@"aSrcImage"];
theImage = [[NSImage alloc] initWithContentsOfFile:title];
// [title release]; // causes crash "EXC_BAD_ACCESS"
// [title autorelease]; // causes crash "EXC_BAD_ACCESS"
}
return self;
}
As you can see , now that I've commented out the line that releases "title"
it works, but I believe this is a memory leak as "title" will never be
released, and I have no way to access it outside of this method. Can
someone please give me a tip as to what else might work, or an alternative
method. Maybe I'm just doing this wrong to begin with. =)
I understand that when I release title, I get the error because later on, it
is trying to access the "title" string which no longer exists. I thought
that "autorelease" might solve it but that doesn't work either. Can anyone
explain to me (better than apple's docs do) how images are initialized and
used, and why I need to keep the "title" string around for longer than this
method.
Thanks,
Dan