Is it safe to catch an access violation in this scenario?

Posted by Eloff on Stack Overflow See other posts from Stack Overflow or by Eloff
Published on 2010-05-28T22:05:17Z Indexed on 2010/05/28 22:12 UTC
Read the original article Hit count: 299

Filed under:
|

I've read a lot, including here on SO that suggests this is a very bad idea in general and that the only thing you can do safely is exit the program. I'm not sure that this is true.

This is for a pooling memory allocator that hands off large allocations to malloc. During pool_free() a pointer needs to be checked it it belongs to a pool or was allocated with malloc. By rounding the address down to the nearest 1MB boundary, I get a pointer to the beginning of a block of memory in the pool, or undefined if malloc was used. In the first case I can easily verify that the block of memory belongs to the pool, but, if it does not I will either fail this verification, or I will get an access violation (note that this is a read-only process). Could I not catch this with SEH (Windows) or handle the signal (POSIX) and simply treat it as a failed verification? (i.e. this is only possible if malloc was used, so pass the ptr to free())

© Stack Overflow or respective owner

Related posts about c++

Related posts about undefined-behavior