Re: dealloc and instance variables
Re: dealloc and instance variables
- Subject: Re: dealloc and instance variables
- From: "Louis C. Sacha" <email@hidden>
- Date: Sun, 23 Nov 2003 00:53:01 -0800
Hello...
They're both correct, and consistent with each other. NSObject only sends
release to any instance variable *it knows about*, which doesn't include
anything in a subclass.
Actually, that's not quite what it means.
The purpose of a dealloc message is to deallocate all the memory
occupied by the receiver. NSObjects version of the method deallocates
the receivers instance variables, but doesnt follow any variable
that points to other memory.
This means that the NSObject dealloc method deallocates the memory
for _all_ instance variables, even those that are added by subclasses
(so every instance variable for a particular class, and all the
instance variables that it inherits from its superclass hierarchy are
all deallocated).
But most of the instance variables for every class are actually
pointers, not the objects themselves. What the NSObject dealloc
method does is deallocate all of the memory used to store floats,
integers, structs, etc..., and the pointers. When it says that it
doesn't follow any variable that points to other memory, it is
refering to the fact that the memory occupied by the pointer is being
deallocated, but not the actual object that the pointer refers to.
That's why your class needs to have it's own dealloc implementation,
that releases the objects that the pointers refer to (so that the
memory used for the objects will be deallocated if no longer needed),
and then calls the super implementation, so that it eventually gets
back to the NSObject version which deallocates the memory that is
used by the instance of your class to store the pointer. If you
didn't send a release message to your objects in the dealloc message,
they would not know that they are no longer needed, and would never
be deallocated, even though the pointers to those objects would no
longer exist...
For example:
@interface YourClass
{
NSString *myString; /* this instance variable is a pointer to
an NSString object */
}
- (id)initWithString:(NSString *)aString;
@end
@implementation YourClass
- (id)initWithString:(NSString *)aString
{
myString = [aString copy]; /* this method returns a pointer to
an NSString, which is stored in myString */
}
- (void)dealloc
{
/* this sends a release message to the NSString (stored at
myString, not in myString) */
[myString release];
/* this calls the dealloc method of your superclass, which
releases it's objects and passes the message */
/* on, and eventually gets to NSObject, which deallocates the
memory for the pointer (myString) */
[super dealloc];
}
@end
Maybe someone else on the list can explain it more clearly if this
doesn't help...
Louis
_______________________________________________
cocoa-dev mailing list | email@hidden
Help/Unsubscribe/Archives:
http://www.lists.apple.com/mailman/listinfo/cocoa-dev
Do not post admin requests to the list. They will be ignored.