memleak cleanup and fix crash
This commit is contained in:
+133
-45
@@ -70,14 +70,128 @@ static pthread_mutex_t g_term_mutex = PTHREAD_MUTEX_INITIALIZER;
|
||||
static int g_term = 0;
|
||||
#endif
|
||||
|
||||
#if defined(MEMLEAK)
|
||||
#include "/home/j/cvs/xrdp/xrdp/xrdp.h"
|
||||
#endif
|
||||
|
||||
#if defined(MEMLEAK)
|
||||
static int g_memsize = 0;
|
||||
static int g_memid = 0;
|
||||
static struct xrdp_list* g_memlist = 0;
|
||||
static struct list* g_memlist = 0;
|
||||
|
||||
/* for memory debugging */
|
||||
struct mem
|
||||
{
|
||||
int size;
|
||||
int id;
|
||||
};
|
||||
|
||||
/* list */
|
||||
struct list
|
||||
{
|
||||
long* items;
|
||||
int count;
|
||||
int alloc_size;
|
||||
int grow_by;
|
||||
int auto_free;
|
||||
};
|
||||
|
||||
/*****************************************************************************/
|
||||
struct list* list_create(void)
|
||||
{
|
||||
struct list* self;
|
||||
|
||||
self = (struct list*)malloc(sizeof(struct list));
|
||||
memset(self, 0, sizeof(struct list));
|
||||
self->grow_by = 10;
|
||||
self->alloc_size = 10;
|
||||
self->items = (long*)malloc(sizeof(long) * 10);
|
||||
memset(self->items, 0, sizeof(long) * 10);
|
||||
return self;
|
||||
}
|
||||
|
||||
/*****************************************************************************/
|
||||
void list_delete(struct list* self)
|
||||
{
|
||||
int i;
|
||||
|
||||
if (self == 0)
|
||||
{
|
||||
return;
|
||||
}
|
||||
if (self->auto_free)
|
||||
{
|
||||
for (i = 0; i < self->count; i++)
|
||||
{
|
||||
free((void*)self->items[i]);
|
||||
self->items[i] = 0;
|
||||
}
|
||||
}
|
||||
free(self->items);
|
||||
free(self);
|
||||
}
|
||||
|
||||
/*****************************************************************************/
|
||||
void list_add_item(struct list* self, long item)
|
||||
{
|
||||
long* p;
|
||||
int i;
|
||||
|
||||
if (self->count >= self->alloc_size)
|
||||
{
|
||||
i = self->alloc_size;
|
||||
self->alloc_size += self->grow_by;
|
||||
p = (long*)malloc(sizeof(long) * self->alloc_size);
|
||||
memset(p, 0, sizeof(long) * self->alloc_size);
|
||||
memcpy(p, self->items, sizeof(long) * i);
|
||||
free(self->items);
|
||||
self->items = p;
|
||||
}
|
||||
self->items[self->count] = item;
|
||||
self->count++;
|
||||
}
|
||||
|
||||
/*****************************************************************************/
|
||||
int list_index_of(struct list* self, long item)
|
||||
{
|
||||
int i;
|
||||
|
||||
for (i = 0; i < self->count; i++)
|
||||
{
|
||||
if (self->items[i] == item)
|
||||
{
|
||||
return i;
|
||||
}
|
||||
}
|
||||
return -1;
|
||||
}
|
||||
|
||||
/*****************************************************************************/
|
||||
void list_remove_item(struct list* self, int index)
|
||||
{
|
||||
int i;
|
||||
|
||||
if (index >= 0 && index < self->count)
|
||||
{
|
||||
if (self->auto_free)
|
||||
{
|
||||
free((void*)self->items[index]);
|
||||
self->items[index] = 0;
|
||||
}
|
||||
for (i = index; i < (self->count - 1); i++)
|
||||
{
|
||||
self->items[i] = self->items[i + 1];
|
||||
}
|
||||
self->count--;
|
||||
}
|
||||
}
|
||||
|
||||
/*****************************************************************************/
|
||||
long list_get_item(struct list* self, int index)
|
||||
{
|
||||
if (index < 0 || index >= self->count)
|
||||
{
|
||||
return 0;
|
||||
}
|
||||
return self->items[index];
|
||||
}
|
||||
|
||||
#endif
|
||||
|
||||
/* forward declarations */
|
||||
@@ -96,11 +210,7 @@ int g_init_system(void)
|
||||
signal(SIGPIPE, g_pipe_sig);
|
||||
#endif
|
||||
#if defined(MEMLEAK)
|
||||
g_memlist = xrdp_list_create();
|
||||
g_printf("some info\n\r");
|
||||
g_printf("sizeof xrdp_bitmap is %d\n\r", sizeof(struct xrdp_bitmap));
|
||||
g_printf("sizeof xrdp_wm is %d\n\r", sizeof(struct xrdp_wm));
|
||||
g_printf("sizeof stream is %d\n\r", sizeof(struct stream));
|
||||
g_memlist = list_create();
|
||||
#endif
|
||||
return 0;
|
||||
}
|
||||
@@ -114,15 +224,15 @@ int g_exit_system(void)
|
||||
#endif
|
||||
#if defined(MEMLEAK)
|
||||
int i;
|
||||
struct xrdp_mem* p;
|
||||
struct mem* p;
|
||||
|
||||
for (i = 0; i < g_memlist->count; i++)
|
||||
{
|
||||
p = (struct xrdp_mem*)xrdp_list_get_item(g_memlist, i);
|
||||
p = (struct mem*)list_get_item(g_memlist, i);
|
||||
g_printf("leak size %d id %d\n\r", p->size, p->id);
|
||||
}
|
||||
g_printf("mem %d\n\r", g_memsize);
|
||||
xrdp_list_delete(g_memlist);
|
||||
list_delete(g_memlist);
|
||||
g_memlist = 0;
|
||||
#endif
|
||||
return 0;
|
||||
@@ -133,23 +243,23 @@ void* g_malloc(int size, int zero)
|
||||
{
|
||||
#if defined(MEMLEAK)
|
||||
char* rv;
|
||||
struct xrdp_mem* p;
|
||||
struct mem* p;
|
||||
|
||||
rv = (char*)malloc(size + sizeof(struct xrdp_mem));
|
||||
rv = (char*)malloc(size + sizeof(struct mem));
|
||||
if (zero)
|
||||
{
|
||||
memset(rv, 0, size + sizeof(struct xrdp_mem));
|
||||
memset(rv, 0, size + sizeof(struct mem));
|
||||
}
|
||||
g_memsize += size;
|
||||
p = (struct xrdp_mem*)rv;
|
||||
p = (struct mem*)rv;
|
||||
p->size = size;
|
||||
p->id = g_memid;
|
||||
if (g_memlist != 0)
|
||||
{
|
||||
xrdp_list_add_item(g_memlist, (int)p);
|
||||
list_add_item(g_memlist, (long)p);
|
||||
}
|
||||
g_memid++;
|
||||
return rv + sizeof(struct xrdp_mem);
|
||||
return rv + sizeof(struct mem);
|
||||
#else
|
||||
char* rv;
|
||||
|
||||
@@ -162,34 +272,21 @@ void* g_malloc(int size, int zero)
|
||||
#endif
|
||||
}
|
||||
|
||||
/*****************************************************************************/
|
||||
void* g_malloc1(int size, int zero)
|
||||
{
|
||||
char* rv;
|
||||
|
||||
rv = (char*)malloc(size);
|
||||
if (zero)
|
||||
{
|
||||
memset(rv, 0, size);
|
||||
}
|
||||
return rv;
|
||||
}
|
||||
|
||||
/*****************************************************************************/
|
||||
void g_free(void* ptr)
|
||||
{
|
||||
#if defined(MEMLEAK)
|
||||
struct xrdp_mem* p;
|
||||
struct mem* p;
|
||||
int i;
|
||||
|
||||
if (ptr != 0)
|
||||
{
|
||||
p = (struct xrdp_mem*)(((char*)ptr) - sizeof(struct xrdp_mem));
|
||||
p = (struct mem*)(((char*)ptr) - sizeof(struct mem));
|
||||
g_memsize -= p->size;
|
||||
i = xrdp_list_index_of(g_memlist, (int)p);
|
||||
i = list_index_of(g_memlist, (long)p);
|
||||
if (i >= 0)
|
||||
{
|
||||
xrdp_list_remove_item(g_memlist, i);
|
||||
list_remove_item(g_memlist, i);
|
||||
}
|
||||
free(p);
|
||||
}
|
||||
@@ -201,15 +298,6 @@ void g_free(void* ptr)
|
||||
#endif
|
||||
}
|
||||
|
||||
/*****************************************************************************/
|
||||
void g_free1(void* ptr)
|
||||
{
|
||||
if (ptr != 0)
|
||||
{
|
||||
free(ptr);
|
||||
}
|
||||
}
|
||||
|
||||
/*****************************************************************************/
|
||||
void g_printf(char* format, ...)
|
||||
{
|
||||
|
||||
+10
-14
@@ -18,10 +18,6 @@
|
||||
|
||||
simple list
|
||||
|
||||
this list is used to track mem leaks so g_malloc1 and
|
||||
g_free1 should be used for internal allocs but not
|
||||
for auto_free items
|
||||
|
||||
*/
|
||||
|
||||
#include "xrdp.h"
|
||||
@@ -31,10 +27,10 @@ struct xrdp_list* xrdp_list_create(void)
|
||||
{
|
||||
struct xrdp_list* self;
|
||||
|
||||
self = (struct xrdp_list*)g_malloc1(sizeof(struct xrdp_list), 1);
|
||||
self = (struct xrdp_list*)g_malloc(sizeof(struct xrdp_list), 1);
|
||||
self->grow_by = 10;
|
||||
self->alloc_size = 10;
|
||||
self->items = (long*)g_malloc1(sizeof(long) * 10, 1);
|
||||
self->items = (long*)g_malloc(sizeof(long) * 10, 1);
|
||||
return self;
|
||||
}
|
||||
|
||||
@@ -55,8 +51,8 @@ void xrdp_list_delete(struct xrdp_list* self)
|
||||
self->items[i] = 0;
|
||||
}
|
||||
}
|
||||
g_free1(self->items);
|
||||
g_free1(self);
|
||||
g_free(self->items);
|
||||
g_free(self);
|
||||
}
|
||||
|
||||
/*****************************************************************************/
|
||||
@@ -69,9 +65,9 @@ void xrdp_list_add_item(struct xrdp_list* self, long item)
|
||||
{
|
||||
i = self->alloc_size;
|
||||
self->alloc_size += self->grow_by;
|
||||
p = (long*)g_malloc1(sizeof(long) * self->alloc_size, 1);
|
||||
p = (long*)g_malloc(sizeof(long) * self->alloc_size, 1);
|
||||
g_memcpy(p, self->items, sizeof(long) * i);
|
||||
g_free1(self->items);
|
||||
g_free(self->items);
|
||||
self->items = p;
|
||||
}
|
||||
self->items[self->count] = item;
|
||||
@@ -101,11 +97,11 @@ void xrdp_list_clear(struct xrdp_list* self)
|
||||
self->items[i] = 0;
|
||||
}
|
||||
}
|
||||
g_free1(self->items);
|
||||
g_free(self->items);
|
||||
self->count = 0;
|
||||
self->grow_by = 10;
|
||||
self->alloc_size = 10;
|
||||
self->items = (long*)g_malloc1(sizeof(long) * 10, 1);
|
||||
self->items = (long*)g_malloc(sizeof(long) * 10, 1);
|
||||
}
|
||||
|
||||
/*****************************************************************************/
|
||||
@@ -161,9 +157,9 @@ void xrdp_list_insert_item(struct xrdp_list* self, int index, long item)
|
||||
{
|
||||
i = self->alloc_size;
|
||||
self->alloc_size += self->grow_by;
|
||||
p = (long*)g_malloc1(sizeof(long) * self->alloc_size, 1);
|
||||
p = (long*)g_malloc(sizeof(long) * self->alloc_size, 1);
|
||||
g_memcpy(p, self->items, sizeof(long) * i);
|
||||
g_free1(self->items);
|
||||
g_free(self->items);
|
||||
self->items = p;
|
||||
}
|
||||
for (i = (self->count - 2); i >= index; i--)
|
||||
|
||||
+32
-25
@@ -89,36 +89,43 @@ int xrdp_wm_popup_notify(struct xrdp_bitmap* wnd,
|
||||
int xrdp_wm_setup_mod(struct xrdp_wm* self,
|
||||
struct xrdp_mod_data* mod_data)
|
||||
{
|
||||
if (self != 0)
|
||||
if (self == 0)
|
||||
{
|
||||
if (self->mod_handle == 0)
|
||||
return 1;
|
||||
}
|
||||
if (self->mod_handle == 0)
|
||||
{
|
||||
self->mod_handle = g_load_library(mod_data->lib);
|
||||
if (self->mod_handle != 0)
|
||||
{
|
||||
self->mod_handle = g_load_library(mod_data->lib);
|
||||
if (self->mod_handle != 0)
|
||||
self->mod_init = (struct xrdp_mod* (*)(void))
|
||||
g_get_proc_address(self->mod_handle, "mod_init");
|
||||
self->mod_exit = (int (*)(struct xrdp_mod*))
|
||||
g_get_proc_address(self->mod_handle, "mod_exit");
|
||||
if (self->mod_init != 0 && self->mod_exit != 0)
|
||||
{
|
||||
self->mod_init = (struct xrdp_mod* (*)(void))
|
||||
g_get_proc_address(self->mod_handle, "mod_init");
|
||||
self->mod_exit = (int (*)(struct xrdp_mod*))
|
||||
g_get_proc_address(self->mod_handle, "mod_exit");
|
||||
if (self->mod_init != 0 && self->mod_exit != 0)
|
||||
{
|
||||
self->mod = self->mod_init();
|
||||
}
|
||||
}
|
||||
if (self->mod != 0)
|
||||
{
|
||||
self->mod->wm = (long)self;
|
||||
self->mod->server_begin_update = server_begin_update;
|
||||
self->mod->server_end_update = server_end_update;
|
||||
self->mod->server_fill_rect = server_fill_rect;
|
||||
self->mod->server_screen_blt = server_screen_blt;
|
||||
self->mod->server_paint_rect = server_paint_rect;
|
||||
self->mod->server_set_pointer = server_set_pointer;
|
||||
self->mod->server_palette = server_palette;
|
||||
self->mod->server_msg = server_msg;
|
||||
self->mod->server_is_term = server_is_term;
|
||||
self->mod = self->mod_init();
|
||||
}
|
||||
}
|
||||
if (self->mod != 0)
|
||||
{
|
||||
self->mod->wm = (long)self;
|
||||
self->mod->server_begin_update = server_begin_update;
|
||||
self->mod->server_end_update = server_end_update;
|
||||
self->mod->server_fill_rect = server_fill_rect;
|
||||
self->mod->server_screen_blt = server_screen_blt;
|
||||
self->mod->server_paint_rect = server_paint_rect;
|
||||
self->mod->server_set_pointer = server_set_pointer;
|
||||
self->mod->server_palette = server_palette;
|
||||
self->mod->server_msg = server_msg;
|
||||
self->mod->server_is_term = server_is_term;
|
||||
}
|
||||
}
|
||||
/* id self->mod is null, there must be a problem */
|
||||
if (self->mod == 0)
|
||||
{
|
||||
DEBUG(("problem loading lib in xrdp_wm_setup_mod"));
|
||||
return 1;
|
||||
}
|
||||
return 0;
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user