Path: utzoo!attcan!uunet!cs.utexas.edu!swrinde!zaphod.mps.ohio-state.edu!sunybcs!boulder!gore!jacob From: jacob@gore.com (Jacob Gore) Newsgroups: comp.sys.next Subject: Re: [self free] is a bad example Message-ID: <130056@gore.com> Date: 12 Jan 90 19:15:13 GMT References: <1842@opus.cs.mcgill.ca> Reply-To: jacob@gore.com (Jacob Gore) Organization: Gore Enterprises Lines: 114 / comp.sys.next / clement@opus.cs.mcgill.ca (Clement Pellerin) / Jan 11, 1990 / > In the SysRefMan chap. 3 p.27 > there is an example on how to use the message free in class Object. [...] > The example is [self free]. This is indeed a bad example. It's technically correct, but it belongs in something like "Objective-C Traps and Pitfalls", not as an example for the typical use of -free. > Note that self will be deallocated by the time this message returns. The > object class of self will not be deallocated and hence the method will > still be available. The instance variables of self will be gone. I don't follow this reasoning. Whether or not the methods of the former self are still callable is at best implementation-dependent. For example, if methods are reached through the 'isa' instance variable (a pointer to the class struct), and that variable is not available anymore, neither are the methods. You may get "lucky" if all pointers just happen to be intact in memory, or because the method is cached, etc. In any case, trying to access instance variables or instance methods of a freed objects is a bad idea. > Should > an object that does not exist anymore be allowed to continue executing a > method if it does not access its variables nor sends a message to itself? Yes, since it may not access its variables or send messages to itself for the rest of the execution of the method. Besides, 'self' is assignable. One may want to do something like this: - finishUnarchiving { if ([self isInconsistent]) { [self free]; self = [SelfsClass new]; [self setSomeInstanceVariables]; } return self; } > What does the objective C ref manual say about this? Don't know, don't have one. > I tried the following program and the variables are kept after [self free] > but you can't send a message to self. I think that's a fluke. > I would prefer if I would get a run-time error by accessing the variables. I agree. > let me propose a different example: > > id anObject; > > anObject = [AClass new]; > // use anObject until you don't need it > [anObject free]; > // continue execution without anObject Yes, except I'd use "AClass *anObject" instead of "id anObject". Now that we finaly have compile-time object typing in Objective-C, let's encourage people to take advantage of it. I would also provide an example on how to write the -free method for an object: #import "Something.h" @interface Example : Object { char *aString; Something *aSomething; } + new; - free; @end #import @implementation Example + new { self = [super new]; aString = malloc(100); aSomething = [Something new]; return self; } - free { if (aString) free(aString); // May still fail if aString is // neither NULL nor points to a // malloc'ed chunk. [aSomething free]; // If aSomething is nil, any // message sent to it is a no-op. return [super free]; // The convention is for all -free // methods to return nil. } @end > Is it worth contacting NeXT? Definitely about the manual, and probably about having run-time errors generated in the situation you presented. Are you going to? Jacob -- Jacob Gore Jacob@Gore.Com boulder!gore!jacob