tags:

views:

426

answers:

3

When i use mtrace in my c++ programme,i get output like the following

Memory not freed:

  Address           Size        Caller 
 0x0804a3c8         0x4 at     0x400b159f 

How do i know where in the code is 0x400b159f?

+1  A: 

You need to insert mtrace hooks into your program.

#include <mcheck.h>

Then call

mtrace();

Before you start allocating any memory.

Also compile with -g if using g++.

Xorlev
what arguments needs to be give to mcheck() ?
Ajay
No arguments are needed for mcheck(). :) You might set the MALLOC_TRACE environmental variable to a file for it to output to though.
Xorlev
you mean,we need to call mtrace() function from code,right? because mcheck() is a disk checking utility for MS-DOS formatted disks.
Ajay
>.> yes indeed. mtrace() is the function, I'm just terrible at transposing things. :)
Xorlev
+5  A: 

On many unix systems you can also use the addr2line utility to map an address back to a file name and line number. This utility requires that the code be compiled with the debug flag (-g for gcc). For a program named wombat you would use it like such:

addr2line -e wombat 0x400b159f

and it will print out something like

wombat_helper.c:1023

if you get ??:0 it can't find the function.

UPDATE: The memory addresses reported by mtrace are the locations where the malloc and free functions are called. For C++, this is almost always in the new and delete operators, and thus would be of very limited use, without other information, such as a stack trace to tell where in your program new or delete operator is called from.

diverscuba23
+1  A: 

You can load your program in gdb and use info symbol command

[root@localhost ~]#
[root@localhost ~]# mtrace ./a.out mtrace.log

Memory not freed:
-----------------
   Address     Size     Caller
0x08de3378      0x4  at 0x42028da
[root@localhost ~]#
[root@localhost ~]#
[root@localhost ~]#
[root@localhost ~]#
[root@localhost ~]# gdb ./a.out
GNU gdb (GDB) 7.2
Copyright (C) 2010 Free Software Foundation, Inc.
License GPLv3+: GNU GPL version 3 or later <http://gnu.org/licenses/gpl.html&gt;
This is free software: you are free to change and redistribute it.
There is NO WARRANTY, to the extent permitted by law.  Type "show copying"
and "show warranty" for details.
This GDB was configured as "i686-pc-linux-gnu".
For bug reporting instructions, please see:
<http://www.gnu.org/software/gdb/bugs/&gt;...
Reading symbols from /root/a.out...done.
(gdb) start
Temporary breakpoint 1 at 0x80484fd: file test.cpp, line 6.

Temporary breakpoint 1, main () at test.cpp:6
6               mtrace();
(gdb) info symbol 0x42028da
operator new(unsigned int) + 42 in section .text of /usr/lib/libstdc++.so.6
(gdb)

Also you may need to start your program until the beginning of the main procedure to load all symbols from shared libraries. In the example above the caller is operator new which is located in libstdc++.so.6.

ks1322
ahh, yes, forgot that about mtrace with c++, the locations that are reported are almost always located in either the new or the delete operator calls, and not anywhere usefull for figuring out where in your code the problem is.
diverscuba23