Forum Stats

  • 3,852,832 Users
  • 2,264,140 Discussions
  • 7,905,155 Comments

Discussions

JNI, OpenGL, glibc detected java: free(): invalid next size (fast)

I have a simple Java program that calls a single JNI method. Inside the native method a GLWindow (wraps OpenGL) is created and then deleted. The native method returns to the Java side, and then when the program attempts to exit it crashes and reports the problematic frame:
C [libc.so.6+0x6f871] cfree+0x31

Interestingly, when I create a GLWindow, delete it, create another GLWindow, and delete it, all within the same native method (without returning to java), the crash happens while in native code (during the second delete) with one of the following errors (backtraces at end of post):
(1) *** glibc detected *** java: free(): invalid next size (fast): 0x081ab388 ***
(2)*** glibc detected *** java: double free or corruption (out): 0x09fd5a20 ***
(3)*** glibc detected *** java: munmap_chunk(): invalid pointer: 0x084dbdc0 ***

Also, I don't encounter any problems creating and deleting GLWindows when I'm running from a c++ program (no java), which has led me to believe this is a Java problem. Any help on this issue would be greatly appreciated. Please see the source code and partial stack traces below. Thanks!

Java Main:
package test;

public class Main {

    static {
        System.loadLibrary("native");
    }

static public native void createGLWindow();

    /**
     * @param args the command line arguments
     */
    public static void main(String[] args) {
        System.out.println("starting test project");
	createGLWindow();
	System.out.println("returned from native.");
    }

}
.cpp that contains native method called from Java
#include "test_Main.h"
#include <stdio.h>
#include <string>
#include <iostream>
#include "GLWindow/GLWindow.h"

JNIEXPORT void JNICALL Java_test_Main_createGLWindow(JNIEnv *, jclass) {
	std::cout << "in native code!" << std::endl;
	blortGLWindow::GLWindow* temp_window = new blortGLWindow::GLWindow(320, 240, "Tracking");
	std::cout << "glWindow created. sleeping..." << std::endl;
	sleep(5);
	std::cout << "done sleeping" << std::endl;
	delete temp_window;

/*	temp_window = new blortGLWindow::GLWindow(320, 240, "Tracking");
	std::cout << "glWindow created. sleeping..." << std::endl;
	sleep(5);
	std::cout << "done sleeping" << std::endl;
	delete temp_window;
	std::cout << "done deleting" << std::endl;
*/
}
.cpp that wraps OpenGL:
#ifdef LINUX

#include "GLWindow/GLWindow.h"
#include <stdio.h>
#include <stdexcept>
#include <vector>

namespace blortGLWindow{

void GLWindow::init(unsigned int width, unsigned int height, const char* name){
  GLint att[] = { GLX_RGBA, GLX_DEPTH_SIZE, 24, GLX_DOUBLEBUFFER, None };
  dpy = XOpenDisplay(NULL);

  if(dpy == NULL){
    throw std::runtime_error("[GLWindow::init] Error cannot connect to X server");
  }
				  
  root = DefaultRootWindow(dpy);
  vi = glXChooseVisual(dpy, 0, att);

  if(vi == NULL)
    throw std::runtime_error("[GLWindow::init] Error no appropriate visual found");

  cmap = XCreateColormap(dpy, root, vi->visual, AllocNone);

  swa.colormap = cmap;
  swa.event_mask = ExposureMask | KeyPressMask | KeyReleaseMask | ButtonPressMask | ButtonReleaseMask | PointerMotionMask;

  glWin = XCreateWindow(dpy, root, 0, 0, width, height, 0, vi->depth, InputOutput, vi->visual, CWColormap | CWEventMask, &swa);

  wmDelete = XInternAtom(dpy, "WM_DELETE_WINDOW", true);
  XSetWMProtocols(dpy, glWin, &wmDelete, 1);

  XMapWindow(dpy, glWin);
  XStoreName(dpy, glWin, name);

  glc = glXCreateContext(dpy, vi, NULL, GL_TRUE);
  glXMakeCurrent(dpy, glWin, glc);
  
  glXSwapBuffers(dpy, glWin);
}

void GLWindow::quit(){
  glXMakeCurrent(dpy, None, NULL);
  glXDestroyContext(dpy, glc);
  XDestroyWindow(dpy, glWin);
  XCloseDisplay(dpy);
}
GLWindow::GLWindow(unsigned int width, unsigned int height, const char* name){
	init(width, height, name);
}
GLWindow::~GLWindow(){
	quit();
}

void GLWindow::Activate(){
	glXMakeCurrent(dpy, glWin, glc);
}

void GLWindow::Update(){
  glXSwapBuffers(dpy, glWin);
}

} /* namespace */

#endif /* LINUX */
When the crash happens in Java the stack trace is:
C [libc.so.6+0x6f871] cfree+0x31
C [libGLcore.so.1+0x31d9ee]
C [libc.so.6+0x2f05f]
C [libc.so.6+0x2f0cf]
C [libc.so.6+0x16b5e] __libc_start_main+0xee

