[mercury-users] Strange segfault (Mercury, C, C++, sockets, libraries...)

Ondrej Bojar oboj7042 at ss1000.ms.mff.cuni.cz
Fri Mar 14 23:41:35 AEDT 2003


Hi.

I've been hunting a segfault bug since last Friday and I still haven't
succeeded. Would you please help me?

The scenario is a bit complicated: I compile together a heap of Mercury
and C/C++ files, into a library of "core routines" for connecting to our
server of something. Then I use this library and compile a simple Mercury
code with it. This program should just connect to the server and read the
initial data.

The code therefore starts in Mercury, then (for an initialization) steps
to C (routine db_init) and back.
Then, the Mercury calls another C/C++ code and this connects to the
given server and port. (Standard socket communication.)
Still in this C/C++ code, an identification is sent to the socket and then
some data received. The program segfaults in a 'new' operator.

I expect, the heap is corrupt somewhere.

If I do not use Mercury at all, the C/C++ code appears to work fine. I.e.
writing the same main in C and calling both the parts of the C/C++ code
works correctly. I even tried this with the 'mpatrol' library and
exchanged a lot of messages with the server, but no memory error in the
C code appeared.
Unfortunately, I'm not able to link mpatrol to the Mercury code, I get
some strange routines missing:

...(other undefined references as well)
stabs.o(.text+0x3c7): undefined reference to `_sch_istable'
/usr/lib/gcc-lib/i386-redhat-linux/2.96/../../../libbfd.a(merge.o): In
function `merge_strings':
merge.o(.text+0x9f0): undefined reference to `htab_create'
merge.o(.text+0xa11): undefined reference to `htab_create'
merge.o(.text+0xac6): undefined reference to `htab_find_slot_with_hash'
merge.o(.text+0xb26): undefined reference to `htab_find_slot_with_hash'
merge.o(.text+0xb94): undefined reference to `htab_delete'
merge.o(.text+0xbaa): undefined reference to `htab_delete'
/usr/lib/gcc-lib/i386-redhat-linux/2.96/../../../libbfd.a(dwarf2.o): In
function `concat_filename':
dwarf2.o(.text+0x963): undefined reference to `concat'
collect2: ld returned 1 exit status

I attach the full gdb log till the segfault, but I do not see any problem
my code.

The only thing that surprises me is in main:

main -->
  get_options(option_ops(short_option, long_option, option_defaults),
Opts, _Args),
  (
  if { lookup_bool_option(Opts, help, yes) }
  then usage
  else
    { lookup_string_option(Opts, host, Host) },
    { lookup_int_option(Opts, port, Port) },
    db_mint__init(DB),
    io__write_string("Connecting to server at:
"++Host++":"++string__int_to_string(Port)++"\n"),

... the gdb reports this order of calls:

(gdb) 51        get_options(OptionOps, Opts, Args) -->
(gdb) main_2_p_0 () at chatter.m:68
68        if { lookup_bool_option(Opts, help, yes) }
(gdb) 69          then usage
(gdb) 68          if { lookup_bool_option(Opts, help, yes) }
(gdb) 71            { lookup_string_option(Opts, host, Host) },
(gdb) 68          if { lookup_bool_option(Opts, help, yes) }
(gdb) 71            { lookup_string_option(Opts, host, Host) },
(gdb) 72            { lookup_int_option(Opts, port, Port) },
(gdb) 71            { lookup_string_option(Opts, host, Host) },
(gdb) 72            { lookup_int_option(Opts, port, Port) },
(gdb) 73            db_mint__init(DB),
(gdb) 72            { lookup_int_option(Opts, port, Port) },
(gdb) 73            db_mint__init(DB),
(gdb) db_mint__init_3_p_0 (db_mint__Out_4=0xbffff460) at db_mint.m:73

...i.e. there and back again. However, the initialization in C itself is
called only once.

Thanks for any hints,

  Andrew.
-------------- next part --------------
A non-text attachment was scrubbed...
Name: log.out.gz
Type: application/x-gzip
Size: 7865 bytes
Desc: 
URL: <http://lists.mercurylang.org/archives/users/attachments/20030314/a80e3446/attachment.bin>


More information about the users mailing list