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

前端 未结 6 2261
终归单人心
终归单人心 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 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].

提交回复
热议问题