way to catch g_malloc failures
- From: Jared Davis <abiword aiksaurus com>
- To: gtk-list gnome org
- Subject: way to catch g_malloc failures
- Date: Sat, 29 Dec 2001 16:58:43 -0600
Hi,
I'm writing a simple thesaurus dialog that I would like to be able to have
people embed in their programs. Ideally their program should never crash or
otherwise unnaturally end because of my thesaurus. However, the g_malloc
function (which I assume is used by the GtkWidgets that I am creating)
allegedly aborts the program if memory is not available.
I can handle an out-of-memory condition fairly well in most cases. In
particular, I can destroy some widgets that are only displayed some of the
time, and then display a message hopefully using their memory. If that
fails, I can always just close the dialog and print a message to stdout in
case it's visible. I can also return an error to the program, so that it
knows it's in trouble. In any case, if it appears that the "thesaurus
crashed" without bringing the whole program down, that would be just great.
I've been looking at the mailing list archives and it seems like there
I'm wondering if there's a way to override this behavior? I'm using C++ so I
would ideally just like g_malloc to throw a std::bad_alloc if it runs into
trouble so that I can catch it.
It seems like I should be able to do something like this:
void throwBadAlloc() {
throw std::bad_alloc();
}
const char* MyDialog::runModal() throw()
{
GtkFunction* old_handler = g_malloc_get_outofmemory_handler();
g_malloc_set_outofmemory_handler( throwBadAlloc );
const char* ret = NULL;
try {
// run my dialog, modal...
ret = getSelection();
// blah blah blah...
}
catch(std::bad_alloc) {
// handle the out of memory condition
}
g_malloc_set_outofmemory_handler( old_handler );
return ret;
}
Is there any facility to do something like this, or any other way to catch
this problem and stop the abort from happening?
I understand that this is a rare possibility, but I really would prefer it
never happens to anyone.
Thanks,
Jared
--
Love all, serve all.
[
Date Prev][
Date Next] [
Thread Prev][
Thread Next]
[
Thread Index]
[
Date Index]
[
Author Index]