Objective-C, RetainCount

Joined
Jan 6, 2011
Messages
2
Reaction score
0
Points
1
hi,

I am learning obj-c. I know that dealloc automatically start to operate when retain count reaches 0. Im my code [obiektos RetainCount] return 1, but dealloc is called (personally I've thought that [obiektos RetainCount] will return 0 and that's why dealloc is called).

why there is 1, not 0 at output?

output:

2011-04-08 21:19:27.853 Metoda prostokta[20729:a0f] zadzialal dealloc
2011-04-08 21:19:27.869 Metoda prostokta[20729:a0f] 1




Code:
#import "nowycos.h"
//#import "ojej.h"


int main (int argc, const char * argv[]) {
	nowycos*obiektos;
	nowycos*obiektos1;
	
	obiektos=[nowycos new];
	[obiektos release];


	NSLog(@"%d", [obiektos retainCount]);
    
	
	
	
	return 0;
}

here is dealloc:
Code:
-(void) dealloc
{
	NSLog(@"zadzialal dealloc");
	[super dealloc];
}
 
Joined
Dec 13, 2007
Messages
256
Reaction score
10
Points
18
Location
United States of America
Your Mac's Specs
2.1GHz MacBook with 4GB RAM, Mac OS X 10.6, iLife and iWork ‘09
I tried using similar code and got the same result. It seems like what's happening is that the instance hasn't yet been truly freed from memory when NSLog() is called, even though dealloc has been called. If you try to NSLog() the retain count again on the next line, your app will crash because the instance has been freed by then.

In other words, I think you're just seeing the result of a runtime detail that's mostly unimportant. It's unlikely that you'll ever actually need to check the retain count immediately after dealloc is called, although the behavior is strange and unexpected.

I hope that helps, but take my response with a healthy dose of skepticism. I'm honestly not exactly sure what's happening either; that's just the impression I got from experimentation using your code with the debugger.
 

Shop Amazon


Shop for your Apple, Mac, iPhone and other computer products on Amazon.
We are a participant in the Amazon Services LLC Associates Program, an affiliate program designed to provide a means for us to earn fees by linking to Amazon and affiliated sites.
Top