AsyncSocket 和通知 - 内存泄漏

发布于 2024-10-05 21:40:28 字数 1584 浏览 2 评论 0原文

我在以下场景中出现内存泄漏。我每 30 秒读取一次数据,使用 SBJSONParser 将其转换为字典,添加通知,然后使用数据将其绑定到表视图:

// 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];
}

在 CustomViewController 上,我有观察者:

[[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 = 该字典中的某些内容时,我在线上遇到内存泄漏: NSMutableDictionary* dict = [[NSMutableDictionary alloc] initWithDictionary:[content JSONValue]];因为我每 30 秒就读一次,所以这个数字一直在增加。

我很感激你能提供的任何帮助。谢谢。

I have a memory leak in the following scenario. I read data at every 30 seconds, use SBJSONParser to transform it to a dictionary, add a notification and after that use the data to bind it to a tableview:

// 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];
}

On a CustomViewController I have the observer:

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

and the bindData method:

-(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];
}

The problem is: when I set company.name = something from that dictionary, I get a memory leak on the line: NSMutableDictionary* dict = [[NSMutableDictionary alloc] initWithDictionary:[content JSONValue]]; which keeps increasing since I'm reading at every 30 seconds.

I appreciate any help you can give. Thanks.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

赢得她心 2024-10-12 21:40:28

dict 正在泄漏,因为您正在使用 allocinit (从而将其保留计数增加 1),但从未释放它。由于在发布通知后将不再需要该词典,因此您可以在以下行中安全地释放它,如下所示:

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

请参阅 内存管理编程指南 了解更多详细信息。

dict is leaking because you are using alloc and init (thus increasing its retain count by 1), but never releasing it. Since the dictionary will no longer be needed after the notification has been posted, you can safely release it on the following line, like so:

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

See the Memory Management Programming Guide for more details.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文