问题描述
||
我在项目的许多地方的各种功能下使用了以下代码行:
NSURLConnection *theConnection = [[NSURLConnection alloc] initWithRequest: theRequest delegate:self];
现在,我得到一个常规警告“未使用的变量theConnection”。
我也知道它的内存泄漏。
可以使用以下代码吗?
NSURLConnection *theConnection = [[NSURLConnection alloc] initWithRequest: theRequest delegate:self];
[theConnection release];
如果我释放Connection对象,在didReceiveData,connectionDidFinishLoading等委托方法中会出现问题吗?
而如果以上语句可以毫无问题地解决内存泄漏,该如何摆脱警告“未使用的变量等等……”?
解决方法
我想你可以用这个
[[NSURLConnection alloc] initWithRequest:request delegate:self];
我认为这不会显示警告
或者,我认为您也可以尝试将连接对象作为类的实例变量。
theConnection = [[NSURLConnection alloc] initWithRequest: theRequest delegate:self];
您可以通过基于dealloc
的代码或其他方法释放theConnection
来避免泄漏
,Now I\'m getting an usual warning \"Unused variable theConnection\". I also know that its leaking memory.
之所以收到警告,是因为您没有使用theConnection
对象,
如果您不希望此实例不调用delgate函数,请将set8ѭ设置为nil
而不是self
[[NSURLConnection alloc] initWithRequest:request delegate:nil];
编辑:
在您的.h课中:
NSURLConnection* m_URLConnection;
在.m类中:
-(id) init
{
m_URLConnection = [[NSURLConnection alloc] initWithRequest:request delegate:nil];
}
-(void) dealloc
{
[m_URLConnection release];
m_URLConnection = nil ;
}