Is calling [self release] allowed to control object lifetime?

前端 未结 6 2242
终归单人心
终归单人心 2020-12-07 00:40

I want to create an object in Objective C but I don\'t hold a reference to it.

Is it allowed to let the object control its own lifetime by calling [self release]?

相关标签:
6条回答
  • 2020-12-07 00:59

    To quote the great philosopher Alicia Silverstone, "I had an overwhelming sense of ickiness" when I read that. But I couldn't really tell you why.

    I think I would use autorelease rather than a simple release since you're still executing code in self when you call it, but other than that I can't think of any technical reasons why it wouldn't work.

    0 讨论(0)
  • 2020-12-07 00:59

    Well part of the protocol is that if you send release to self, then you should have sent retain once as well, which I suppose you do. Then there is nothing fishy. I mean the allocing code must be able to control the lifetime of your instance; it itself can only prolong its life, never make it shorter (since making it shorter, then you'd suddenly leave the allocing owner of the instance with an invalid pointer).

    0 讨论(0)
  • 2020-12-07 01:04

    If you see this in code, its probably wrong. However there are legitimate response for it in certain circumstances that are arguably defensible. (So make sure you are doing it for the right reasons.)

    A good example of when this makes sense, is when you create an object that goes off to download a url. The object sits in memory while downloading the url, then sends a message to its delegate saying the data is ready (or url couldn't be downloaded). Once its message has been sent it destroys itself as its no longer needed. In this situation the code/function that created the 'url downloader' may no longer even be in memory, i.e. if it was called in response to a user selection a menu item or an action in a view controller that is no longer on the screen.

    This is useful when the code that creates the "download" object doesn't care if the download completes or not.

    0 讨论(0)
  • 2020-12-07 01:15

    And I will use [self autorelease] instead of [self release]. Because usually it's called in

    - (void)aMethod
    {
        [self.delegate aDelegateMethod:self];
        [self release];
    
    //If you add code related to self here, after [self release], you are making a huge mistake.
    }
    

    If I use [self autorelease], I can still do something after autorelease.

    0 讨论(0)
  • 2020-12-07 01:20

    The rules are simple. You should only release an object if you own it. i.e. the object was obtained with a method starting "new" or "alloc" or a method containing copy.

    Cocoa Memory Management Rules

    An object must not therefore do [self release] or [self autorelease] unless it has previously done [self retain].

    0 讨论(0)
  • 2020-12-07 01:21

    It's legal, but be careful. You want to be sure nothing else is going to send you a message after you release yourself.

    I've done this kind of thing for a faulting scheme back before we had CoreData.

    0 讨论(0)
提交回复
热议问题