[vtkusers] Catching VTK memory errors in Java

Steve Joyce Steve.Joyce at sercoassurance.com
Thu Apr 1 05:08:58 EST 2004


Hi,

I have been trying to come up with ways of catching out of memory
errors and other unhandled exceptions that occur in VTK from within
Java. Normally these errors cause the Java virtual machine to crash. I
would rather just warn the user that there is not sufficient memory to
carry out the operation they are attempting.
Following advice in this mailing list I attempted to catch the out of
memory errors by using the set_new_handler function to provide my own
handler for these errors that raises a Java exception. However, I can't
find any way of doing this that doesn't cause Java to crash. Does anyone
have any ideas on how to do this. Below is the test C++ code I am
using.
The handler seems to get called and runs through the code that throws
the Java exception, but I don't see any exception in Java. Then the
handler seems to get called again, doesn't execute the code that throws
the Java exception and just throws the bad_alloc. If I don't put in the
throw of bad_alloc the handler is just called indefinitely.
Is the only way I can handle this is to put catches for bad_alloc
around any  VTK C++ code that I think might throw an out of memory
error?

Thanks,
Steve

#include "MemTest.h"
#include <stdio.h>
#include <stdlib.h>
#include <new>
#include <iostream>
using namespace std;

extern "C" void newhandler();
extern "C" void unexpectedhandler();
JNIEnv *genv;


extern "C" JNIEXPORT void JNICALL
Java_com_sercoassurance_geovisage_jni_MemTest_allocateDoubles 
  (JNIEnv * env, jobject object, jint count, jint size)
{
	genv = env;
	set_new_handler(newhandler);
	fprintf(stderr, "Allocating %ld\n", count * size *
sizeof(double));
	void* mem;
	for (int i = 0; i < count; ++i) {
		mem = new double[size];
	}
    fprintf(stderr, "Allocated %ld\n", count * size * sizeof(double));
    return;
}

/* A handler for out of memory errors */
extern "C" void newhandler()
{
    fprintf(stderr, "Caught memory error\n");
	jclass newExcCls;
	if (genv != NULL) {
		fprintf(stderr, "Trying to throw java out of memory
error\n");
		newExcCls =
(genv)->FindClass("java/lang/OutOfMemoryError");
		if (newExcCls != 0) {
			fprintf(stderr, "Throwing java out of memory
error\n");
			(genv)->ThrowNew(newExcCls, "Out of memory in
the 3D graphics library.");
			fprintf(stderr, "Thrown java out of memory
error\n");
			return;
		}
	}
	fprintf(stderr, "Throwing bad_alloc error\n");
	throw bad_alloc();
	fprintf(stderr, "Thrown bad_alloc error\n");
	return;
}





****Disclaimer***********
This e-mail and any attachments may contain confidential and/or privileged material; it  is for the intended addressee(s) only.  If you are not a named addressee, you must not use, retain or disclose such information.
 
Serco cannot guarantee that the e-mail or any attachments are free from viruses.
 
The views expressed in this e-mail are those of the originator and do not necessarily represent the views of Serco.
 
Nothing in this e-mail shall bind Serco in any contract or obligation.
 
Serco Group plc.  Registered in England and Wales.  No: 2048608
Registered Office:  Serco House, 16 Bartley Wood Business Park, Bartley Way, Hook, Hampshire, RG27 9UY, United Kingdom.
****End Disclaimer*******
<<<<GWIASIG 0.07>>>>



More information about the vtkusers mailing list