Does the memory of a deallocated object remains in memory?
Hello,
I'm quite new to Cocoa programming with Objective C, but I've been programming in C/C++ with Qt and SDL for 2 months. I'm trying to understand the retain/release concept so I'm doing some tests.
Here is the main.m:
#import <Cocoa/Cocoa.h>
#import "Person.h"int main(int argc, char *argv[])
{
Person *aPerson= [[Person alloc] init];
NSString *theName;
NSLog(@"Person retain =%ld\n", [aPerson retainCount]);
theName= [aPerson name];
NSLog(@"name= %@\n", theName);
NSLog(@"Person retain =%ld\n", [aPerson retainCount]);
[aPerson release];
NSLog(@"name= %@\n", theName);
NSLog(@"Person retain =%ld\n", [aPerson retainCount]);
return NSApplicationMain(argc, (const char **) argv);
}
The name method in Person class is implemented this way:
-(NSString*)name { return name;}
Person retain =1
name= John
Person retain =1
name= John
Person retain =1152921504606846975
Can anyone help me?
Thanks
You have the right idea here. retain/release are reference counts. However when you release an object, you should never attempt to use it, as it's memory has been restored to the heap. I'm rather surprised that you're getting that odd integer after [person release] however that code most certainly violates the rule that you are asking for information about an object that you have released.
The cocoa country'n'western song is "please release me, if you m'alloced, and don't retain me any more".
So in your case, you could do something like:
static Person* holdForLater ;
void holdEm(Person* p)
[p retain];
[holdForLater release];
holdForLater = p ;
}
main(....)
{
bla bla
NSLog(... person ....) ; // release count = 1
holdEm(p)
NSLog( .... person ...) ; // release count == 2
[ person release]
NSLog( ... person ...) ; // release count == 1 (this is a violation, you've released person)
holdEm(NULL)
}
I hope that helps.
Thank for your answer.
My problem was not to find a solution for this problem, but i was wondering why the program just doesn't crash? My goal is precisely to make the program crash when I call released instances of objects.
Well, the answer's in the heap of course!
When you say NSLog(.... [person retainCount]), the compiler creates the code something like:
NSLog("...",person->retainCount) ;
"go to the address of person"
"step forward by 4 bytes" (or whatever the offset of retainCount happens to be)
"print the integer at that address"
So if person is a valid heap address (and it is), then retainCount will be a valid address. So no reason to crash! Of course, in the operation of returning the person to the heap could cause the memory occupied by person to become deadfeaf or reused as a totally different object. However, it's unlikely to crash because it is a valid memory address (with invalid data). You should be able to watch this with the debugger.
If you want to see it crash, you could say: person += 100000000 ; NSLog(....,[person retainCount]) and you'll probably die a horrible death. person += 100000000 says "increase the address of person by 100million * sizeof (*person). Now that's unlikely to be a good memory address and you'll crash. Or person = (Person*) 10000L ; NSLog(...) ;