Skip to main content

In objective-C (iphone), how do I manage the memory of "@protocol" references?



I thought I had a pretty good handle on memory management for objective-c, but I can't figure out the following situation:










@protocol MyProtocol

@end












@interface MyObject : NSObject {

id<MyProtocol> reference;

}

@property (nonatomic, retain) id<MyProtocol> reference;

@end












@implementation MyObject

@synthesize reference;

-(void) dealloc {

[reference release];

[super dealloc];

}

...

@end










This gives me a " warning: '-release' not found in protocol(s) ". Can I safely ignore this error? Or am I doing something horribly wrong?



Source: Tips4all

Comments

  1. Yes you can safely ignore this error. An object declared as type id<MyProtocol> may not inherit from NSObject (you do not have to use the Cocoa libraries to program in Objective-C and there are other root classes even in Cocoa such as NSProxy). Since retain (and release, autorelease) are declared in NSObject, the compiler cannot know that the instance declared as type id<MyProtocol> responds to these messages. To get around this, Cocoa also defines the NSObject protocol which mirrors the NSObject API. If you declare your protocol as

    @protocol MyProtocol <NSObject>
    @end


    indicating that MyProtocol extends the NSObject protocol, you'll be set.

    ReplyDelete
  2. Ordinarily, when you declare an object as id it counts an an "any" object (meaning that Objective-C will let you invoke any method from any class or protocol on the id without warning).

    However, when you declare an object as id<SomeProtocol>, the meaning changes. In this case, you are instead saying: I will only invoke SomeProtocol methods on this object.

    The method:

    - (void)release;


    is declared in the NSObject protocol but you have explicitly stated: I will only invoke MyProtocol methods. So the compiler gives you a warning to tell you that you've broken your own promise.

    Therefore, instead of:

    id<MyProtocol> reference;


    you should actually declare:

    id<MyProtocol, NSObject> reference;


    or:

    NSObject<MyProtocol> reference;


    since NSObject (the class) implements NSObject (the protocol).

    or:

    id reference;


    which is the broadest of the lot: let me invoke anything on this object and never complain.

    You can also (as Barry Wark suggested) have MyProtocol include the NSObject protocol -- although from a design perspective, you normally only do this if implementing MyProtocol necessarily means using NSObject. Normally, we only do this if NSObject and MyProtocol are linked heritarily or semantically.



    A little information about the NSObject protocol:

    Everything you invoke retain/release/autorelease upon must implement this protocol. As you can infer from this: basically everything implements the NSObject protocol (even though a few things don't descend from the NSObject base class).

    Another quick clarification: NSObject (the class) and NSObject (the protocol) are not reimplementations of the same API. They are split as follows:


    NSObject (protocol) implements everything required to handle/inspect an existing object in a generic sense (retain/release, isEqual, class, respondsToSelector: etc).
    NSObject (class) implements less generic methods: construction/destruction, thread integration, scripting integration.


    So in most senses, the protocol is the more important of the two. Remeber that the class includes the protocol so if you descend from NSObject, you get both.

    ReplyDelete
  3. Change
    @property (nonatomic, retain) id reference;
    to
    @property (nonatomic, assign) id reference;

    Why bother to retain the object implementing the protocol? All you need is a pointer to the object by which we can call methods declared in your protocol.

    ReplyDelete

Post a Comment

Popular posts from this blog

Why is this Javascript much *slower* than its jQuery equivalent?

I have a HTML list of about 500 items and a "filter" box above it. I started by using jQuery to filter the list when I typed a letter (timing code added later): $('#filter').keyup( function() { var jqStart = (new Date).getTime(); var search = $(this).val().toLowerCase(); var $list = $('ul.ablist > li'); $list.each( function() { if ( $(this).text().toLowerCase().indexOf(search) === -1 ) $(this).hide(); else $(this).show(); } ); console.log('Time: ' + ((new Date).getTime() - jqStart)); } ); However, there was a couple of seconds delay after typing each letter (particularly the first letter). So I thought it may be slightly quicker if I used plain Javascript (I read recently that jQuery's each function is particularly slow). Here's my JS equivalent: document.getElementById('filter').addEventListener( 'keyup', function () { var jsStart = (new Date).getTime()...

Is it possible to have IF statement in an Echo statement in PHP

Thanks in advance. I did look at the other questions/answers that were similar and didn't find exactly what I was looking for. I'm trying to do this, am I on the right path? echo " <div id='tabs-".$match."'> <textarea id='".$match."' name='".$match."'>". if ($COLUMN_NAME === $match) { echo $FIELD_WITH_COLUMN_NAME; } else { } ."</textarea> <script type='text/javascript'> CKEDITOR.replace( '".$match."' ); </script> </div>"; I am getting the following error message in the browser: Parse error: syntax error, unexpected T_IF Please let me know if this is the right way to go about nesting an IF statement inside an echo. Thank you.