Skip to content

Commit

Permalink
fix memory leak
Browse files Browse the repository at this point in the history
  • Loading branch information
changchang committed Mar 20, 2013
1 parent f33dcaf commit 71bc1b9
Show file tree
Hide file tree
Showing 10 changed files with 55 additions and 20 deletions.
11 changes: 9 additions & 2 deletions example/destroy.c
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,11 @@ void on_request_cb(pc_request_t *req, int status, json_t *resp) {
if(status == -1) {
printf("Fail to send request to server.\n");
} else if(status == 0) {
printf("server response: %s\n", json_dumps(resp, 0));
char *json_str = json_dumps(resp, 0);
if(json_str != NULL) {
printf("server response: %s\n", json_str);
free(json_str);
}
}

// release relative resource with pc_request_t
Expand All @@ -24,7 +28,10 @@ void do_request(pc_client_t *client) {
// compose request
const char *route = "connector.helloHandler.hi";
json_t *msg = json_object();
json_object_set(msg, "msg", json_string("hi~"));
json_t *str = json_string("hi~");
json_object_set(msg, "msg", str);
// decref for json object
json_decref(str);

pc_request_t *request = pc_request_new();
pc_request(client, request, route, msg, on_request_cb);
Expand Down
5 changes: 4 additions & 1 deletion example/notify.c
Original file line number Diff line number Diff line change
Expand Up @@ -40,7 +40,10 @@ void do_notify(pc_client_t *client) {
// compose notify.
const char *route = "connector.helloHandler.hello";
json_t *msg = json_object();
json_object_set(msg, "msg", json_string("hello"));
json_t *json_str = json_string("hello");
json_object_set(msg, "msg", json_str);
// decref json string
json_decref(json_str);

pc_notify_t *notify = pc_notify_new();
pc_notify(client, notify, route, msg, on_notified);
Expand Down
11 changes: 9 additions & 2 deletions example/request.c
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,11 @@ void on_request_cb(pc_request_t *req, int status, json_t *resp) {
if(status == -1) {
printf("Fail to send request to server.\n");
} else if(status == 0) {
printf("server response: %s\n", json_dumps(resp, 0));
char *json_str = json_dumps(resp, 0);
if(json_str != NULL) {
printf("server response: %s\n", json_str);
free(json_str);
}
}

// release relative resource with pc_request_t
Expand All @@ -27,7 +31,10 @@ void do_request(pc_client_t *client) {
// compose request
const char *route = "connector.helloHandler.hi";
json_t *msg = json_object();
json_object_set(msg, "msg", json_string("hi~"));
json_t *str = json_string("hi~");
json_object_set(msg, "msg", str);
// decref for json object
json_decref(str);

pc_request_t *request = pc_request_new();
pc_request(client, request, route, msg, on_request_cb);
Expand Down
22 changes: 11 additions & 11 deletions src/client.c
Original file line number Diff line number Diff line change
Expand Up @@ -178,32 +178,32 @@ void pc_client_stop(pc_client_t *client) {
void pc_client_destroy(pc_client_t *client) {
if(PC_ST_INITED == client->state) {
pc__client_clear(client);
free(client);
return;
goto finally;
}

if(PC_ST_CLOSED == client->state) {
pc__client_clear(client);
free(client);
return;
goto finally;
}

// 1. asyn worker thread
// 2. wait cond until signal or timeout
// 2. wait work thread exit
// 3. free client
uv_async_send(client->close_async);

pc__cond_wait(client, 3);
pc_client_join(client);

if(PC_ST_CLOSED != client->state) {
pc_client_stop(client);
pc__client_clear(client);
// wait uv_loop_t stop
sleep(1);
if(client->uv_loop) {
free(client->uv_loop);
client->uv_loop = NULL;
}
pc__client_clear(client);
}

finally:
if(client->uv_loop) {
uv_loop_delete(client->uv_loop);
client->uv_loop = NULL;
}
free(client);
}
Expand Down
3 changes: 3 additions & 0 deletions src/map.c
Original file line number Diff line number Diff line change
Expand Up @@ -108,6 +108,7 @@ int pc_map_set(pc_map_t *map, const char *key, void *value) {

if(old_pair) {
map->release_value(map, old_pair->key, old_pair->value);
free((void *)old_pair->key);
free(old_pair);
}

Expand Down Expand Up @@ -147,6 +148,7 @@ void *pc_map_del(pc_map_t *map, const char *key) {
ngx_queue_remove(q);
ngx_queue_init(q);
value = pair->value;
free((void *)pair->key);
free(pair);
return value;
}
Expand All @@ -169,6 +171,7 @@ void pc_map_clear(pc_map_t *map) {
ngx_queue_remove(q);
ngx_queue_init(q);
map->release_value(map, pair->key, pair->value);
free((void *)pair->key);
free(pair);
}
}
Expand Down
1 change: 1 addition & 0 deletions src/message.c
Original file line number Diff line number Diff line change
Expand Up @@ -252,4 +252,5 @@ void pc__raw_msg_destroy(pc__msg_raw_t *msg) {
if(msg->body.len > 0) {
free(msg->body.base);
}
free(msg);
}
2 changes: 2 additions & 0 deletions src/network.c
Original file line number Diff line number Diff line change
Expand Up @@ -67,6 +67,7 @@ void pc_connect_req_destroy(pc_connect_t *req) {
free(req->address);
req->address = NULL;
}
free(req);
}

/**
Expand Down Expand Up @@ -129,6 +130,7 @@ int pc_connect(pc_client_t *client, pc_connect_t *req,
return 0;

error:
req->data = NULL;
if(data) free(data);
if(transport) pc_transport_destroy(transport);
if(connect_req) free(connect_req);
Expand Down
4 changes: 3 additions & 1 deletion src/pb-decode.c
Original file line number Diff line number Diff line change
Expand Up @@ -209,7 +209,9 @@ static bool checkreturn pb_decode_proto(pb_istream_t *stream, json_t *proto,
if (!pb_decode_string(stream, str_value, str_len)) {
return false;
}
json_object_set(result, key, json_string(str_value));
json_t *json_str = json_string(str_value);
json_object_set(result, key, json_str);
json_decref(json_str);
free(str_value);
break;
default:
Expand Down
13 changes: 10 additions & 3 deletions src/pkg-handshake.c
Original file line number Diff line number Diff line change
Expand Up @@ -27,9 +27,14 @@ int pc__handshake_req(pc_client_t *client) {
}

json_object_set(body, "sys", sys);
json_decref(sys);

json_object_set(sys, "type", json_string(PC_TYPE));
json_object_set(sys, "version", json_string(PC_VERSION));
json_t *json_type = json_string(PC_TYPE);
json_t *json_version = json_string(PC_VERSION);
json_object_set(sys, "type", json_type);
json_object_set(sys, "version", json_version);
json_decref(json_type);
json_decref(json_version);

if(handshake_opts) {
json_t *user = json_object_get(handshake_opts, "user");
Expand Down Expand Up @@ -108,7 +113,9 @@ int pc__handshake_resp(pc_client_t *client,
json_object_foreach(dict, key, value) {
memset(code_str, 0, 16);
sprintf(code_str, "%u", ((int)json_integer_value(value) & 0xff));
json_object_set(client->code_to_route, code_str, json_string(key));
json_t *json_key = json_string(key);
json_object_set(client->code_to_route, code_str, json_key);
json_decref(json_key);
}
}

Expand Down
3 changes: 3 additions & 0 deletions src/protocol.c
Original file line number Diff line number Diff line change
Expand Up @@ -158,6 +158,9 @@ pc_msg_t *pc__default_msg_parse_cb(pc_client_t *client, const char *data,
}
}

// release resources nolong used
pc__raw_msg_destroy(raw_msg);

return msg;

error:
Expand Down

0 comments on commit 71bc1b9

Please sign in to comment.