Invalid global pointer in dll
I hope someone can help me with this...
I wrote a JNI wrapper dll that accesses another commercial dll. This commercial dll exposes all its functionalities through a c++ singleton object. You access this object from an exported global pointer. I assume that this object is either statically initialized, or initialized by the commercial dll's DllMain(), as there is no initialization required. You just dereference this pointer to get at the methods.
Now the problem is that I get an Access Violation when I call a JNI function that accesses this pointer. Apparently the pointer is invalid.
But when I create an EXE that also access this pointer the same way, it works just fine. I tried to explicitly load the commercial dll, either by LoadLibrary(.) in the JNI function, or through static {System.loadLibrary(.);} from the Java side. But no luck.
So what is the problem?
[915 byte] By [
jeffyuan] at [2007-9-26 9:07:47]

Thanks for your reply. I am sure it is dereferenced correctly. It seems that global pointer is not initialized properly. In fact, it's pointing to an area of memory full of zeros. I suspect that the DLL init code is not called properly. Perhaps _DllMainCRTStartup() or DLLMain() is not called for some reason.
How does the 3rd party dll get loaded? Are you loading it in java?
Presumably you are using the same compiler to create the java dll as you use to create the executable for testing. Are you changing options - other than the obvious ones necessary for a dll?
Is your executable written in C or C++?
The third party dll is loaded implicitly since I am not explicitly loading it. Even if I explicitly load it, I can use GetProcAddress to get back a function pointer only. I can't use it to get a static global pointer.
Yes, same compiler, and I used default switches for making a multithreaded dll. All normal stuff.
It seems as if the dlient dll is not loaded or initialized. so when the JNI dll wrapper tries to use the pointer...
I'm having a similar problem, but with Solaris and Java 1.3.1_01. Global pointers that are initialized at compile time cause a Segmentation Violation when dereferenced. I did a bit of investigation and it almost looks like globals are initialized before symbols get relocated.
Here's an example of what would happen with the following C source:
#include <stdio.h>
static int my_global = 0;
static int *my_global_p = &my_global;
void demonstrate_error(void)
{
printf("&my_global == %p\n", &my_global);
printf("my_global_p == %p\n", my_global_p);
}
When I would link this code in and call demonstrate_error() via JNI, the output would look like the following:
&my_global == 0xeb3805d0
my_global_p == 0x3805d0
These are not the exact numbers I got as output, but the principle is the same: the addresses were equivalent, except &my_global was 0xeb whereas my_global_p was 0x00. This only happens when linked and called through JNI.
Perhaps you were having the same problem under Windows? Did the Access Violation happen as a result of using global pointers initialized at compile time?