c++ - will the shared_ptr be deleted if i delete the class -
i have class :
header:
class curlasio { public: boost::shared_ptr<boost::asio::io_service> io_ptr; boost::shared_ptr<curl::multi> multi_ptr; curlasio(); virtual ~curlasio(); void deleteself(); void someevent(); };
cpp:
curlasio::curlasio(int i) { id = boost::lexical_cast<std::string>(i); io_ptr = boost::shared_ptr<boost::asio::io_service>(new boost::asio::io_service()); multi_ptr = boost::shared_ptr<curl::multi>(new curl::multi(*io_ptr)); } curlasio::~curlasio() { } void curlasio::someevent() { deleteself(); } void curlasio::deleteself() { if (io_ptr) { io_ptr.reset(); } if (multi_ptr) multi_ptr.reset(); if (this) delete this; }
during run time, many instances of curlasio class created , deleted.
so questions are:
- even though calling shared_ptr.reset() , necessary ?
- i monitor virtual memory usage of program during run time , expect memory usage go down after deleteself() has been called, not. why that?
if modify deleteself() this:
void curlasio::deleteself() { delete this; }
what happens 2 shared pointers ? deleted ?
the shared_ptr
members have own destructor decrement reference count on pointee object, , delete
if count reaches 0. not need call .reset()
explicitly given destructor run anyway.
that said - why using shared_ptr
? members shared other objects? if not - consider unique_ptr
or storing value.
as memory - doesn't returned operating system until program terminates, available memory reuse. there many other stack overflow questions this.
if you're concerned memory, using leak detection tool idea. on linux example, valgrind excellent.
Comments
Post a Comment