Sign in to follow this  
Aliii

C++ debug tools

Recommended Posts

Aliii    1456
Are there any tools for detecting index out of bound bugs at runtime? I heard about Electric Fence but I need one for Win. Thanks!

Share this post


Link to post
Share on other sites
Adam_42    3629
The simplest option is to enable [url="http://msdn.microsoft.com/en-us/library/windows/hardware/ff549557%28v=vs.85%29.aspx"]Page Heap Mode[/url] but it does require plenty of spare address space (link with /LARGEADDRESSAWARE if it's a 32-bit program to double available address space when run on 64-bit Windows).

You could also try http://www.drmemory.org/ but it's not so easy to use.

Share this post


Link to post
Share on other sites
Aliii    1456
[quote name='Adam_42' timestamp='1346510723' post='4975438']
The simplest option is to enable [url="http://msdn.microsoft.com/en-us/library/windows/hardware/ff549557%28v=vs.85%29.aspx"]Page Heap Mode[/url] but it does require plenty of spare address space (link with /LARGEADDRESSAWARE if it's a 32-bit program to double available address space when run on 64-bit Windows).

You could also try [url="http://www.drmemory.org/"]http://www.drmemory.org/[/url] but it's not so easy to use.
[/quote]

Do these work when the "out of buond" is inside the programs memory?

Share this post


Link to post
Share on other sites
Hodgman    51234
What kind of allocation is being indexed? Is it created with malloc or new? Is it a member of a class with members on either side? Is it a container class that you've got the code for? Is it a standard container like a std::vector?

For structures that you allocate with malloc/new, most games I've worked on have had a [font=courier new,courier,monospace]#define[/font] that you can enable, which pads every new/malloc call with a begin/end buffer, so that you can watch the buffer areas for corruption. I guess your project doesn't have an allocator with debugging features like this?


There's some good tips in this blog post, including the above mentioned Page Heap mode:
[url="http://randomascii.wordpress.com/2011/12/07/increased-reliability-through-more-crashes/"]http://randomascii.w...h-more-crashes/[/url]
[quote]Page Heap puts each allocation on its own 4-KB page, with the allocated memory aligned to the end of the page. Therefore if you overrun the buffer you will touch the next page. Page Heap ensures that the next page will be unmapped memory so you get a guaranteed access violation at the exact moment that you overrun the buffer.[/quote] Edited by Hodgman

Share this post


Link to post
Share on other sites
Ripiz    539
If I array size is static you should be able to replace it with std::array with no code modifications; it'll throw exception right away if you go out of bounds.
If it's dynamic then you can use std::vector, but that requires a bit code modification. Edited by Ripiz

Share this post


Link to post
Share on other sites
Aliii    1456
Thanks! Im allocating with new. I have at least 100 classes in this project(and many many allocations)so I dont know where is it coming from, ...the program doesnt crash, no segmentation fault or anything, ....it overwrites some of the object geometry. But I cant reproduce this "error" in debug mode. On an other forum they said thats because in that mode the heap is allocated differently or something.

Share this post


Link to post
Share on other sites
the_edd    2109
Is it always the same piece of object geometry that gets overwritten?

If so, assuming you're using Visual Studio, you can [url="http://msdn.microsoft.com/en-us/library/aa295838%28v=VS.60%29.aspx#_core_setting_a_breakpoint_when_the_value_at_a_specified_memory_address_changes"]set a break point that is triggered when a particular piece of memory is modified[/url]. The call stack at that breakpoint will give you the culprit.

A better way, imho, is to start adding more assertions and/or tests.

Share this post


Link to post
Share on other sites

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now

Sign in to follow this