views:

71

answers:

3

Hello guys!

I found a part of a code written by someone else.

@interface Fly : CCSprite
{   
id var1;
id var2;
}

Then in the .m file

- (void) dealloc
{
[var1 release];
[var2 release];

// don't forget to call "super dealloc"
[super dealloc];
}

It is written right? I don't think the id type can be released. Maybe the isa variable in it... Can you explain me why this is released? Or can you help me to explain why is this bad?

+1  A: 

The id is a dynamic typing and it can reference to any object. And because it is an object, it can be released and deallocated

vodkhang
A: 

The code seems right to me.

id var1

is just declaring a generic object variable. It can be released seeing as its just an object in memory

and then both variables are being released from memory during deallocation

dubbeat
+3  A: 

id can hold any object, and when you send a message to it, it may or may not respond to it. If it does respond to release (which most classes in Cocoa do, as they're subclassing NSObject) it will be released. Depending on which OS this is being run on, you may get an exception if the object does not respond to the message (iOS throw an exception, OS X logs and continues), but that can be checked at runtime using respondsToSelector:.

paxswill