2012-09-20 32 views
-1

资源创建者:componentsSeparatedByString内存泄漏,你能帮助我吗?

(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标记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];  

} 
+0

你使用哪种语言/技术?添加适当的标签以帮助人们找到您的问题并回答问题。 [Crash]和[Leak]是非语言特定的标签。 – Artemix

+0

objective-c 2.0 –

+1

它在Mac OS X或iOS下运行吗? – Artemix

回答

1

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

从盒式词典中删除盒装RoomUser时,必须确保NSString实例已正确释放。

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

+0

谢谢,用字典可以避免这个错误? –

+0

是的,使用字典将修复所含字符串对象的内存管理。 –