Skip to main content

Simple example of strong and weak pointers on Android using RefBase class

Here is a simple example explaining the usage of strong and weak pointers using android's RefBase class and the Android.mk to compile the same. Its a modified version of code written by Daniel in the following link.


strong_pointer.cpp file: 

#include <stdio.h>
#include "utils/RefBase.h"

namespace android { 
class RefTest : public RefBase {
public:
    RefTest(int32_t id) : mID(id) {
        printf("RefTest ctor: %d\n", mID);
    }
    virtual ~RefTest() {
        printf("RefTest dtor: %d\n", mID);
    }
    int32_t id() const {
        return mID;
    }
private:
    int32_t mID;
};

int strong_pointer() {
    sp<RefTest> ref1 = new RefTest(1);
    sp<RefTest> ref5;

    {
    sp<RefTest> ref2 = new RefTest(2);
    ref5 = ref2; // comment this out and check when the destructor of ref2 is called
    }
    wp<RefTest> ref3 = ref1;
    sp<RefTest> ref4 = ref3.promote();
    if (ref4 == NULL) {
        printf("RefTest object is destroyed\n");
    } else {
        printf("RefTest object %d is still around\n",
            ref4->id());
    }
    ref4 = NULL;
    ref1 = NULL; // comment this out to check to check change in prints
    ref4 = ref3.promote();
    if (ref4 == NULL) {
        printf("RefTest object is destroyed\n");
    } else {
        printf("RefTest object %d is still around\n",
            ref4->id());
    }
    printf("Before return \n"); 
    return 0;
}
}

int main()
{
    android::strong_pointer();
    return 0;
}

Android.mk file:

LOCAL_PATH:= $(call my-dir)
include $(CLEAR_VARS)

LOCAL_SRC_FILES:=                       \
        strong_pointer.cpp            

LOCAL_MODULE:= strong_pointer

LOCAL_SHARED_LIBRARIES := \
        libutils    \

LOCAL_MODULE_TAGS := optional

LOCAL_C_INCLUDES := \
        frameworks/native/include \

include $(BUILD_EXECUTABLE)


Comments

Post a Comment

Popular posts from this blog

Getting and Setting Microphone Gain or Microphone Boost on Windows 7 / 8 Programmatically

After hours of searching the net I have finally figured out a way to programmatically get and set the microphone gain (boost) value. The code mentioned below can be built on VS and does the job of setting gain value. #include "stdafx.h" #include <mmdeviceapi.h> #include <endpointvolume.h> #include <Functiondiscoverykeys_devpkey.h> #include "Audioclient.h" #include "comutil.h" #define EXIT_ON_ERROR(hres)   if (FAILED(hres)) { goto Exit; } #define SAFE_RELEASE(punk)   if ((punk) != NULL)  { (punk)->Release(); (punk) = NULL; } HRESULT getMicrophoneBoostVolumeLevel(IMMDevice *pEndptDev, IAudioVolumeLevel** ppVolumeLevel) { HRESULT hr = S_OK; DataFlow flow; IDeviceTopology *pDeviceTopology = NULL; IConnector *pConnFrom = NULL; IConnector *pConnTo = NULL; IPart *pPartPrev = NULL; IPart *pPartNext = NULL; *ppVolumeLevel = NULL; wchar_t microphoneBoostName[] = L"Microphone Boost";//if your system ...

MPEG2,4 AAC Decoder

I have modified latm aac decoder based on open source python decoder written by Arkq. https://gist.github.com/Arkq/66fe948c1051684d8909d730c34396d8 This decoder will decode AAC audio(IOS devices) captured with FTS HCI A2DP dump and save it in a playable audio WAV format. Using the tool :  1. Install latest python version > 3 on either windows or linux. 2. Download ffmpeg binaries from following link https://ffmpeg.zeranoe.com/builds/ 3. Extract the ffmpeg binaries and add the extracted bin folder to system path, for linux it can be done by exporting PATH variable and for windows it can be done by changing environment path variable. 4. Install pydub using following command pip install pydub 5. To decode the latm mpeg2,4 aac audio run following command with the attached python script.       latm_aac_decoder.py <extracted latm mpeg2,4 aac audio> eg :  latm_aac_decoder.py Bookmarked_snoop(A2DP)(1).mpeg2...