Skip to content
This repository has been archived by the owner on May 14, 2024. It is now read-only.

is it memory leak? #200

Open
breaklee opened this issue Jul 14, 2014 · 2 comments
Open

is it memory leak? #200

breaklee opened this issue Jul 14, 2014 · 2 comments

Comments

@breaklee
Copy link

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?

@breaklee
Copy link
Author

maybe i think, "JSONStringFromObject" function use memories.

when call 'JSONStringFromObject', memory has increased, but not freed.

@breaklee
Copy link
Author

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

    }
    }

Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Labels
None yet
Projects
None yet
Development

No branches or pull requests

1 participant