I'm trying to implement the approach described here:
which was initially described here:
because on some machines I'm getting out of memory errors related to native memory, even though the heap seems to have plenty of memory.
This solution is intended to allow me to allocate memory in my C++ code on the Java heap. Is this an approach that should work? I tried implementing it, but I keep getting segfaults. Here's an excerpt of the method, which is inside a C++ new operator. The idea is that the new caller gets a pointer to a Java byte array which is returned to the JVM in the delete operator.
JNIEnv *env = JNU_GetEnv();
jbyteArray jba = env->NewByteArray((int) t + sizeof(Jalloc));
void *jbuffer = static_cast<void *>(env->GetByteArrayElements(jba, 0));
Jalloc *pJalloc = static_cast<Jalloc *>(jbuffer);
pJalloc->jba = jba;
pJalloc->ref = env->NewGlobalRef(jba);
return static_cast<void *>(static_cast<char *>(jbuffer) + sizeof(Jalloc));