質問

NSURLConnection *接続は、クラスのプロパティです。

@property (nonatomic, retain) NSURLConnection *connection;

インスツルメンツは、私は以下のコードの2行目にNSURLConnectionオブジェクトをリークしてることを報告してます。

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

タグdidFinishLoadingdidFinishWithErrorデリゲートセレクタでは、私は接続を解放することだし、nilに設定します
[self.connection release];
self.connection = nil;

私は「NSURLConnectionが漏れ?」のポストや他のいくつかを読みました。私は完全に何かを明らかに不足しているしなければならないような気がします。ヘルプ?

役に立ちましたか?

解決

卵からのコメントが言ったように、

は、あなたの接続プロパティ(カウント2を保持)で再びそれを保持し、接続を(数1を保持)を割り当てるとしています。あなただけのデリゲートセレクタに一度リリースします。次の2つのオプションがあります:

1)を割り当てるのではなく保持するために、あなたの接続プロパティを変更します。

@property (nonatomic, assign) NSURLConnection *connection;

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

@property (nonatomic) NSURLConnection *connection;
それはあなたの接続プロパティによって保持された後、

2)割り当てられたオブジェクトを解放します:

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

オプション2はALLOC及び放出が可能な限り互いに近づけているので漏れの可能性が少ないがあるので好ましいです。以前の接続を解放するのを忘れた場合も、合成された方法はあなたのために以前のものをリリースする予定。 deallocでself.connectionを解放することを忘れないでください。

ライセンス: CC-BY-SA帰属
所属していません StackOverflow
scroll top