Skip to content

JNA 5.13.0: C++ Side Mutex Creation Fails with "Invalid Memory" Error #1670

Description

@ctxqlxs

Description:

When using JNA 5.13.0 to interface with a C++ library, an issue arises where attempting to create a mutex on the C++ side results in an "invalid memory" error. This issue occurs consistently whenever the mutex creation is attempted. Below are the details of the environment and the problem:

Environment:

  • Java Version: 1.8.0_361 (Oracle JDK)
  • JNA Version: 5.13.0(5.17.0 same err)
  • Operating System: [Windows 10]
  • C++ Compiler/Environment: [MSVC]

Problem Description:
The Java application uses JNA to interact with a native C++ library.
On the C++ side, when attempting to create a std::mutex (or any other type of mutex), the program throws an error related to invalid memory access.
The error message or behavior indicates that the memory allocation or usage for the mutex is corrupted or invalid.

Example Code:

Java Side:

public interface MyLibrary extends Library {
    MyLibrary INSTANCE = Native.load("mylibrary", MyLibrary.class);

    void createMutex();
}

public class Main {
    public static void main(String[] args) {
        MyLibrary.INSTANCE.createMutex();
    }
}

C++ Side:

#include <mutex>
extern "C" {
    void createMutex() {
        std::mutex mtx; // Attempting to create a mutex
        mtx.lock();      // Error occurs here or during construction
        mtx.unlock();
    }
}

Error
java.lang.Error: Invalid memory access

Activity

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions