[ create a new paste ] login | about

Link: http://codepad.org/USIPjPHJ    [ raw code | output | fork ]

C++, pasted on Jul 6:
#include <vector>
using std::vector;

// This along with "delete ref_count;" will cause a crash
vector<int> mUnitTestClasses;

template<class T>
class strong_ref {
public:
   explicit strong_ref(T* ptr = 0) : weak_ref_count(new size_t()), ref_count(new size_t()), ptr(ptr) {
      if(ptr) {
         *ref_count = 1;
      }
   }

   strong_ref(strong_ref<T> const& other) : ptr(other.ptr), ref_count(other.ref_count), weak_ref_count(other.weak_ref_count) {
      ++(*ref_count);
   }

   ~strong_ref() {
      if(*ref_count) {
         if(--(*ref_count) == 0) {
            delete ptr;
            if(!*weak_ref_count) {
               delete ref_count; // With this line here, there will be a heap crash
               delete weak_ref_count;
            }
         }
      }
      else
      {
         if(!*weak_ref_count) {
            delete ref_count;
            delete weak_ref_count;
         }
      }
   }

private:
   T* ptr;
   size_t* ref_count;
   size_t* weak_ref_count;
};

int main(void)
{
   // This causing an exception to be thrown ...
   strong_ref<int> reg1;
   strong_ref<int> copy1(reg1);

   return 0;
};


Output:
1
2
Line 52: error: extra ';'
compilation terminated due to -Wfatal-errors.


Create a new paste based on this one


Comments: