kevin lyda wrote something:
{ i had a question here but i seem to have answered it, so i thought i'd
{ share...
He's back ;-) Wheres me t-shirt ?/??
{
{ memory leaks are really annoying. and they're also tough to spot.
{ glibc provides a method to spot them:
{
{ #include <mcheck.h>
{
{ and then call mtrace() at the start of main(). compile the program with
{ -g for debugging, and set the environment variable MALLOC_TRACE to a
{ file to log memory allocation and freeing. once you've run the program
{ run this:
{
{ mtrace <program> $MALLOC_TRACE
{
follow up:
I've tried this before with the following prog:
--BEGIN
#include <mcheck.h>
#include <stdio.h>
#include <stdlib.h>
int main(int argc, char *argv[])
{
int i;
char *buff;
mtrace(); /* for mtrace */
for (i = 0; i < 10; i++) buff = malloc(i * i * sizeof(char));
free(buff);
return 0;
}
--END
(stupid I know, but an example)
mtrace does:
Memory not freed:
-----------------
Address Size Caller
0x08049690 0x1 at /home/sean/c/mem.c:13
0x080496a0 0x2 at /home/sean/c/mem.c:13
0x080496b0 0x3 at /home/sean/c/mem.c:13
0x080496c0 0x4 at /home/sean/c/mem.c:13
0x080496d0 0x5 at /home/sean/c/mem.c:13
0x080496e0 0x6 at /home/sean/c/mem.c:13
0x080496f0 0x7 at /home/sean/c/mem.c:13
0x08049700 0x8 at /home/sean/c/mem.c:13
0x08049710 0x9 at /home/sean/c/mem.c:13
Electric fence brings an error up:
ElectricFence Aborting: Allocating 0 bytes, probably a bug.
{ voila, it lists all the memory leaks and the lines where the memory was
{ allocated.
Not bad. This together with other tools could lead to entensive debugging...
Still not totally an excuse for remembering the [m|c]allocs ones does
while programming, but a help. Its better than most.
{
{ dunno if this works from c++, but then c++ programs are supposed to leak
{ like a sieve anyway so does it really matter?
Not if the compiler is ok and the program is programed correctly.
C++ is just about mature enough not to blame the tools. Some other OO
languages...
{
{ kevin
{ --
{ kevin at suberic.net "we were goin' for breakfast. in canada. we
{ fork()'ed on 37058400 made a deal: if she'd stop hookin', i'd stop
{ meatspace place: home shootin' people. maybe we were aiming high."
{ --porter, "payback"
{
Sean.
.
Maintained by the ILUG website team. The aim of Linux.ie is to
support and help commercial and private users of Linux in Ireland. You can
display ILUG news in your own webpages, read backend
information to find out how. Networking services kindly provided by HEAnet, server kindly donated by
Dell. Linux is a trademark of Linus Torvalds,
used with permission. No penguins were harmed in the production or maintenance
of this highly praised website. Looking for the
Indian Linux Users' Group? Try here. If you've read all this and aren't a lawyer: you should be!