繁体   English   中英

JNI使Windows 7中的Java崩溃

[英]JNI is crashing the java in windows 7

我正在将代码从linux移植到Windows,因为Java中不支持Linux共享内存段。 因此,我们使用Java本机接口(JNI)来访问共享内存。它在linux平台上运行良好。

shmget的代码:

JNIEXPORT jint JNICALL Java_emulatorinterface_communication_shm_SharedMem_shmget
(JNIEnv * env, jobject jobj, jint COUNT,jint MaxNumJavaThreads,jint EmuThreadsPerJavaThread,
        jlong coremap, jint pid) {

    uint64_t mask = coremap;

    int size;//=sizeof(packet)*(COUNT+5)*MaxNumJavaThreads*EmuThreadsPerJavaThread;

    char str[50];
    int ptr;

#ifdef _WIN32
    HANDLE hMapFile;
#endif
    printf("hello");
    size=sizeof(packet)*(COUNT+5)*MaxNumJavaThreads*EmuThreadsPerJavaThread;
    /*if (sched_setaffinity(0, sizeof(mask), (cpu_set_t *)&mask) <0) {
        perror("sched_setaffinity");
    }*/


    //set the global variables
    gCOUNT = COUNT;
    gMaxNumJavaThreads = MaxNumJavaThreads;
    gEmuThreadsPerJavaThread = EmuThreadsPerJavaThread;

    //size1 is the number of packets needed in the segment.

#ifdef _WIN32

    _itoa(pid,str,10);


    hMapFile = CreateFileMapping(INVALID_HANDLE_VALUE,
        NULL, PAGE_READWRITE,
        0,32, str);

    if (hMapFile == NULL)
    {

         exit(1);
    }
    CloseHandle(hMapFile);

    hMapFile = CreateFileMapping(INVALID_HANDLE_VALUE,
        NULL, PAGE_READWRITE,
        0,size, str);

    if (hMapFile == NULL)
    {
        printf("Unable to create a shared mem file.");
        exit(1);
    }
    if (hMapFile < 0)
    {
        return (-1);
    }

    /*
    if((hMapFile = CreateFileMapping(INVALID_HANDLE_VALUE,
        NULL, PAGE_READWRITE,
        0,size, str))<0)
    {
        printf("error window %d\n",size);
        return (-1);
    }
    */
    ptr=*((int*)(hMapFile));

    return ptr;          ///////////////error in return type 
    #else

如果我尝试在JNI中使用任何其他功能,我的Java崩溃。

错误

#
# A fatal error has been detected by the Java Runtime Environment:
#
#  EXCEPTION_ACCESS_VIOLATION (0xc0000005) at pc=0x000007feedf911be, pid=4696, tid=4216
#
# JRE version: Java(TM) SE Runtime Environment (8.0_77-b03) (build 1.8.0_77-b03)
# Java VM: Java HotSpot(TM) 64-Bit Server VM (25.77-b03 mixed mode windows-amd64 compressed oops)
# Problematic frame:
# C  [JNIShm.dll+0x11be]
#
# Failed to write core dump. Minidumps are not enabled by default on client versions of Windows
#
# An error report file with more information is saved as:
# D:\workspace\tejasMinor\hs_err_pid4696.log
#
# If you would like to submit a bug report, please visit:
#   http://bugreport.java.com/bugreport/crash.jsp
# The crash happened outside the Java Virtual Machine in native code.
# See problematic frame for where to report the bug.
#

谁能指出问题出在哪里?

您不能将HANDLE取消引用到文件映射对象。 只需让您的函数返回句柄即可,如下所示:

return (int)hMapFile;

在64位Windows(具有64位JRE / JNI)上, HANDLE类型为64位宽,但是只有低32位有效,因此可以。

旁注:

  1. 不要使用hMapFile < 0 如果API失败,则返回值为NULL
  2. 确定要先尝试使用32字节的文件,然后再将其关闭吗? 为什么?
  3. 您可能要调用GetLastError并针对ERROR_ALREADY_EXISTS测试结果(如果您希望能够检测到该情况)

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM