componentsSeparatedByString内存泄漏,你能帮帮我吗?

时间:2012-09-20 10:02:58

标签: objective-c crash memory-leaks

资源创建BY:

(void)parseNodeAsMap:(XMPPElement*)message
{
 NSAutoreleasePool *pool = [[NSAutoreleasePool alloc] init];

if ([[message name] isEqualToString:@"presence"]) {
   // NSLog(@"presence--%@--%@",[message attributeStringValueForName:@"type"],[message from].resource);
    if ([[message attributeStringValueForName:@"type"] isEqualToString:@"unavailable"]) {
        [[NSNotificationCenter defaultCenter] postNotificationName:@"VedioChatUserStatusNoti" object:[message from].resource userInfo:[NSDictionary dictionaryWithObject:@"0" forKey:@"key"]];
    }else {
        [[NSNotificationCenter defaultCenter] postNotificationName:@"VedioChatUserStatusNoti" object:[message from].resource userInfo:nil];
    }

}else if([[message name] isEqualToString:@"message"]) {

    if ([[message elementForName:@"body"] stringValue].length<1) {

        [[NSNotificationCenter defaultCenter] postNotificationName:@"VedioChatMsgNoti" object:@"...." userInfo:nil];
    }else {
     //   NSLog(@"message--%@",[[message elementForName:@"body"] stringValue]);
        [[NSNotificationCenter defaultCenter] postNotificationName:@"VedioChatMsgNoti" object:[[message elementForName:@"body"] stringValue] userInfo:nil];
    }

}else {

}
message = nil;
[pool drain];
}

并使用此处:`#pragma mark NOTI

(void)getChatUserStatus:(NSNotification*)noti{

NSAutoreleasePool *pool = [[NSAutoreleasePool alloc] init];
NSArray *user = [[noti object]componentsSeparatedByString:@"@userid"];
if (user.count<2)
{
    RoomUser _ru = {_ru.p_id = userId,_ru.nickName = [[user objectAtIndex:0] copy],_ru.role = @"0"};
    [_listDic setObject:[NSValue valueWithBytes:&_ru objCType:@encode(RoomUser)] forKey:userId];

    int insertIndex = 0;
    ...
    [_listKeys insertObject:userId atIndex:insertIndex];


    onlineUserNum++;
}else {
    RoomUser _ru = {_ru.p_id = [[user objectAtIndex:1] copy],_ru.nickName = [[user objectAtIndex:0] copy],_ru.role = [[user objectAtIndex:2] copy]};

    if (![noti userInfo]) {
        [_listDic setObject:[NSValue valueWithBytes:&_ru objCType:@encode(RoomUser)] forKey:[user objectAtIndex:1]];

        int insertIndex = 0;
        ...
        [_listKeys insertObject:_ru.p_id atIndex:insertIndex];

        onlineUserNum++;

    }else {

        [_listKeys removeObject:_ru.p_id];
        [_listDic removeObjectForKey:_ru.p_id];
        onlineUserNum--;
    }
}

//设置第一个标题的 内容
[(UIButton*)[titlesView viewWithTag:1] setTitle:[NSString stringWithFormat:@"%@ %d",[_titles objectAtIndex:0],onlineUserNum] forState:UIControlStateNormal];
user = nil;
[pool drain];     

}

1 个答案:

答案 0 :(得分:1)

您正在从NSString泄露componentsSeparatedByString:个对象,因为您复制了它们:[[user objectAtIndex:0] copy]

当从字典中删除装箱的RoomUser时,您必须确保正确释放NSString个实例。

更好的方法是不要携带C结构RoomUser,而是将数据放在字典或适当的Objective-C对象中。