While trying to follow the implementation of weak and strong (smart)
pointers in Android, I created a few code snippets to test my
understanding. I'm sharing a few samples here. I have attached a file
that show the contains of my Android.mk file, source files, commands
to build this sample code, and also the logs.

I'm providing it here believing that there are many like me out there
who'd benefit. Please feel free to provide comments and suggestions.Of
course, one needs to spend some time with RefBase.h and RefBase.cpp to
really appreciate what's going on behind the scene. I'm reserving the
explanations for some other day :)

regards
Devi Prasad

--~--~---------~--~----~------------~-------~--~----~
You received this message because you are subscribed to the Google
Groups "Android Developers" group.
To post to this group, send email to android-developers@googlegroups.com
To unsubscribe from this group, send email to
android-developers-unsubscr...@googlegroups.com
For more options, visit this group at
http://groups.google.com/group/android-developers?hl=en
-~----------~----~----~----~------~----~------~--~---

-----------------------------------------------------------------------
    A C++ source file (without a main function)
-----------------------------------------------------------------------
#define LOG_TAG "smart-ptr-sample"

#include "utils/RefBase.h"
#include "utils/Log.h"

using namespace android;

class Point : public RefBase {
public:
    Point(int x, int y) :  mx(x), my(y) {
        LOGD("Point constructor");
        extendObjectLifetime(OBJECT_LIFETIME_WEAK);
    }
    virtual ~Point() {
        LOGD("Point destructor");
    }

    virtual void onFirstRef() {
        LOGD("first weak ptr ref callback");
    }

    virtual void onLastStrongRef(const void* id) {
        LOGD("last strong ptr ref callback");
    }

    virtual void onLastWeakRef(const void* id) {
        LOGD("last weak ptr ref callback");
    }

private:
    int mx, my;
};

-----------------------------------------------------------------------
    Here's the contents of Android.mk file
-----------------------------------------------------------------------
LOCAL_PATH:= $(call my-dir)
include $(CLEAR_VARS)

$(info building weak pointer example code)

LOCAL_SRC_FILES:= weakp.cpp

LOCAL_SHARED_LIBRARIES := \
   libutils \
   libcutils
        
LOCAL_C_INCLUDES += frameworks/base/include system/core/include
                
LOCAL_MODULE:= weakp

include $(BUILD_EXECUTABLE)

-----------------------------------------------------------------------
  Here's are the instructions to compile and run the code within the emulator
-----------------------------------------------------------------------

I created a folder named "example" within the top directory (i.e., at same 
level as frameworks and external directories), and used the following commands 
to build my samples:

(1) . build/envsetup.sh 
(2) mmm

Then I'd push the resulting executable (using 'adb push') to '/data/app' 
directory and run it from there. Use 'adb logcat' to view the logs.


-----------------------------------------------------------------------
  Here's are a few samples of 'main' function and corresponding logs
-----------------------------------------------------------------------
int main()
{
    {
        {
            wp<Point> weakp(new Point(10, 20));

            LOGD("weak ptr's lifetime is just about to finish ...\n");
        }
        LOGD("weak ptr is out of scope.\n");
    }

    return 0;
}

D/smart-ptr-sample(  731): Point constructor
D/smart-ptr-sample(  731): weak ptr's lifetime is just about to finish ...
D/smart-ptr-sample(  731): last weak ptr ref callback
D/smart-ptr-sample(  731): Point destructor
D/smart-ptr-sample(  731): weak ptr is out of scope.
-----------------------------------------------------------------------

int main()
{
    {
        {
            wp<Point> weakp(new Point(10, 20));

            weakp.promote();
            LOGD("weak ptr's lifetime is just about to finish ...\n");
        }

        LOGD("weak ptr is out of scope.\n");
    }
    
    return 0;
}

D/smart-ptr-sample(  734): Point constructor
D/smart-ptr-sample(  734): first ptr ref callback
D/smart-ptr-sample(  734): last strong ptr ref callback
D/smart-ptr-sample(  734): weak ptr's lifetime is just about to finish ...
D/smart-ptr-sample(  734): last weak ptr ref callback
D/smart-ptr-sample(  734): Point destructor
D/smart-ptr-sample(  734): weak ptr is out of scope.

-----------------------------------------------------------------------

int main()
{
    {
        sp<Point> strp;
        
        {
            wp<Point> weakp(new Point(10, 20));
            
            strp = weakp.promote();
            LOGD("weak ptr's lifetime is just about to finish ...\n");
        }
        LOGD("weak ptr is out of scope.\n");
        LOGD("only strong ptr is in scope.\n");
    }
    
    LOGD("strong ptr is out of scope.\n");
    
    return 0;
}

D/smart-ptr-sample(  729): Point constructor
D/smart-ptr-sample(  729): first weak ptr ref callback
D/smart-ptr-sample(  729): weak ptr's lifetime is just about to finish ...
D/smart-ptr-sample(  729): weak ptr is out of scope.
D/smart-ptr-sample(  729): only strong ptr is in scope.
D/smart-ptr-sample(  729): last strong ptr ref callback
D/smart-ptr-sample(  729): last weak ptr ref callback
D/smart-ptr-sample(  729): Point destructor
D/smart-ptr-sample(  729): strong ptr is out of scope.

-----------------------------------------------------------------------

Reply via email to