本文主要是介绍Amessage的clear会清除对象并释放内存空间,希望对大家解决编程问题提供一定的参考价值,需要的开发者们随着小编来一起学习吧!
项目中用了Amessage,通过调试发现:
定义一个向量集合:
vector<DateStructure*> list;
其中DateStructure是一个继承Refbase的类
然后往list中添加数据
DateStructure* data = new DateStructure();
list.push_back(data);
然后将这个list通过Amessage发送
sp<Amessage> msg = new Amessage(AWhatSend, ***)
int size = list.size();
msg->setInt32("size", size);
if(size == 0) {
return;
}
for(int i = 0; i<data.size(); 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<DataStructure*> 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<DataStructure*>(obj.get());list.push_back(obj1);
}
调用堆栈如下
01-09 07:59:26.883 1022 1969 D ~DataStructure: #00 pc 00007cad /vendor/bin/hw/vendor.xxx.hardware.yyy@1.0-service (android::DataStructure::~DataStructure()+68)
01-09 07:59:26.883 1022 1969 D ~DataStructure: #01 pc 00007d0b /vendor/bin/hw/vendor.xxx.hardware.yyy@1.0-service (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)
这篇关于Amessage的clear会清除对象并释放内存空间的文章就介绍到这儿,希望我们推荐的文章对编程师们有所帮助!