views:

46

answers:

3

Hi All,

I need a clarification in releasing the objects assigned to the protocol type.

I have a protocol name iTaskFinish;

I need to assign a particular object for this protocol to which class I have implemented the methods.

so I declared as .h as follows

id<iTaskFinish> taskFinish;

@property(nonatomic, retain) id taskFinish;

so if i assign someobject to this "taskFinish" as follows :

[self setItaskFinish:someObject]

do I need to release the object ??

I tried releasing the object as follows :

[iTaskFinish release];

but its giving a compilation warning as "protocol cannot be released"..

I am not clear that if we retain objects of some other class ,we need to release . But for protocol if we did not release the object retained, wont those memory gets leaked in the application

Any clarification would be greatly appreciated ...

+1  A: 

There's no such thing as protocol type.

A protocol is simply a list of method declarations, unattached to a class definition.

So if your class conforms to a protocol it means that it just takes an obligation to implement required methods defined in that protocol and states that it might implement its optional methods. You should definitely read a "Protocols" (as well as all others) part of Obj-C reference.

To release an object (not a class or protocol) you must just send it a release message:

[taskFinish release];

In your example if you define a property for taskFinish then you can set it by self.taskFinish = someObject - automatically generated setter method will do all basic memory manipulation for you.

Vladimir
A: 

You can just assign it back to nil object like---

[self setItaskFinish:nil];

this will solve ur purpose

Madhup
Actually automatically generated setter is setTaskFinish unless there;s a typo in a question somewhere
Vladimir
A: 

Madhup is right, [self setTaskFinish:nil] will work but if you want to release it somewhere manually, this is how you do it:

You have to change your protocol declaration like so

@property iTaskFinish <NSObject>

and your property like

@property (nonatomic, retain) <iTaskFinish> taskFinish;

This will mean that your taskFinish has the methods defined in <iTaskFinish> but also has the methods in <NSObject>, one of which is release :)

Sam

deanWombourne