Trying to fix memory leak.

This commit is contained in:
Anony Moose 2013-08-26 20:16:09 +00:00
parent 60bf9991b4
commit 79f759049b
No known key found for this signature in database
GPG Key ID: A567AC285089C602
2 changed files with 8 additions and 17 deletions

View File

@ -161,19 +161,9 @@ static inline void tox_array_pop(tox_array *arr, uint32_t num)
arr->data = realloc(arr->data, arr->elem_size*arr->len);
}
/* TODO: do not use type, since we track the needed info in
* elem_size (but array user will have to cast)
*/
/* TODO: return ptr and do not take type */
#define tox_array_get(arr, i, type) (((type*)(arr)->data)[i])
/* This version requires C99 (declaring variables inside for loop)
#define tox_array_for_each(arr, type, tmp_name) \
for ( \
struct { type val; uint32_t i; } tmp_name = { tox_array_get(arr, 0, type), 0 }; \
tmp_name.i != (arr)->len; \
tmp_name.val = tox_array_get(arr, ++tmp_name.i, type) \
)
*/
#define tox_array_for_each(arr, type, tmp_name) \
type tmp_name; uint32_t tmp_name ## _i = 0; \

View File

@ -206,11 +206,13 @@ int incoming_connection(Lossless_UDP *ludp)
*/
int kill_connection(Lossless_UDP *ludp, int connection_id)
{
Connection* connection = &tox_array_get(&ludp->connections, connection_id, Connection);
if (connection_id >= 0 && connection_id < ludp->connections.len) {
if (tox_array_get(&ludp->connections, connection_id, Connection).status > 0) {
tox_array_get(&ludp->connections, connection_id, Connection).status = 0;
change_handshake(ludp, tox_array_get(&ludp->connections, connection_id, Connection).ip_port);
tox_array_pop(&ludp->connections, 0);
if (connection->status > 0) {
connection->status = 0;
change_handshake(ludp, connection->ip_port);
memset(connection, 0, sizeof(Connection));
return 0;
}
}
@ -320,9 +322,8 @@ int write_packet(Lossless_UDP *ludp, int connection_id, uint8_t *data, uint32_t
if (length > MAX_DATA_SIZE || length == 0)
return 0;
Connection *connection = &tox_array_get(&ludp->connections, connection_id, Connection);
if (sendqueue(ludp, connection_id) < BUFFER_PACKET_NUM) {
Connection *connection = &tox_array_get(&ludp->connections, connection_id, Connection);
uint32_t index = connection->sendbuff_packetnum % MAX_QUEUE_NUM;
memcpy(connection->sendbuffer[index].data, data, length);
connection->sendbuffer[index].size = length;