When the crash happens in native the stack trace is one of the following:
(1)
*** glibc detected *** java: free(): invalid next size (fast): 0x081ab388 ***
======= Backtrace: =========
/lib/tls/i686/cmov/libc.so.6[0xb76e80d1]
/lib/tls/i686/cmov/libc.so.6[0xb76e97d2]
/lib/tls/i686/cmov/libc.so.6(cfree+0x6d)[0xb76ec8ad]
/usr/lib/libstdc++.so.6(_ZdlPv+0x21)[0x6c5736f1]
/home/evankrause/hril/Test/src/libnative.so(Java_test_Main_createGLWindow+0x1c1)[0xb39e0b7d]
[0xb3ab95aa]
[0xb3ab1e61]
[0xb3aaf34c]
/usr/lib/jvm/java-6-openjdk/jre/lib/i386/server/libjvm.so[0xb6ef3e65]
/usr/lib/jvm/java-6-openjdk/jre/lib/i386/server/libjvm.so[0xb706f429]
/usr/lib/jvm/java-6-openjdk/jre/lib/i386/server/libjvm.so[0xb6ef2d5f]
/usr/lib/jvm/java-6-openjdk/jre/lib/i386/server/libjvm.so[0xb6efd6ec]
/usr/lib/jvm/java-6-openjdk/jre/lib/i386/server/libjvm.so[0xb6f099ad]
java(JavaMain+0xd4d)[0x804b9ad]
/lib/tls/i686/cmov/libpthread.so.0[0xb77d080e]
/lib/tls/i686/cmov/libc.so.6(clone+0x5e)[0xb7749a0e]

(2)
*** glibc detected *** java: double free or corruption (out): 0x09fd5a20 ***
======= Backtrace: =========
/lib/tls/i686/cmov/libc.so.6[0xb77530d1]
/lib/tls/i686/cmov/libc.so.6[0xb77547d2]
/lib/tls/i686/cmov/libc.so.6(cfree+0x6d)[0xb77578ad]
/usr/lib/libX11.so.6(_XFreeDisplayStructure+0x3f)[0x6b2e071f]
/usr/lib/libX11.so.6(XCloseDisplay+0xe1)[0x6b2cd1c1]
/usr/local/lib/libGLWindow.so(_ZN13blortGLWindow8GLWindow4quitEv+0x78)[0x6c806714]
/usr/local/lib/libGLWindow.so(_ZN13blortGLWindow8GLWindowD1Ev+0x2b)[0x6c8068e9]
/home/evankrause/hril/Test/src/libnative.so(Java_test_Main_createGLWindow+0x1b9)[0xb6b5cb75]
[0xb3b245aa]
[0xb3b1ce61]
[0xb3b1a34c]
/usr/lib/jvm/java-6-openjdk/jre/lib/i386/server/libjvm.so[0xb6f5ee65]
/usr/lib/jvm/java-6-openjdk/jre/lib/i386/server/libjvm.so[0xb70da429]
/usr/lib/jvm/java-6-openjdk/jre/lib/i386/server/libjvm.so[0xb6f5dd5f]
/usr/lib/jvm/java-6-openjdk/jre/lib/i386/server/libjvm.so[0xb6f686ec]
/usr/lib/jvm/java-6-openjdk/jre/lib/i386/server/libjvm.so[0xb6f749ad]
java(JavaMain+0xd4d)[0x804b9ad]
/lib/tls/i686/cmov/libpthread.so.0[0xb783b80e]
/lib/tls/i686/cmov/libc.so.6(clone+0x5e)[0xb77b4a0e]

(3)
*** glibc detected *** java: munmap_chunk(): invalid pointer: 0x084dbdc0 ***
======= Backtrace: =========
/lib/tls/i686/cmov/libc.so.6[0xb77290d1]
/lib/tls/i686/cmov/libc.so.6[0xb772a2d5]
/usr/lib/libXext.so.6(XextRemoveDisplay+0xb2)[0x6b3c1a42]
/usr/lib/libX11.so.6(XCloseDisplay+0xab)[0x6b29b18b]
/usr/local/lib/libGLWindow.so(_ZN13blortGLWindow8GLWindow4quitEv+0x78)[0x6cb09714]
/usr/local/lib/libGLWindow.so(_ZN13blortGLWindow8GLWindowD1Ev+0x2b)[0x6cb098e9]
/home/evankrause/hril/Test/src/libnative.so(Java_test_Main_createGLWindow+0x1b9)[0x6cb21b75]
[0xb3afa5aa]
[0xb3af2e61]
[0xb3af034c]
/usr/lib/jvm/java-6-openjdk/jre/lib/i386/server/libjvm.so[0xb6f34e65]
/usr/lib/jvm/java-6-openjdk/jre/lib/i386/server/libjvm.so[0xb70b0429]
/usr/lib/jvm/java-6-openjdk/jre/lib/i386/server/libjvm.so[0xb6f33d5f]
/usr/lib/jvm/java-6-openjdk/jre/lib/i386/server/libjvm.so[0xb6f3e6ec]
/usr/lib/jvm/java-6-openjdk/jre/lib/i386/server/libjvm.so[0xb6f4a9ad]
java(JavaMain+0xd4d)[0x804b9ad]
/lib/tls/i686/cmov/libpthread.so.0[0xb781180e]
/lib/tls/i686/cmov/libc.so.6(clone+0x5e)[0xb778aa0e]
This discussion has been closed.