Re: init vs awakefromnib
Re: init vs awakefromnib
- Subject: Re: init vs awakefromnib
- From: Mark Ritchie <email@hidden>
- Date: Mon, 12 Mar 2007 21:41:08 -0400
Hi Ahmet!
The following is somewhat long winded... Won't we all be happy when
we have modern garbage collection!! (as announced here: http://
www.apple.com/macosx/leopard/xcode.html)
If instances of your object are getting created in IB and then
archived, then you'll need to make sure that the encodeWithCoder: and
initWithCoder: methods are properly implemented. That might explain
the troubles you're having.
If you've bound someArray in one of your NIB files (perhaps via
File's Owner, via direct instantiation) then you might also
experience troubles with references to objects mysteriously going
away. (long shot here but possible with right combination of
incorrect assignment methods and unarchiving issues.)
On 10-Mar-07, at 6:44 PM, Ahmet Taha Sakar wrote:
1) Why does the location of alloc and initilizaing a class
variable code
make this difference?
The location of the alloc/init (or new) should not matter. Something
else is going on.
2) Arent all class variables automaticaly retained until the whole
class is released?
In a word, no. Objects which are created with alloc/init or new are
retained until they are explicitly released or indirectly by an
autorelease pool.
3) What's the way to allocate and initialize the class variable in
init method of the class to be able to use it with other methods
later. I have tried adding retain, but it hasnt worked.
The following discussion assumes that we're talking about an instance
variable (where each instance of the class has it's own private
copy.) If you meant a class variable (a static variable which is as
close as we get in Objective-C to real class variables) which is
shared by all instances of the class then the following isn't exactly
what you want. Ping me if that's the case and I'll post the alternate
code. Ok, that said, here we go:
My preference is to put all retain and release messages inside the
set method. And yes, I use release as opposed to autorelease so that
double release issue arise as early as possible. Much easier to
track down and fix doing it that way.
// Assuming that I have an instance variable defined like this:
NSMutableArray *__someArray;
// I'd write the accessor and set method like this:
- (NSMutableArray *)someArray {
return __someArray;
}
- (void)setSomeArray:(NSMutableArray *)newValue{
[newValue retain];
[__someArray release];
__someArray = newValue;
}
// and cleanup the instance like this:
- (void)dealloc {
// ...
[__someArray release];
// ...
[super dealloc];
}
// finally, I initialize the variable by calling the set method:
[self setSomeArray:[NSMutableArray array]];
Hope that helps!
Mark
__
Mark Ritchie
Cocoa and WebObjects Developer
Diamond Lake Consulting Inc.
Toronto, Ontario, Canada
_______________________________________________
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