Does “self.propery = [[SomeClass alloc] init];” leak memory?

前端 未结 4 1876
伪装坚强ぢ
伪装坚强ぢ 2021-01-26 18:07

Is it safe in Objective-C to write

self.propery = [[SomeClass alloc] init];

instead of

SomeClass *tmp = [[SomeClass alloc] init         


        
4条回答
  •  半阙折子戏
    2021-01-26 18:44

    It doesn't make a bit of difference whether or not your property is defined as retain, copy, or assign. When you create a local instance of a class with [[SomeClass alloc] init], you are responsible for releasing it within the scope it was created.

    Kevin's response is correct. If you do not feel like creating, setting, releasing - you can use autorelease. The main autorelease pool is drained from time to time, you will not be using that memory for the lifetime of the application.

    It is worth noting that the unpredictable nature of autorelease pools means that you can not be sure when that memory will be released. If working in a memory constrained platform like the iPhone, you should avoid using autorelease except in places where necessary.

提交回复
热议问题