0

I would like a simple example on how to use the Leaks tool.
see:

#include <iostream>

int main (int argc, char * const argv[]) {
  char *buf = new char[100];

  sprintf(buf, "Hello, World!\n");
  // insert code here...
  std::cout << buf;
  return 0;
}

the code above(simple example) should leak the pointer allocated on *buf, right?
with valgrind I would find this very easy after a run
but I just can't find how to do this on the Leaks program
I tried to put sleep(60) but it is still not friendly to me...

thanks,
Jonathan

Jonathan
  • 4,724
  • 7
  • 45
  • 65
  • Ask something. Also, you have no leak in the code shown. – San Jacinto Nov 11 '10 at 18:35
  • He's saying because he doesn't free buf, it's a leak, but since the program terminates right after I don't know if that counts as a leak as the memory will be restored right after. Also i think it should be 'char* buf' not 'char buf' – robev Nov 11 '10 at 18:46
  • @robev good call. I missed the bad assignment to buf. My eyes read it as being declared on the stack. Still, he's not asking anything. – San Jacinto Nov 11 '10 at 19:10
  • I would like an explanation on how to find that leak on the Leaks tool. I just typed that code thinking you would understand what I meant... Surely it was supposed to be char *buf, my intent was to create a memoruy address and do not free it to see if Leaks would tell me about it like Valgrind would. Sorry my english... – Jonathan Nov 12 '10 at 18:36

1 Answers1

0

I've found that Leaks doesn't work all that well with command line applications that exit quickly. I would suggest adding a sleep() like you've done, but add one before the main program logic, and again at the end, so that Leaks is likely to sample the leaked state.

int main() {
  sleep(20); // 20s may be enough
  {
     // do leaky operations, then local variables will go out of scope
  }
  sleep(20);
}
the_mandrill
  • 29,792
  • 6
  • 64
  • 93
  • can you give me an example of a simple application that leaks something and you can see it on Leaks tool? – Jonathan Nov 12 '10 at 18:37
  • I haven't tried it for a while, but I was able to get some useful output out of it when having a delay at the start and end. You could also try attaching to the process (so you catch it during the first sleep period). Also try creating a mark at that point so that it shows deltas from that point. – the_mandrill Nov 15 '10 at 08:37