开发者

NSData initWithContentsOfURL: does not return the original allocation?

开发者 https://www.devze.com 2023-03-07 17:48 出处:网络
In开发者_Python百科 the documentation of NSData\'s initWithContentsOfURL:, it says: The returned object might be different than the original receiver.

In开发者_Python百科 the documentation of NSData's initWithContentsOfURL:, it says:

The returned object might be different than the original receiver.

What are the implications of that? It seems to imply that a standard "alloc/init" line could leak memory.


there are several reasons why - class clusters being the most publicly recognized:

- (id)initWithContentsOfURL:(NSURL *)url
{
    self = [super init];
    if (self != nil) {
        NSData * result =
           [[NSDataClassClusterSpecialization alloc] initWithContentsOfURL:url];
        [self release];
        return result;
    }
    return self;
}

no leaks are introduced using this form.

it just means that you should only use the result of the alloc+init call (rather than holding onto the result of alloc), which is a good idea in any case -- even when not explicitly documented.

0

精彩评论

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