Skip to content

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

Open

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

  1. matthiasblaesing commented on Apr 26, 2025

    @matthiasblaesing
    Member

    In general Invalid memory access indicates, that something is wrong in your setup. I have yet to see evidence, that the JNA bindings are at fault.

    I suspect your code uses a different calling convention than the one assumed by JNA and thus the stack is not correctly setup for calls. As indicated in the issue template this is not for "my code causes fauls - why???" issues. I will not look further without a runnable example.

  2. mrwitzman commented on Jul 23, 2025

    @mrwitzman

    Hi @ctxqlxs,

    I am curious if you managed to resolve this issue? I am experiencing the same issue and it doesn't seem to matter whether I declare my DLL functions as __cdecl or __stdcall. I get the same crash. I haven't been able to figure out what is going wrong. Will work on making a runnable example and post it here if I don't hear back.

    Thanks

  3. syriku commented on Aug 29, 2025

    @syriku

    I met the same error. When I call a native function and try to get a global variable protected with mutex, I got a 'Invalid memory access' error. I even did not use anything from Jvm and just return a pointer.
    code seems like this:

    public interface Ixxx extends Library {
       Pointer xxxCreate();
    
    }

    and C++

    void* xxx_create()
    {
        return new xxx();
    }
    
    xxx::xxx(): _member(getSomeThingGlobal()) {}
    
    some* getSomeThingGlobal() {
        std::lock_guard lock(g_mutex);
        // do something
    }

    If I take out the mutex, it works correctly. So I believe it's because of mutex/lock guard.
    And I'm working on JNA 5.17.0

  4. ctxqlxs commented on Sep 29, 2025

    @ctxqlxs
    Author

    any update?

  5. connelltim commented on Sep 29, 2025

    @connelltim

    Had this same issue and it ended up being related to https://stackoverflow.com/questions/78598141/first-stdmutexlock-crashes-in-application-built-with-latest-visual-studio/78599923#78599923. In our case it was due to an older version of msvcp140.dll present in the working directory (JDK's bin folder) and being loaded instead of the system's version. Our native code was compiled against a newer version, and we got this crash when calling into it. Removing this dll from the jdk folder fixed it.

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