NSURLConnection Leaks — Why?

喜夏-厌秋 提交于 2019-12-03 10:15:00

As the comment from roe said, you are allocating the connection (retain count 1) and then retaining it again with your connection property (retain count 2). You only release once in the delegate selectors. You have two options:

1) Change your connection property to assign rather than retain.

@property (nonatomic, assign) NSURLConnection *connection;

// OR, since assign is the default you may omit it

@property (nonatomic) NSURLConnection *connection;

2) Release the allocated object after it is retained by your connection property:

NSURLRequest *request = [[NSURLRequest alloc] initWithURL:_url];
NSURLConnection *connection = [[NSURLConnection alloc] initWithRequest:request delegate:self];
self.connection = connection;
[connection release];
[request release];

Option 2 is preferred since there is less of a chance for leaks since alloc and release are as close together as possible. Also, if you forget to release the previous connection the synthesized methods will release the previous one for you. Don't forget to release self.connection in dealloc.

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!