2010-11-30 7 views
0

次のシナリオでは、メモリリークが発生しています。私は辞書にそれを変換するためにSBJSONParserを使用し、30秒ごとにデータを読み、通知を追加し、その後テーブルビューにバインドするためにデータを使用します。AsyncSocketとNotifications - メモリリーク

:私はオブザーバーを持ってCustomViewControllerで

// Read data and send notification 
-(void)onSocket:(AsyncSocket *)sock didReadData:(NSData *)data withTag:(long)tag 
{ 
    NSString *content = [[NSString alloc] initWithData:[data subDataWithRange:NSMakeRange(0, [data length] - 2)] encoding: NSUTF8StringEncoding]; 

    // Line where leaks appear 
    NSMutableDictionary* dict = [[NSMutableDictionary alloc] initWithDictionary:[content JSONValue]]; 

    [content release]; 

    // Post notification 
    [[NSNotificationCenter defaultCenter] postNotificationName:@"BindData" object:nil userInfo:dict]; 

    [dict release]; 
} 

[[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(bindData) name:@"BindData" object:nil]; 

とbindData方法:

-(void)bindData:(NSNotification*)notification 
{ 
    NSAutoreleasePool* pool = [[NSAutoReleasePool alloc] init]; 

    NSMutableArray* customers = [notification.userInfo objectForKey:@"Customers"]; 
    for (NSDictionary* customer in customers) 
    { 
     Company* company = [[Company alloc] init]; 
     company.name = [customer objectForKey:@"CompanyName"]; 
     NSLog(@"Company name = %@", company.name); 
     [company release]; 
    } 

    [pool drain]; 
} 

問題がある:私はその辞書からcompany.name =何かを設定すると、私はライン上のメモリリークを取得:NSMutableDicti onary * dict = [[NSMutableDictionary alloc] initWithDictionary:[コンテンツJSONValue]];私は30秒ごとに読んでいるので、それは増加し続ける。

私は何か助けていただけることを感謝します。ありがとう。

答えて

0

dictは、allocinitを使用しているため(リザーブカウントが1増加します)、決して解放しないため、リークしています。通知が掲載された後に辞書が不要になりますので、あなたは安全にそうように、次の行にそれを解放することができます

// Post notification 
[[NSNotificationCenter defaultCenter] postNotificationName:@"BindData" object:nil userInfo:dict] 
[dict release]; 

は詳細についてはMemory Management Programming Guideを参照してください。

+0

ご意見ありがとうございます。私は自分のコードでdictをリリースしていますが、最初のメッセージに追加するのを忘れてしまっただけです。私はdictをリリースしてもまだ問題が残っています。 – mit

+0

その場合、投稿したコードにリークはありません。それは、それが上に含まれていないいくつかのセクションにある可能性があります。 –

関連する問題