Exception handling and heap corruption

R

Riley DeWiley

I have a program which must call into third party code that might do a wild
write (I have been getting ASSERTs and access violations in debug code when
I am freeing pointers after a call into that code).

Assuming I have no control over what the third party code does, [how] can I
use the try/except mechanism to trap heap corruption? How can I recover?

It is easy enough to do this:

try
{
foo();
}
except(...)
{
//handle problems ...
recover_foo();
};

but when I get to recover_foo()
- Is my heap already thrashed?
- If so, can I recover somehow?

RDeW
 
A

Alan Johnson

Riley said:
I have a program which must call into third party code that might do a wild
write (I have been getting ASSERTs and access violations in debug code when
I am freeing pointers after a call into that code).

Assuming I have no control over what the third party code does, [how] can I
use the try/except mechanism to trap heap corruption? How can I recover?

It is easy enough to do this:

try
{
foo();
}
except(...)
{
//handle problems ...
recover_foo();
};

but when I get to recover_foo()
- Is my heap already thrashed?
- If so, can I recover somehow?

RDeW

Unless the third party code is actually throwing exceptions when
something goes wrong, the try/catch mechanism isn't going to do anything
for you. Any method for repairing a corrupted heap is going to depend
highly on your platform. (C++ doesn't even require that an
implementation have any such concept.)

-Alan
 
J

Jim Langston

Alan Johnson said:
Riley said:
I have a program which must call into third party code that might do a
wild write (I have been getting ASSERTs and access violations in debug
code when I am freeing pointers after a call into that code).

Assuming I have no control over what the third party code does, [how] can
I use the try/except mechanism to trap heap corruption? How can I
recover?

It is easy enough to do this:

try
{
foo();
}
except(...)
{
//handle problems ...
recover_foo();
};

but when I get to recover_foo()
- Is my heap already thrashed?
- If so, can I recover somehow?

RDeW

Unless the third party code is actually throwing exceptions when something
goes wrong, the try/catch mechanism isn't going to do anything for you.
Any method for repairing a corrupted heap is going to depend highly on
your platform. (C++ doesn't even require that an implementation have any
such concept.)

-Alan

Only thing I could think of would be perhaps to place the library
call in it's own thread and run it. It is OS specific, but in windows
each thread gets it's own stack. So it would trash it's own stack
but not yours (hopefully). Of course then you'd have to implement
code to get get the actual return value, such as shared memory.
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
473,744
Messages
2,569,483
Members
44,903
Latest member
orderPeak8CBDGummies

Latest Threads

Top