I have a java application which calls into a VC++ DLL via JNI. Within
the DLL, I call some code via a 3rd party API which ultimately does
some disk IO. When the "File Writing" code is hit, my application
crashes. I get the following error (I'll spare you the entire dump)
An unexpected exception has been detected in native code outside the
VM.
Unexpected Signal : EXCEPTION_ACCESS_VIOLATION (0xc0000005) occurred at
PC=0x7C9111DE
Function=wcsncpy+0x74F
Library=C:\WINDOWS\system32\ntdll.dll
Here are a couple of interesting points...
The code will crash when running under XP Service Pack 2
The code will NOT crash when running under Windows 2000 Service Pack 4
When running on XP, the code will crash if the java progam makes either
of the following calls before making the JNI call:
UIManager.setLookAndFeel("com.sun.java.swing.plaf.windows.WindowsLookAndFeel");
- or -
UIManager.setLookAndFeel(UIManager.getSystemLookAndFeelClassName());
When running on XP, the code will NOT crash if I omit the
setLookAndFeel() call all together, or use an alternate L&F (Metal for
example)
I have spent the past couple of days trying to find clues to this
problem on the newsgroups, and have had absolutely no luck.
If anyone has any experience with this problem, I would appreciate any
leads.
Also - if anyone could recommend any tools and/or techniques that would
be useful in debugging a VM crash such as I described above, that would
be appreciated as well.
Thanks!
Ed Sandfort
Gordon Beaton - 10 Aug 2006 16:57 GMT
> I have a java application which calls into a VC++ DLL via JNI. Within
> the DLL, I call some code via a 3rd party API which ultimately does
[quoted text clipped - 9 lines]
>
> Here are a couple of interesting points...
You have an error in your native code that leaves the JVM in an
unstable state.
The fact that it appears to work correctly in some configurations is
not an indication that the code is ok.
Your error is likely due to slopping pointer handling, failure to
manage memory properly, uninitialized variables, failure to check
return values, and things like that. More information can be found
here:
http://c-faq.com/strangeprob/segv.html
http://c-faq.com/strangeprob/funnybugs.html
http://c-faq.com/malloc/crash.html
/gordon

Signature
[ don't email me support questions or followups ]
g o r d o n + n e w s @ b a l d e r 1 3 . s e
Bill Medland - 10 Aug 2006 18:20 GMT
>> I have a java application which calls into a VC++ DLL via JNI. Within
>> the DLL, I call some code via a 3rd party API which ultimately does
[quoted text clipped - 12 lines]
> You have an error in your native code that leaves the JVM in an
> unstable state.
Of course. Microsoft NEVER make a mistake. Sun NEVER make a mistake. The
third party NEVER make a mistake. It can't possibly be any one else that
is wrong; it must be the OP.
> The fact that it appears to work correctly in some configurations is
> not an indication that the code is ok.
[quoted text clipped - 9 lines]
>
> /gordon
Based on my experience there is a 1% chance that the problem is not the OP.
I personally would expect that the problem is caused by the L&F resulting in
something becoming null where the 3rd party library doesn't handle that
possibility.
Honestly I would suggest the "if it hurts, don't do it" solution.

Signature
Bill Medland
Gordon Beaton - 11 Aug 2006 08:03 GMT
> Of course. Microsoft NEVER make a mistake. Sun NEVER make a mistake.
> The third party NEVER make a mistake. It can't possibly be any one
> else that is wrong; it must be the OP.
At first I thought you were being ironic, but then you wrote:
> Based on my experience there is a 1% chance that the problem is not
> the OP.
I agree, even if I think that 1% is somewhat high.
/gordon

Signature
[ don't email me support questions or followups ]
g o r d o n + n e w s @ b a l d e r 1 3 . s e
esandfort - 10 Aug 2006 18:25 GMT
Thanks Gordon,
Your point is well taken, but I'm fairly confident this is not the
problem. I experienced the error in a more complex system, and was
able to duplicate it with a VERY simple test program. It's unlikely
the test code contains are any of the errors you describe in your
response.
I should also mention that when the crash does happen, the last line of
code executed in my program seems to be somewhat randon according to
the print logs I include. It is as if the severe error occurs off in
another thread, and any correlation between the timing of the system
crash, and the line of my code that is currently being executed can't
be established with any regularity.
Thanks again!
Ed Sandfort
Inácio Ferrarini - 11 Aug 2006 13:15 GMT
Hi there.
I am not experienced (I never did) in coding a dll and access it via
Java, but, I would say to you that, if you do not mind, try to use the
same DLL via C++ or other windows native language, and, use the same
thing on the test app that your Java app uses, and, see if the crash
still occurs. This way, you will be able to isolate if the error is
internal of the dll or of the way you are using it in Java - even if
the error only happens sometimes under certain so quasai conditions -
but, as said before, it does not mean that the code is entirely ok.
Hope I Helped,
- Inácio Ferrarini
> I have a java application which calls into a VC++ DLL via JNI. Within
> the DLL, I call some code via a 3rd party API which ultimately does
[quoted text clipped - 37 lines]
>
> Ed Sandfort
esandfort - 11 Aug 2006 14:23 GMT
Hello Inácio,
Thanks for the suggestion. If I understand you correctly, you are
suggesting that I access the C++ DLL's functions via a C++ program, to
take the JVM out of the equation. I have done this already, and this
scenario works fine without any crash.
I suspect there is some memory conflict condition that I am causing by
accessing the Windows XP disk read/write code (presumably in ntdll.dll
??????) running from within a JVM, and by calling the WindowLookAndFeel
code into play.
I have been researching this issue pretty rigorously for the past few
days, and have found other posting reporting similar problems.
Unfortunately, it seems that nobody has been able to offer a satisfying
explanation, and in my particular case, I have to use the Windows L&F,
so simply omitting it is not an option.
Short of a complete explanation on how to fix this problem, I could
really use advise on how to get more information from the crash that I
can use to track the problem down.
Thanks again for your post.
Ed Sandfort