You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
{{ message }}
Repository navigation
JNA 5.13.0: C++ Side Mutex Creation Fails with "Invalid Memory" Error #1670
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();
}
}
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.
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.
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:
Activity
matthiasblaesing commentedon Apr 26, 2025
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.
mrwitzman commentedon Jul 23, 2025
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
syriku commentedon Aug 29, 2025
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:
and C++
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
ctxqlxs commentedon Sep 29, 2025
any update?
connelltim commentedon Sep 29, 2025
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.