If you're using Linux, just use valgrind. It will detect invalid uses of memory (eg accessing data that's been freed), and not only that, it will give you a stack dump at the point the freed block was illegally accessed,
and at the point where that block was originally freed (with the option to attach gdb).
It can do lots of other stuff too.
And the best thing about it is you don't have to do any different compiling or linking: valgrind will run against standard binaries, eg
$ valgrind perl myprog.pl
Dave.