Amessage的clear会清除对象并释放内存空间

项目中用了Amessage,通过调试发现:

定义一个向量集合:

vector list;

其中DateStructure是一个继承Refbase的类

然后往list中添加数据

DateStructure*   data = new DateStructure();

list.push_back(data);

然后将这个list通过Amessage发送

sp msg =  new Amessage(AWhatSend, ***)

int size = list.size();

msg->setInt32("size", size);

if(size == 0) {

    return;

}

for(int i = 0; i

    string tag = to_string(i) 

    msg->setObject(tag, data[i]);

}

msg->post();

然后在onMessageReceived方法中接收这个message,然后执行list.clear()清除这个操作,然后发现对象的内存空间也被清除了,而这个清除操作是Amessage做的,Amessage.clear时发现会将DataStructure之前new出来的所有对象都清除,并释放内存空间。

int size;

msg->findInt32("size", &size);

if(size == 0) {

    return;

}

vector list;

for(int i = 0; data.size; i++) {j

    string tagid= to_string(i);
    const char* tag = tagid.c_str();

    RefBase obj;

    msg->findObject(tag, &obj);

    DataStructure* obj1 = nullptr;
    obj1 = static_cast(obj.get());

    list.push_back(obj1);

}

调用堆栈如下 

01-09 07:59:26.883  1022  1969 D ~DataStructure: #00 pc 00007cad  /vendor/bin/hw/[email protected] (android::DataStructure::~DataStructure()+68)
01-09 07:59:26.883  1022  1969 D ~DataStructure: #01 pc 00007d0b  /vendor/bin/hw/[email protected] (android::DataStructure::~DataStructure()+2)
01-09 07:59:26.883  1022  1969 D ~DataStructure: #02 pc 0000a725  /apex/com.android.vndk.v32/lib/libutils.so (android::RefBase::decStrong(void const*) const+68)
01-09 07:59:26.883  1022  1969 D ~DataStructure: #03 pc 00011da3  /vendor/lib/vndk/libstagefright_foundation.so (android::AMessage::clear()+86)
01-09 07:59:26.883  1022  1969 D ~DataStructure: #04 pc 00011d05  /vendor/lib/vndk/libstagefright_foundation.so (android::AMessage::~AMessage()+16)
01-09 07:59:26.883  1022  1969 D ~DataStructure: #05 pc 00011db7  /vendor/lib/vndk/libstagefright_foundation.so (android::AMessage::~AMessage()+2)
01-09 07:59:26.883  1022  1969 D ~DataStructure: #06 pc 0000a725  /apex/com.android.vndk.v32/lib/libutils.so (android::RefBase::decStrong(void const*) const+68)
01-09 07:59:26.883  1022  1969 D ~DataStructure: #07 pc 00010977  /vendor/lib/vndk/libstagefright_foundation.so (android::ALooper::loop()+510)
01-09 07:59:26.883  1022  1969 D ~DataStructure: #08 pc 0000d303  /apex/com.android.vndk.v32/lib/libutils.so (android::Thread::_threadLoop(void*)+302)
01-09 07:59:26.883  1022  1969 D ~DataStructure: #09 pc 0000cdc3  /apex/com.android.vndk.v32/lib/libutils.so (thread_data_t::trampoline(thread_data_t const*)+262)
01-09 07:59:26.883  1022  1969 D ~DataStructure: #10 pc 00080d97  /apex/com.android.runtime/lib/bionic/libc.so (__pthread_start(void*)+40)
01-09 07:59:26.883  1022  1969 D ~DataStructure: #11 pc 00039d93  /apex/com.android.runtime/lib/bionic/libc.so (__start_thread+30)

 

 

你可能感兴趣的:(android)