2014-06-17 57 views
1

我從Android應用程序本地端傳遞窗口緩衝區到Java端。我應該爲android位圖釋放內存嗎?

AndroidBitmapInfo info; 

void saveBufferToBitmap(JNIEnv *env, ANativeWindow_Buffer *buffer, jobject bitmap) { 
    void *pixels; 

    LOGI(10, "saving buffer to bitmap"); 

    if (AndroidBitmap_getInfo(env, bitmap, &info) < 0) { 
     LOGE(10, "Failed to get bitmap info"); 
      return; 
    } 

    if (AndroidBitmap_lockPixels(env, bitmap, &pixels) < 0) { 
     LOGE(10, "Failed to lock pixles for bitmap"); 
     return; 
    } 

    int i, scan_length; 
    scan_length = buffer->width * 4; 

    memcpy(pixels, buffer->bits, buffer->width * buffer->height * 4); // 4 = (rgba) 

    AndroidBitmap_unlockPixels(env, bitmap); 

    //free(pixels); // here 
} 

我應該在// here免費像素緩衝區? AndroidBitmap_lockPixels/AndroidBitmap_unlockPixels是否將緩衝區複製到位圖?

回答

0

作爲一般規則,您通常不應該使用free指針,而您自己並不是用new創建的。庫調用你得到的指針可以使用任何分配,或者只是傳遞一個指向內部數據結構的指針。第二種很可能在這種情況下。

看着從源文件:

/** 
* Given a java bitmap object, attempt to lock the pixel address. 
* Locking will ensure that the memory for the pixels will not move 
* until the unlockPixels call, and ensure that, if the pixels had been 
* previously purged, they will have been restored. 
* 
* If this call succeeds, it must be balanced by a call to 
* AndroidBitmap_unlockPixels, after which time the address of the pixels should 
* no longer be used. 
* 
* If this succeeds, *addrPtr will be set to the pixel address. If the call 
* fails, addrPtr will be ignored. 
*/ 
int AndroidBitmap_lockPixels(JNIEnv* env, jobject jbitmap, void** addrPtr); 

來源:https://android.googlesource.com/platform/frameworks/native/+/master/include/android/bitmap.h

這告訴我們不要AndroidBitmap_unlockPixels後pixels做任何事情,絕對不是free它。

相關問題