NSData initWithContentsOfURL: does not return the original allocation?
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.
精彩评论