Re: confused about allocating and releasing objects
Re: confused about allocating and releasing objects
- Subject: Re: confused about allocating and releasing objects
- From: Memo Akten <email@hidden>
- Date: Wed, 27 Aug 2008 12:13:28 +0100
ok thanks, I"ve added that link to my ever growing Cocoa bookmarks!!
I'm just writing a Quartz Composer plugin using the QCPlugIn API and
ran into a problem regarding this, my question isn't related to the
QCPlugIn API so I'm posting here first.
At the start of the plugin (in enableExecution) I use the code:
- (void) loadCardXMLData {
NSString *dataPath = @"/data.plist";
cardsLoadedData = [NSDictionary dictionaryWithContentsOfFile:
dataPath];
// cardsLoadedData = [[NSDictionary alloc] initWithContentsOfFile:
dataPath];
if(cardsLoadedData) {
NSLog(@"*** SUCCESSFULLY LOADED %@", dataPath);
} else {
NSLog(@"*** COULD NOT LOAD %@", dataPath);
}
}
If I use the convenience route (with no release in disableExecution) I
get a freeze (I need to force quit QC) as soon as I enable the plugin.
If I use the alloc route (and release in disableExecution) everything
works fine. Is this normal behaviour? or could it be QC related?
P.S. cardsLoadedData is a property of my QCPlugIn class, and I don't
use it at all except at the end of execute I just send it out to QC
with self.outputXMLData = cardsLoadedData;
Cheers,
Memo.
On 27 Aug 2008, at 11:57, Jonathan del Strother wrote:
On Wed, Aug 27, 2008 at 11:45 AM, Memo Akten <email@hidden> wrote:
HI All, i'm a bit confused about the 2 scenarios:
NSDictionary *myData1 = [NSDictionary
dictionaryWithContentsOfFile:@"mydata.plist"]; // this one
I don't
need to release when I'm done?
NSDictionary *myData2 = [[NSDictionary alloc]
initWithContentsOfFile:@"mydata.plist"]; // this one
I do
need to relase when I'm done?
There seems to be a lot of situations in Cocoa where this happens
so I guess
this is a generic question for all cases when an object is created
via means
of a static method vs using alloc:initWith... (e.g. NSArray,
NSString,
NSNumber etc. are just a few I've seen).
Whats the difference between the two methods? (I know the second
one creates
a blank dictionary first, and then loads the file - but I mean
which one is
better for what purpose? - why would I choose one over the other).
Use whichever is easiest. Generally if I'm looking for a temporary
object, I'll use the autoreleased method if present. If I'm going to
keep the object around, I'll use the alloc/init method. But either of
these can be transformed to the other with a retain or autorelease
message, so there's no huge difference.
And am I
correct in assuming if I use the first method i do not need to
release the
variable when I'm done? but do need to release the variable in the
second
instance?
Correct. Review
http://developer.apple.com/documentation/Cocoa/Conceptual/MemoryMgmt/MemoryMgmt.html
_______________________________________________
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
_______________________________________________
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