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 8648633
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 12, 20262026-06-12T13:21:30+00:00 2026-06-12T13:21:30+00:00

I use NDK to allocate large buffer for Java: allocNativeBuffer(JNIEnv* env, jobject cls, jlong

  • 0

I use NDK to allocate large buffer for Java:

allocNativeBuffer(JNIEnv* env, jobject cls, jlong size) {
    void* buffer = malloc(size);
    jobject directBuffer = env->NewDirectByteBuffer(buffer, size);
    jobject globalRef = env->NewGlobalRef(directBuffer);
    return globalRef;
}

After using this buffer I deallocate it:

freeNativeBuffer(JNIEnv* env, jobject cls, jobject globalRef) {
    void *buffer = env->GetDirectBufferAddress(globalRef);
    env->DeleteGlobalRef(globalRef);
    free(buffer);
}

On Android 2.2 it works fine, but on Android 4.0.3 application crashes during DeleteGlobalRef call. What am I doing wrong?

  • 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-12T13:21:31+00:00Added an answer on June 12, 2026 at 1:21 pm

    The implementation of global and local references was completely overhauled in ICS, with intention to improve memory management on the Java side. Find the explanation in developers guide Read more in the developers blog.

    In the nutshell, whatever you receive in a JNI function, including the globalRef parameter of freeNativeBuffer() function, are local references. You can create and keep a global reference in your C code, like this:

    static jobject globalRef;
    
    allocNativeBuffer(JNIEnv* env, jobject cls, jlong size) {
        void* buffer = malloc(size);
        jobject directBuffer = env->NewDirectByteBuffer(buffer, size);
        globalRef = env->NewGlobalRef(directBuffer);
        return directBuffer;
    }
    
    freeNativeBuffer(JNIEnv* env, jobject cls, jobject localRef) {
        void *buffer = env->GetDirectBufferAddress(localRef);
        if (buffer == env->GetDirectBufferAddress(globalRef) {
            /* we received the object that we saved */
            env->DeleteGlobalRef(globalRef);
            free(buffer);
        }
    }
    

    PS I found the stackoverflow discussion which looks like the inspiration for your experiment. See the answer which explains that there was no need to create and delete global references in the first place.

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

Sidebar

Related Questions

I'm trying to use the NDK with C++ and can't seem to get the
I try to run my project in windows. Project use android ndk. I install
I use android-sdk-r12m-r17m-linux.zip and android-ndk-r8-linux-x86.tar.bz2 to compile my android apk. My compiling steps are:
i'm trying to use the VertexArray with Android NDK for a project that already
How can I build libsdl using the Android NDK's standalone toolchain? I can't use
the Android ndk is hard to use for the old autoconf based code, so
I try to record audio using android ndk. people say I can use frameworks/base/media/libmedia/AudioRecord.cpp.
I compiled the sqlite3 amalgamation into Android NDK, and I'm able to use it
I am attempting to use the android NDK. Is there a way to return
All the ndk samples only make use of basic C functions declared as extern

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.