开发者

Is there a command I can call to print out the malloc data structures?

开发者 https://www.devze.com 2023-01-29 21:09 出处:网络
Hi I wonder if there is any ready made function that I can call to print all the malloc data structures, so that I can see which memory is allocated for which variable?

Hi I wonder if there is any ready made function that I can call to print all the malloc data structures, so that I can see which memory is allocated for which variable?

I have this memory corruption, that when I free开发者_开发问答 one variable it complains, but I have no idea which variable is adjacent to it.

Thanks!


Try running your program under valgrind. If you're lucky it'll point you right at the offending out-of-bounds memory write. (If you're unlucky you'll just get a flood of spurious complaints about code deep in the C library.)


I think this would be extremely difficult to figure out the way you describe it, since there is nothing in the memory that points back to the owning variables.

You could in theory traverse the entire tree of all objects your application created, until you find a pointer that points to memory next to the spot that causes trouble.

You could use a tool like gdb to dump the chunk of memory around your allocation and see for yourself. Maybe you will recognize the data by looking at them and that would point you to the code that causes trouble.

Your best bet would be to use a tool like electricfence that would kill your application instantly when your code attempts to corrupt the memory.


I don't know of any function you can call from within your program but you can do this with a debugger. Try https://github.com/cloudburst/libheap or under Win32 in windbg use the '!heap' command.


Our CheckPointer tool can likely find the exact place where you corrupt the memory.

Most memory checking tools have some kind of "fence" around your data to detect a bad access. Such fences have granularitly considerably larger than the object being fenced; if you access outside the object but inside the fence, the error isn't detected. For instance, Valgrind has no clue about stack frames, so it can't detect an access to a stack frame which has gone out of scope and been overwritten by another. CheckPointer can.

CheckPointer tracks the exact allocation of each block of store (heap, stack, part-of-struct) and each access. It knows exactly when you've stepped outside the exact space set aside for the storage entity (e.g., if you reach off the end of an array embedded in the middle of a struct). It can thus provide much better checking.

CheckPointer will also provide a post-execution dump of all still-allocated storage; of course, you could call that dump procedure at an arbitrary place in your code as a debugging aid.

0

精彩评论

暂无评论...
验证码 换一张
取 消