programs holds on malloc

R

Robert

Hi,

I don't know if this is the right newsgroup to ask this so if i'm wrong
please redirect me.

Here's my problem, i sometimes get that my program ( a server ) just stops
reacting, if
i use gdb and do a backtrace i get this:

#0 0x402331d1 in _int_malloc () from /lib/libc.so.6
#1 0x402320cf in malloc () from /lib/libc.so.6
#2 0x0804ab9d in mysql_getUserid ()
#3 0x0804a7b9 in protocol_checkSocket (socket=6, username=0x8052498
"blaat")
at protocol.c:344
#4 0x0804a281 in protocol_process (buf=0x804d2e0 "add", socket=6)
at protocol.c:224
#5 0x08049343 in network_process (buf=0x804d2e0 "add", socket=6)
at network.c:133
#6 0x080492ef in network_loop () at network.c:119
#7 0x08048f22 in main (argc=5, argv=0xbffffb44) at main.c:61
#8 0x401d490b in __libc_start_main () from /lib/libc.so.6

Looks to me like it stops working in malloc? I've included the
mysql_getUserid function soyou can tell me if somethings is wrong.

int mysql_getUserid(char * username)
{
MYSQL_RES * res_set;
MYSQL_ROW row;
char * sql;

if((sql=malloc(256*sizeof(char))) == NULL)
{
printf("Malloc failed at mysql_getUserid!\n");
return -1;
}
sprintf(sql, "SELECT userid FROM userlist WHERE username='%s'", username);
if(mysql_query (conn, sql) != 0)
{
free(sql);
return -1;
}
res_set = mysql_store_result(conn);
if(!mysql_num_rows(res_set) > 0) return -1;
row=mysql_fetch_row(res_set);
mysql_free_result(res_set);
free(sql);
return atoi(row[0]);
}

I don't know why it happens sometimes, most of the time the function just
works fine...
If anyone got a solution, please tell me...


Thanks in advance,

Robert Mens
 
E

Ed Morton

Robert said:
Hi,

I don't know if this is the right newsgroup to ask this so if i'm wrong
please redirect me.

Here's my problem, i sometimes get that my program ( a server ) just stops
reacting, if
if((sql=malloc(256*sizeof(char))) == NULL)
{
printf("Malloc failed at mysql_getUserid!\n");
return -1;
}
if(!mysql_num_rows(res_set) > 0) return -1;

There may be other problems too, but this looks like a memory leak - you
don't free sql before returning.

Ed.
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
473,774
Messages
2,569,599
Members
45,175
Latest member
Vinay Kumar_ Nevatia
Top