What's the difference between setting an object to nil vs. sending it a release message in dealloc

前端 未结 4 784
我在风中等你
我在风中等你 2020-12-09 13:49

I have Object:

MyClass *obj= [[MyClass alloc] init];

What\'s the difference between:

[obj release]; // Only obj own this ob         


        
4条回答
  •  孤城傲影
    2020-12-09 14:02

    iOS does not support garbage collection, which means that doing obj = nil would result in a memory leak. If you want automatic control over deallocation, you should do something like: obj = [[[NSObject alloc] init] autorelease] (you must NOT release it if you do that). Autorelease would cause the object to be automatically released when the current NSRunloop event ends. The NSRunloop automatically drains it's NSAutoReleasePool for each event iteration, which is usually very helpful.

提交回复
热议问题