如何在iPhone(目标C)中释放NSURLConnection

|| 我在项目的许多地方的各种功能下使用了以下代码行:
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 ;
}
    

要回复问题请先登录注册