开发者

Question regarding Memory leak in Objective-C

开发者 https://www.devze.com 2023-02-07 09:12 出处:网络
If I assign something like self.connection = [NSURLConnection connectionWithRequest:request delegate:self];

If I assign something like

self.connection = [NSURLConnection connectionWithRequest:request delegate:self];

Now if do the following:

开发者_如何学Go

self.connection = nil;

will this be a memory leak? If No, then why?


This won't be a memory leak. Its because of how objective c properties are implemented. I'm assuming you are using retain in your property declaration. Now when you do self.connection, the following method will be called. Since your connection is released first and then a retain operation to nil is performed which will just return nil. So no memory leak will occur.

-(void)setConnection:(NSURLConnection *)newConnection {
    if (connection != newConnection) {
        [connection release];
        connection = [newConnection retain];
    }
}

You can find details here http://developer.apple.com/library/mac/#documentation/Cocoa/Conceptual/ObjectiveC/Articles/ocProperties.html

0

精彩评论

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

关注公众号