Path: utzoo!attcan!uunet!cs.utexas.edu!jarvis.csri.toronto.edu!clyde.concordia.ca!mcgill-vision!quiche!opus!clement From: clement@opus.cs.mcgill.ca (Clement Pellerin) Newsgroups: comp.sys.next Subject: [self free] is a bad example Message-ID: <1842@opus.cs.mcgill.ca> Date: 12 Jan 90 01:43:11 GMT Reply-To: clement@opus.UUCP (Clement Pellerin) Organization: SOCS, McGill University, Montreal, Canada Lines: 77 In the SysRefMan chap. 3 p.27 there is an example on how to use the message free in class Object. The intent of free is to release the memory taken up by objects that you allocated with new but you don't need anymore. The example is [self 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. 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? What does the objective C ref manual say about this? I tried the following program and the variables are kept after [self free] but you can't send a message to self. I would prefer if I would get a run-time error by accessing the variables. 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 Is it worth contacting NeXT? ------------------------------------------------------------------- // put me in file selffree.m // compile me with cc -ObjC -I/usr/include/objc selffree.m -lNeXT_s -lsys_s // it took me 30mins to find that command, is there a better way? #import @interface Suicide:Object { int x; } - setx: (int)val; - die; - sayhi: (int)cnt; @end @implementation Suicide:Object; - setx: (int)val { x = val; } - die { [self free]; printf("I'm dead but I remember the value of x = %d\n", x); [self sayhi: 2]; } - sayhi: (int)cnt { printf("I'm alive %d\n", cnt); } @end main() { Suicide *suicidal; suicidal = [Suicide new]; [suicidal setx: 111]; [suicidal sayhi: 1]; [suicidal die]; [suicidal sayhi: 3]; } ------------------------------------------------------------------- -- news