开发者

properly creating object in objective-c for avoiding memory leaks

开发者 https://www.devze.com 2023-01-14 02:18 出处:网络
after having spent few months in trying to master the syntax and rules, I am going deeper in memory management rules.

after having spent few months in trying to master the syntax and rules, I am going deeper in memory management rules. One thing I do not understand and causing me confusion is how one creates objects.

Based on what stated in apple memory management guide, the following is a valid approach:

– (NSArray *)sprockets {
    NSArray *array = [NSArray arrayWithObjects:mainSprocket,auxiliarySprocket, nil];
    return array;
}

because I am not causing any memory leaks. The reason why is that it's not using alloc for creating array and therefore sprockets is not the owner. However I am wondering now what's inside arrayWithObjects. Because it happens that in my apps I often have factory for creating custom objects using something similiar to:

return [[MyObject alloc] initWithParameter:(id)params]; // possible leak

If I want to change with a static method like:

return [MyObject initWithParameter:(id)params];

what could be in initWithParameter for adhere to memory rules ? And what if MyObject extends some other object ? I also find out that method naming rules are important to properly advise programmer, what are this rule ? Also could yo开发者_如何学编程u point out a web link where this is explained (I am not yet good in finding docs on apple web site).

thanks


This is the page you're looking for: Memory Management Rules. It all comes down to adhering to the rules of ownership for an object.

If you create an object using alloc then you own that object and must release it. For example:

NSString* str = [[NSString alloc] init]; 
//do something with your str object 
[str release];
str = nil; //don't forget to set it to nil, it's still 
           //pointing to the (now unallocated) block of memory

If you create an object using a factory method, for example:

NSString* str = [NSString stringWithString:@"blah"];

Then what is happening here is that it is creating the string object using alloc for you and then returning the object but first adding it to the local autorelease pool. To use the example in your question.

return [[MyObject alloc] initWithParameter:(id)params]; // possible leak
return [[[MyObject alloc] initWithParameter:(id)params] autorelease]; //now autoreleased -- no leak

Here is the page on Autorelease Pools. Apple say it better than me.

An autorelease pool is an instance of NSAutoreleasePool that “contains” other objects that have received an autorelease message; when the autorelease pool is deallocated it sends a release message to each of those objects. An object can be put into an autorelease pool several times, and receives a release message for each time it was put into the pool. Thus, sending autorelease instead of release to an object extends the lifetime of that object at least until the pool itself is released (the object may survive longer if it is retained in the interim).

Method naming rules are important as it gives a strong indication as to whether the method returns an autoreleased object. In this example I would name the method:

[MyObject myObjectWithParameter:(id)param]

Hope this has helped.


It is standard practice for alloc and creation methods to return a retained object. Anything that starts with init, create, or the object name will clue other programmers into the purpose of the method.

Even if you create with alloc, if the purpose of your method is to pass on ownership, you should not release the object.

0

精彩评论

暂无评论...
验证码 换一张
取 消