socket.IO-objc
socket.IO-objc copied to clipboard
is it memory leak?
i'm testing so many 'event' send to server.
and there are so many increated memory usages.
i've checked where is it.
-
(void) sendEvent:(NSString *)eventName withData:(id)data andAcknowledge:(SocketIOCallback)function { NSMutableDictionary *dict = [NSMutableDictionary dictionaryWithObject:eventName forKey:@"name"];
// do not require arguments if (data != nil) { [dict setObject:[NSArray arrayWithObject:data] forKey:@"args"]; }
SocketIOPacket *packet = [[SocketIOPacket alloc] initWithType:@"event"]; packet.data = [SocketIOJSONSerialization JSONStringFromObject:dict error:nil]; packet.pId = [self addAcknowledge:function]; if (function) { packet.ack = @"data"; }
[self send:packet]; }
when i call JSON string convert function, "SocketIOJSONSerialization JSONStringFromObject:error:" memories are not freed after usage.
anybody experienced like me?
maybe i think, "JSONStringFromObject" function use memories.
when call 'JSONStringFromObject', memory has increased, but not freed.
i've found that what's the probrem is.
packet.data = [SocketIOJSONSerialization JSONStringFromObject:dict error:nil];
dataWithJSONObject function in JSONStringFromObject is autoreleased object.
so, this object is registered in Main RunLoop AutoreleasePool.
but until called drain, this is not released and cause increase memory.
so i've fixed code under blow.
-
(void) sendEvent:(NSString *)eventName withData:(id)data andAcknowledge:(SocketIOCallback)function { @autoreleasepool { NSMutableDictionary *dict = [NSMutableDictionary dictionaryWithObject:eventName forKey:@"name"];
// do not require arguments if (data != nil) { [dict setObject:[NSArray arrayWithObject:data] forKey:@"args"]; }
SocketIOPacket *packet = [[SocketIOPacket alloc] initWithType:@"event"]; packet.data = [SocketIOJSONSerialization JSONStringFromObject:dict error:nil]; packet.pId = [self addAcknowledge:function]; if (function) { packet.ack = @"data"; } [self send:packet];} }