Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 8882477
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 14, 20262026-06-14T20:35:21+00:00 2026-06-14T20:35:21+00:00

I have a native function, defined in my C++ code, that is called within

  • 0

I have a native function, defined in my C++ code, that is called within Java (after the Java code is called by C++).

(1) Declaration / definition of the native function in C++:

// C++ declaration/definition of the native function:

JNIEXPORT void JNICALL setEncoderProgressStatus (JNIEnv * env, jobject theClass, jlong jEncoderDecoderDlg, jstring status)
{
    // Do nothing.  Obviously, the real function does something.
    // But an access violation is reported even in the do-nothing case.
}

(2) Registration of the native function in C++

// Registration of this function with JNI in the C++ code

JNINativeMethod commandLineEncoderMethods[] =
{
    {"setEncoderProgressStatus","(JLjava/lang/String;)V", (void*)setEncoderProgressStatus}
};

// pEnv is a valid JNIEnv *
// jCommandLineEncoderClass has already been initialized
pEnv->RegisterNatives(jCommandLineEncoderClass, commandLineEncoderMethods, sizeof(commandLineEncoderMethods)/sizeof(JNINativeMethod))

(3) Declaration of the native function in Java:

// The Java code, in turn, declares the native function as follows:

public static native void setEncoderProgressStatus(long EncoderDecoderDlg, String status);

(4) Calling the native function within the Java code:

// Finally, the Java code calls the native function as follows:

// The real code passes different args,
// but an access violation is reported even in the do-nothing case.
// Commenting out the following line of code makes the access violation disappear.

setEncoderProgressStatus(0, "");

Note that this code works 100% successfully, and has worked for a long time. I just noticed that when running in the debugger in Visual Studio 2010, an access violation is reported. Simply commenting out the call to the native function in Java, causes the access violation to disappear.

If I knew that this is a known “fluke” with JNI when running applications in debug mode in VS 2010, and that it does not represent a problem with my code, then I would be satisfied.

However, I would like to confirm that there is no possible problem with my code.

…

* ADDENDUM *

The access violations do not cause program execution to halt. Instead, the only indication of the access violation is a line in the Output window. I am not sure if this means that the access violation was handled or not; however, I would guess it means that it was handled. Even if it’s handled, however, I would like to know if it necessarily indicates that there is a problem with my code.

Here is the message in the Output window:

First-chance exception at 0x03fbb256 in EncoderDecoder.exe:
0xC0000005: Access violation reading location 0x003d0100

…

* SECOND ADDENDUM *

I find that when the C++ application remains connected to the JVM, and I execute the same code again from the C++ application (causing the same Java code to be called, which in turn causes the Java code to call the same JNI function), that the access violations do not appear.

(In my actual program, there are a number of different native functions called within a number of different classes, so it’s not simple to isolate exactly when the access violation is reported, and when it stops being reported – but by the third run of the same functionality without quitting the C++ application, using the same loaded JVM, there are no first-chance access violation errors (and no plain access violation errors, either)).

This leads me to believe, in conjunction with Ben Voigt’s answer (and associated comments) below, that the JVM code does cause a first-chance exception (access violation) to occur as part of its NORMAL processing, and that this is not an issue with my code.

…

* ADDENDUM 3 *

From http://blogs.msdn.com/b/davidklinems/archive/2005/07/12/438061.aspx:

First chance exception messages most often do not mean there is a
problem in the code.

There still is the possibility that a first-chance exception that happens to be an access violation is more likely to represent a real problem, in which case my question is still open; but – I think the evidence points to the possibility that this is just how the JVM works, and does not represent a problem with my code.

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-14T20:35:23+00:00Added an answer on June 14, 2026 at 8:35 pm

    Is it an unhandled access violation, or does the call proceed if you continue (letting the exception handler run)?

    JIT compilers have a whole bunch of tricks to generate the native code for a function without every call suffering the cost of a conditional to see if the function has been JITted. One such technique is for the first call to be intercepted because it generates an access violation to trigger the JIT compiler. After the code has been generated once, future calls go through without an exception.

    I’m not familiar with whether any Java JIT compiler actually does so, but you can’t get a definitive answer without giving specific information about what JIT you’re running. Is it the Sun/Oracle HotSpot compiler? x86 or x86_64 or some other platform? You’re deep into implementation details which vary by implementation.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have a native function call that calls my c++ code and this code
I have Java application which sends pointer to function (callback) to some native dll
The Setup I have a PDF API which has a native function that is
i'm having an issue PInvoking a native function from c++... i have PInvoked all
I have a native/unmanaged DLL and it has a CreateObject function which returns a
I have a native object (C++) that has a gcroot pointer to a managed
Here I create a class in JAVA in which I have function (callback) which
I have created one native application in android that is working fine. This application
My problem is the following; I have a class that uses non-static native methods
I've been using JNA with relative success to make native function calls from Java

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.