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)
Excellent blog. Looking for software courses?
ReplyDeleteLoadrunner Training in Chennai
JAVA Training in Chennai
Hadoop Training in Chennai
Selenium Training in Chennai
German Classes in chennai
French Classes in Chennai
french institute in chennai
It is a excellent blog looking some thing different.
ReplyDeleteBig Data Hadoop Training In Chennai | Big Data Hadoop Training In anna nagar | Big Data Hadoop Training In omr | Big Data Hadoop Training In porur | Big Data Hadoop Training In tambaram | Big Data Hadoop Training In velachery
en son çıkan perde modelleri
ReplyDeleteNUMARA ONAY
mobil ödeme bozdurma
nft nasıl alınır
ankara evden eve nakliyat
Trafik Sigortasi
dedektor
KURMA WEB SİTESİ
ask romanlari