c++ - Double free or corruption, why does this appear? -


i've finished smart pointer (i know...) uploaded univerity's website runs number of automated tests on code. there 2 things wrong number of tests:

  • memory or time limit exceeded
  • memory access problem (ie. null pointer)

the thing don't know kind of tests being excecuted. able read automated tests' stdout did, written there:

in instantiation of ‘my_pointer<t>::my_pointer() [with t = tester]’: error: no matching function call ‘tester::tester()’ note: candidates are: tester::tester(my_pointer<tester>) candidate expects 1 argument, 0 provided tester::tester(const tester&) candidate expects 1 argument, 0 provided 

so i'm guessing odd reason won't invoke my_pointer() constructor? smart pointer class:

template<class t> class my_pointer {     t* raw_pointer;  public:     my_pointer() {         raw_pointer = new t();         raw_pointer->increfcnt();     }      my_pointer(t *obj) : raw_pointer(obj) {         if(raw_pointer != null) raw_pointer->increfcnt();     }      my_pointer(const my_pointer<t>& smart_pointer) : raw_pointer(smart_pointer.raw_pointer) {         if(raw_pointer != null) raw_pointer->increfcnt();     }      t& operator*() {         return *raw_pointer;     }      t* operator->() {         return raw_pointer;     }      operator t*() {         return raw_pointer;     }      my_pointer<t> &operator=(const my_pointer<t> &smart_pointer) {         if(this != &smart_pointer && raw_pointer != null) {             /** if last reference given memory address */             if (raw_pointer->decrefcnt() == 0) {             delete raw_pointer;                                 }              raw_pointer = smart_pointer.raw_pointer;             raw_pointer->increfcnt();         }          return *this;     }      bool operator== (const t* pointer) {         return raw_pointer == pointer;     }      bool operator!= (const t* pointer) {         return raw_pointer != pointer;     }      bool operator== (const my_pointer<t> &smart_pointer) {         return raw_pointer == smart_pointer.raw_pointer;     }      bool operator!= (const my_pointer<t> &smart_pointer) {         return raw_pointer != smart_pointer.raw_pointer;     }      ~my_pointer() {        if(raw_pointer->decrefcnt() == 0 && raw_pointer != null) {            delete raw_pointer;         }     } }; 

this class, in references can counted:

class refcounted { private:     int count; public:     refcounted() : count(0) { }      int increfcnt() {         return ++count;     }      int decrefcnt() {         return --count;     } }; 

can see problems code? in advance!

you unconditionally deleting raw pointer in destructor. wrong. must decrement reference count, , delete if becomes zero.

you call raw_pointer->increfcnt(); in several places without checking whether raw_pointer null.


Comments

Popular posts from this blog

javascript - Slick Slider width recalculation -

jsf - PrimeFaces Datatable - What is f:facet actually doing? -

angular2 services - Angular 2 RC 4 Http post not firing -