new will throw if it has a problem, but malloc and free don't throw to my knowlage. The std::vector .at() method will throw on a boundry violation. Without using vectors there is not much the language can do for you. Note that you can overwrite heap structures at any time. It's probably not going to be just before the next malloc. malloc(16)-1 might be a pointer that the heap uses as part of a linked list to track blocks when they are freed, but this can change often.
The thing is it's not just a matter of getting it to work, undefined behavior often works, you really need to go through and track down all your bounds violations. Wrapping your pointers in non-copyable(make the copy ctor private) classes is usually the cheapest solution. the boost libraries shared_ptr will track down most of the rest. Boost also provides a standard non_copyable class that tends to be cleaner that rolling your own.
Almost always this is a double free(). There are often some heap related structures that can cause problems if they are courrupted, but usually these are just before the pointer that malloc() returns.
Access Errors When Freeing Memory Anybody ever see this?
in Programming
Posted · Report reply
new will throw if it has a problem, but malloc and free don't throw to my knowlage. The std::vector .at() method will throw on a boundry violation. Without using vectors there is not much the language can do for you. Note that you can overwrite heap structures at any time. It's probably not going to be just before the next malloc. malloc(16)-1 might be a pointer that the heap uses as part of a linked list to track blocks when they are freed, but this can change often.
The thing is it's not just a matter of getting it to work, undefined behavior often works, you really need to go through and track down all your bounds violations. Wrapping your pointers in non-copyable(make the copy ctor private) classes is usually the cheapest solution. the boost libraries shared_ptr will track down most of the rest. Boost also provides a standard non_copyable class that tends to be cleaner that rolling your own.