memleak cleanup and fix crash

ulab-original
jsorg71 20 years ago
parent 66956f4624
commit bca2f57fe8

@ -70,14 +70,128 @@ static pthread_mutex_t g_term_mutex = PTHREAD_MUTEX_INITIALIZER;
static int g_term = 0; static int g_term = 0;
#endif #endif
#if defined(MEMLEAK)
#include "/home/j/cvs/xrdp/xrdp/xrdp.h"
#endif
#if defined(MEMLEAK) #if defined(MEMLEAK)
static int g_memsize = 0; static int g_memsize = 0;
static int g_memid = 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 #endif
/* forward declarations */ /* forward declarations */
@ -96,11 +210,7 @@ int g_init_system(void)
signal(SIGPIPE, g_pipe_sig); signal(SIGPIPE, g_pipe_sig);
#endif #endif
#if defined(MEMLEAK) #if defined(MEMLEAK)
g_memlist = xrdp_list_create(); g_memlist = 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));
#endif #endif
return 0; return 0;
} }
@ -114,15 +224,15 @@ int g_exit_system(void)
#endif #endif
#if defined(MEMLEAK) #if defined(MEMLEAK)
int i; int i;
struct xrdp_mem* p; struct mem* p;
for (i = 0; i < g_memlist->count; i++) 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("leak size %d id %d\n\r", p->size, p->id);
} }
g_printf("mem %d\n\r", g_memsize); g_printf("mem %d\n\r", g_memsize);
xrdp_list_delete(g_memlist); list_delete(g_memlist);
g_memlist = 0; g_memlist = 0;
#endif #endif
return 0; return 0;
@ -133,23 +243,23 @@ void* g_malloc(int size, int zero)
{ {
#if defined(MEMLEAK) #if defined(MEMLEAK)
char* rv; 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) if (zero)
{ {
memset(rv, 0, size + sizeof(struct xrdp_mem)); memset(rv, 0, size + sizeof(struct mem));
} }
g_memsize += size; g_memsize += size;
p = (struct xrdp_mem*)rv; p = (struct mem*)rv;
p->size = size; p->size = size;
p->id = g_memid; p->id = g_memid;
if (g_memlist != 0) if (g_memlist != 0)
{ {
xrdp_list_add_item(g_memlist, (int)p); list_add_item(g_memlist, (long)p);
} }
g_memid++; g_memid++;
return rv + sizeof(struct xrdp_mem); return rv + sizeof(struct mem);
#else #else
char* rv; char* rv;
@ -162,34 +272,21 @@ void* g_malloc(int size, int zero)
#endif #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) void g_free(void* ptr)
{ {
#if defined(MEMLEAK) #if defined(MEMLEAK)
struct xrdp_mem* p; struct mem* p;
int i; int i;
if (ptr != 0) 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; g_memsize -= p->size;
i = xrdp_list_index_of(g_memlist, (int)p); i = list_index_of(g_memlist, (long)p);
if (i >= 0) if (i >= 0)
{ {
xrdp_list_remove_item(g_memlist, i); list_remove_item(g_memlist, i);
} }
free(p); free(p);
} }
@ -201,15 +298,6 @@ void g_free(void* ptr)
#endif #endif
} }
/*****************************************************************************/
void g_free1(void* ptr)
{
if (ptr != 0)
{
free(ptr);
}
}
/*****************************************************************************/ /*****************************************************************************/
void g_printf(char* format, ...) void g_printf(char* format, ...)
{ {

@ -18,10 +18,6 @@
simple list 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" #include "xrdp.h"
@ -31,10 +27,10 @@ struct xrdp_list* xrdp_list_create(void)
{ {
struct xrdp_list* self; 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->grow_by = 10;
self->alloc_size = 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; return self;
} }
@ -55,8 +51,8 @@ void xrdp_list_delete(struct xrdp_list* self)
self->items[i] = 0; self->items[i] = 0;
} }
} }
g_free1(self->items); g_free(self->items);
g_free1(self); g_free(self);
} }
/*****************************************************************************/ /*****************************************************************************/
@ -69,9 +65,9 @@ void xrdp_list_add_item(struct xrdp_list* self, long item)
{ {
i = self->alloc_size; i = self->alloc_size;
self->alloc_size += self->grow_by; 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_memcpy(p, self->items, sizeof(long) * i);
g_free1(self->items); g_free(self->items);
self->items = p; self->items = p;
} }
self->items[self->count] = item; self->items[self->count] = item;
@ -101,11 +97,11 @@ void xrdp_list_clear(struct xrdp_list* self)
self->items[i] = 0; self->items[i] = 0;
} }
} }
g_free1(self->items); g_free(self->items);
self->count = 0; self->count = 0;
self->grow_by = 10; self->grow_by = 10;
self->alloc_size = 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; i = self->alloc_size;
self->alloc_size += self->grow_by; 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_memcpy(p, self->items, sizeof(long) * i);
g_free1(self->items); g_free(self->items);
self->items = p; self->items = p;
} }
for (i = (self->count - 2); i >= index; i--) for (i = (self->count - 2); i >= index; i--)

@ -89,8 +89,10 @@ int xrdp_wm_popup_notify(struct xrdp_bitmap* wnd,
int xrdp_wm_setup_mod(struct xrdp_wm* self, int xrdp_wm_setup_mod(struct xrdp_wm* self,
struct xrdp_mod_data* mod_data) struct xrdp_mod_data* mod_data)
{ {
if (self != 0) if (self == 0)
{ {
return 1;
}
if (self->mod_handle == 0) if (self->mod_handle == 0)
{ {
self->mod_handle = g_load_library(mod_data->lib); self->mod_handle = g_load_library(mod_data->lib);
@ -119,6 +121,11 @@ int xrdp_wm_setup_mod(struct xrdp_wm* self,
self->mod->server_is_term = server_is_term; 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; return 0;
} }

Loading…
Cancel
Save