Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations SkipVought on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

segfault in malloc_consolidate ??

Status
Not open for further replies.

zaichik

MIS
Aug 7, 2003
29
0
0
US
I have no idead where to go from here.

I have a line in a program that is generating a segfault.

The line that generates this error is trying to open a connection to mysql on the localhost. This connection has been successfully opened before; in fact, this is the second time through the loop, and the first execution of the loop executed flawlessly.

This exact error occurs every time there is a second iteration of the loop. I double-checked to make sure that the connection is closed before it is reopened. Here is output from gdb:
Code:
2018    if( !mysql_real_connect( &conn_temp, server, mysqlUser, mysqlPass, mysqlDB, 0, NULL, 0 )) {
(gdb)

Program received signal SIGSEGV, Segmentation fault.
0x00d21301 in malloc_consolidate () from /lib/tls/libc.so.6

(gdb) backtrace
#0  0x00d21301 in malloc_consolidate () from /lib/tls/libc.so.6
#1  0x00d20b29 in _int_malloc () from /lib/tls/libc.so.6
#2  0x00d1fecd in malloc () from /lib/tls/libc.so.6
#3  0x00eceb8a in my_malloc () from /usr/lib/libmysqlclient.so.12
#4  0x00ee0055 in my_net_init () from /usr/lib/libmysqlclient.so.12
#5  0x00ecb4db in mysql_real_connect () from /usr/lib/libmysqlclient.so.12
#6  0x0804e8e2 in scan (report=1) at ipman.c:2018
#7  0x08049cc9 in main (argc=2, argv=0xbfffc054) at ipman.c:292
(gdb)

I don't expect that anyone can solve this problem, but if you would at least throw some ideas out as to what might be causing it, that might help. I am fairly sure that I have closed and opened all mysql structures properly, but if you'd like to look as well, a second pair of eyes wouldn't hurt. I've posted the code at
In that code, all instances of target_mac[] will be empty strings, and ip_is_bound( ip ) will always return 0 (in my test case).

TIA!
 
Oh, I see.

Why do you need to create the connection in each iteration? Can't you do it just once outside the loop?

Cheers,
Dian
 
I see lots of free() calls, but what I don't see is any declarations, or any corresponding malloc() calls.

First things to check are that you actually allocated some memory to each of those pointers, and then that you actually allocated enough.

> I have a line in a program that is generating a segfault.
Where you crash is largely irrelevant, it just means your mistake happened in the past, and now you get to find out about it.

If you have it, try either "electric fence" or valgrind to help you find the cause of the problem (and not just the symptom).
Eg.
[tt]gcc -g prog.c -lefence[/tt]
If you run that in the debugger, you should be taken to the actual line of code which steps where it shouldn't.


--
 
Hi all,

Thanks for the ideas.

>Why do you need to create the connection in each iteration? Can't you do it just once outside the loop?

There is a chance on any given iteration that the connection would not be needed, and I did not want to connect unnecessarily.

>I see lots of free() calls, but what I don't see is any declarations, or any corresponding malloc() calls.

For brevity, I did not include the declarations or malloc() calls. Everything was declared, and everything was malloc'd with enough space.

What I did to resolve this was to rewrite the entire function the way I should have written it in the first place. Since the first iteration of the loop was always successful, I just broke that out into a separate function. That function was called a second and third time, and everything worked fine.

The code was simply too involved to debug properly. I must have closed a MySQL session and not reinitalized it somewhere before trying to connect again. A good demonstration of the importance of structured programming. :)

Thanks again for the ideas!
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top