0

Im developing a game using SFML+Box2D and got a heap corruption when I delete a Controller object, it triggers a breakpoint on

retval = HeapFree(_crtheap, 0, pBlock);
    if (retval == 0)
    {
        errno = _get_errno_from_oserr(GetLastError());
    }

On debug it says its a heap corruption and tells me "Unhandled exception at 0x771CE753 (ntdll.dll) in Alumni.exe: 0xC0000374: A heap has been corrupted (parameters: 0x77204270)."

How do I know what has been previously written on this memory adress? What are good pratices to avoid heap corruption? I was careful with my pointers, Im not double deleting anything.

If anyone wanna take the time, the repository is at github.com/mrseth/Alumni. You can trigger the crash pressing enter once and backspace. Enter spawns a Controller, backspace deletes the last spawned one. The code that deletes the controller is at https://github.com/mrseth/Alumni/blob/Stephen/Alumni/Alumni/TestClickListener.cpp

Stephen Lynx
  • 1,077
  • 2
  • 14
  • 29
  • 1
    It's not just double-deletion. A good way to corrupt the heap is a buffer overrun that trashes other memory. – paddy Nov 28 '13 at 22:14
  • 1
    use memory checking tools, valgrind is a good choice – Ulterior Nov 28 '13 at 22:18
  • Amazing code, **everything** is a pointer, even the vectors are pointers. Not surprised there are problems, you should try to wean yourself off pointers. Had a quick browse, saw one problem, in FocusGroup currentFocused and focusables are uninitalised pointers. – john Nov 28 '13 at 22:53
  • Since everything is a pointer, your classes cannot be safely copied. Didn't see any instances where you are actually doing that, but to be safe you should disable copying of your objects. Declaring the copy ctor and assignment op private is a simple way to do that. – john Nov 28 '13 at 22:55
  • I had problems using heap, stuff was turning null, so I made everything a pointer so I could just control and mannually deallocate, my memory management background is obj-c. What kind of variables you advise for me to use? When I should use what? – Stephen Lynx Nov 28 '13 at 22:56
  • 1
    @CrimsonTulip I guess you should read up on the rule of three, http://stackoverflow.com/questions/4172722/what-is-the-rule-of-three. If you really need a pointer (i.e. you actually need the semantics of a pointer, it's not just that you feel better able to manage a pointer) then you should look into smart pointers. These will manage deallocation for you. The obvious choice for you would be `std::shared_ptr`, which is essentially a reference counted pointer. But in general the C++ way is to avoid pointers. – john Nov 28 '13 at 23:03
  • Instead I should use the heap? Like in ClassType variable = ClassType(); – Stephen Lynx Nov 28 '13 at 23:05
  • Yes, but that's the stack not the heap. When you write `new ClassType()` then you are using the heap. Of course you need heap allocation often, but you can get heap allocation without using raw pointers. – john Nov 28 '13 at 23:05
  • Need to get the definition heap and stack straight, otherwise you are going to be very confused. – john Nov 28 '13 at 23:08

1 Answers1

1

I found the problem. I was calling delete on the SceneNodes from within SceneNode, so it called delete on a SceneNode pointer. The problem is, it could delete its children nodes, wich could be subclasses of SceneNode. So I changed so every subclass its responsible for deallocating itself and it solved the heap overrun. Thanks for the insights and information.

Stephen Lynx
  • 1,077
  • 2
  • 14
  • 29