templates - Visual C++ Debug Assertion Failed? -


in code below implemented smart pointer executes fine, @ end following message:

enter image description here

here code:

smart_ptr.h:

#ifndef smart_ptr_h #define smart_ptr_h  /*     class: auto_ptr      implements generic     smart pointer doesn't need     deleted explicitly, i.e.     provides garbage collection. */ template<class t> class auto_ptr{ public:     // constructors     explicit auto_ptr(t* p = nullptr): value(p) { };    // constructor     auto_ptr(auto_ptr& p);                      // copy constructor     auto_ptr& operator= (const auto_ptr& p);            // copy assignment     ~auto_ptr() { std::cout << "pointer deleted\n"; delete value; }                     // destructor      // access operators     const t& operator* () const { return *value; }      // dereference operator     const t* operator->() const { return value; }       // indirect class member access (arrow) operator      // non-modifying members     t* get() { return value; }                          // getter method     void reset(t* v);                                   // reassing new value(default value: nullptr)     t* release();                                       // transfers object pointer; without destroying private:     // data member     t* value;  };  //-------------------------------------------------------------------------------------------------------- // class auto_ptr member implementations // constructors // copy constructor template<class t> auto_ptr<t>::auto_ptr(auto_ptr& p) {     value = p.release(); }  // copy assignment template<class t> auto_ptr<t>& auto_ptr<t>::operator= (const auto_ptr& p ) {     if (this == &p) return *this;     if (value) delete value;     value = p.value;     return *this; }  /*     function: release()     use: t ptr =  auto_ptr_obj.release();      transfers pointer value     caller, setting data member value     nullptr. */ template <class t> t* auto_ptr<t>::release() {     t* temp = value;     value = nullptr;     return temp; }  /*     function: reset()     use: auto_ptr_obj.release(new_pointer);      deletes object pointer     pointer value , assings new_pointer;  */ template <class t> void auto_ptr<t>::reset(t* v) {     delete value;     value = v; }     #endif 

main.cpp:

#include <iostream> #include "smart_ptr.h" #include "assert.h"  //===================================================================== void test1 () {     std::cout <<"\ntest constructor , get() member.\n";     auto_ptr<int> p(new int);     *p.get() = 5;     std::cout <<"p points to: "<< *p << "\n";      //assert(*p, 5);     std::cout <<"test 1 done\n"; }  void test2 () {     std::cout <<"\ntest reset() , release() members.\n";     auto_ptr<int> p(new int);     *p.get() = 5;     std::cout <<"p points to: "<< *p << "\n";      p.reset(new int(10));     std::cout <<"reset() p points to: "<< *p << "\n";     //assert(*p, 10);      int *temp = p.release();     std::cout <<"caller of release(), temp points to: "<< *temp << "\n";     //assert(*temp, 10);      // nullptr dereferece error      // std::cout <<"p after being release()d points to: "<< *p << "\n";      std::cout <<"test 2 done\n"; }  void test3 () {     std::cout <<"\ntest copy constructor , copy assignment.\n";     auto_ptr<int> p1(new int(10));     auto_ptr<int> p2(p1);      std::cout <<"copy constructed p2 points to: "<< *p2 << "\n";     //assert(*p2, 10);      auto_ptr<int> p3(new int(20));     p1 = p3;     std::cout <<"copy assigned p1 points to: "<< *p1 << "\n";     //assert(*p1, 20);      std::cout <<"test 3 done\n"; }  //===================================================================== int main () {     test1 ();     test2 ();     test3 ();      getchar(); } 

the interesting note in live example error in not reproducible.

what cause of message ?


this result of "double delete" - in case suspect copy assignment.

you assign value = p.value; , both copies hold same value , both attempt delete it.

you need move pointer 1 object other, case copy constructor. need remove const well.


Comments

Popular posts from this blog

php - Wordpress website dashboard page or post editor content is not showing but front end data is showing properly -

javascript - Get parameter of GET request -

javascript - Twitter Bootstrap - how to add some more margin between tooltip popup and element -