memory management - How do I create an array or list of externally managed objects in modern c++? -
i building add-in program. add-in manipulates ptr objects passed me host application. create vector of externally created , managed objects host. unfortunately, documentation doesn't have clear examples of how this.
class players { vector<ptr<player>> vectorofgamers; // deletes , when this? public void createplayers () { // call static application create 3 players ( int = 0; < 3; i++ ) vectorofgamers.push_back(application.getnextplayer()); } }
confused how build class , prevent memory leaks , causing null exception if items deleted prematurely. also, how use modern c++ facilities achieve yet gain of benefits of new memory management make_shared, make_unique, nullptr, etc?
for information, below snapshot of ptr.i confused ptr appears superfluous given modern c++'s new memory management facilities.
class incompletetype { public: template<typename t> static void addref(void* ptr) { reinterpret_cast<adsk::core::referencecounted*>(ptr)->addref(); } template<typename t> static void release(void* ptr) { reinterpret_cast<adsk::core::referencecounted*>(ptr)->release(); } }; class completetype { public: template<typename t> static void addref(t* ptr) { ptr->addref(); } template<typename t> static void release(t* ptr) { ptr->release(); } }; template<class t, class pt = incompletetype> class ptr { public: typedef t element_type; ptr() : ptr_(nullptr) {} ptr(const ptr& rhs) : ptr_(nullptr) { reset(rhs.ptr_); } ptr(const t* ptr, bool attach = true) : ptr_(nullptr) { reset(ptr, attach); } // casting constructor. call operator bool verify if cast successful template<class v, class vpt> ptr(const ptr<v, vpt>& rhs) : ptr_(nullptr) { if (rhs) reset(rhs->template query<t>(), false); } ~ptr() { reset(nullptr); } void operator=(const ptr<t, pt>& rhs) { if (&rhs != this) reset(rhs.ptr_); } void operator=(const t* ptr) { reset(ptr, true); } // casting assignment operator. call operator bool verify if cast successful template<class v, class vpt> void operator=(const ptr<v, vpt>& rhs) { if (rhs) reset(rhs->template query<t>(), false); else reset(nullptr); } void reset(const t* ptr, bool attach = false) { if (ptr_ != ptr) { if (ptr_) pt::template release<t>(ptr_); ptr_ = const_cast<t*>(ptr); if (!attach && ptr_) pt::template addref<t>(ptr_); } } t* operator->() const { assert(ptr_ != nullptr); if (ptr_ == nullptr) throw std::exception(); return ptr_; } // test if pointer empty (if operator-> throw) /*explicit*/ operator bool() const { return ptr_ != nullptr; } bool operator==(const ptr& rhs) const { return ptr_ == rhs.ptr_; } bool operator!=(const ptr& rhs) const { return ptr_ != rhs.ptr_; } bool operator<(const ptr& rhs) const { return ptr_ < rhs.ptr_; } // iteration support. usable if t has count , item members , iterable_type typedef iterator<t, pt> iterator; iterator begin() const { return iterator<t, pt>(*this); } iterator end() const { return iterator<t, pt>(*this, true); } // caution following functions if used incorrectly can cause reference count leak t* get() const { return ptr_; } t* detach() { t* t = ptr_; ptr_ = nullptr; return t; } private: t* ptr_; };
you’re right ptr
wouldn’t needed in post-c++03 environment. plugin api old enough c++11 wasn’t around then. code posted best guess ptr
supposed reference counted smart pointer manages shared ownership std::shared_ptr
does.
how use thing should become clear plguin api documentation , maybe source code of host program. snippet posted , without mentioning program’s name it’s impossible definite.
Comments
Post a Comment