Click here to Skip to main content
15,885,914 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
Hi Everyone..

Am implementing Garbage Collection in my project. But am not clarified when Memory will be de-allocated. My code looks like this:

Header File: Cmfc2Dlg.h

template <class T>class RefCount 
{
  protected:
  
  int refVal; 
  T* p;

  public:
  
  RefCount(){ refVal=0; p=(T*)this;}
  void AddRef() { refVal++; }
  void ReleaseRef() 
  { 
    refVal--;
    if(refVal == 0) 
      delete [] this; 
  } 

  T* operator->(void) { return p; } 
  
};

template <class T> class gcPtr
{
  T* ptr;
  char c;

  public:

  gcPtr()
  {
    c='0'; // called when variable declare // as gcPtr<foo> a;
  }
  gcPtr(T* ptrIn)
  {
    ptr=ptrIn;
    // called when variable declared
    // as gcPtr<foo> a=new foo;
    ptr->AddRef();
    c='1';
  }
  // assuming we have variable gcPtr<foo> x
  operator T*(void) { return ptr; } //for x[]
  T& operator*(void) { return *ptr; } // for *x type operations
  T* operator->(void){ return ptr; } // for x-> type operations
  gcPtr& operator=(gcPtr<T> &pIn)
  // for x=y where y is also gcPtr<foo> object
  {
    return operator=((T *) pIn);
  }
  gcPtr& operator=(T* pIn)
  {
    if(c=='1')
    // called by gcPtr& operator=(gcPtr<T>&pIn) in case of
    { // assignment
      // Decrease refcount for left hand side operand
      ptr->ReleaseRef();
      // of ‘=’ operator
      ptr = pIn;
      pIn->AddRef(); // Increase reference count for the Right Hand
      // operand of ‘=’ operator
      return *this;
    }
    else
    // if c=0 i.e variable was not allocated memory when // it was declared
    { // like gcPtr<foo> x. in this case we //allocate memory using new
      // operator, this will be called
      ptr=pIn;
      ptr->AddRef();
      return *this
    }
  }
  ~gcPtr(void) { ptr->ReleaseRef(); } // Decrement the refcount
};

class Cmfc2Dlg : public CDialog,RefCount<Cmfc2Dlg>
{
 public:
   void Take_Screenshot();
   //other function declarations..
}


C++
//Source file: Cmfc2Dlg.cpp
void Cmfc2Dlg :: OnBnClickedOK()
{
  while(1)
  {
//Am running Take_Screenshot function forever: 
    gcPtr <cmfc2Dlg> obj = new Cmfc2Dlg();
    obj->Take_Screenshot();
  }
}


Since Am running Take_Screenshot() function for long hours, I need to take care of CPU Memory/Resources. While running continuously, memory should not goes on increasing . After executing the Take_Screenshot() function, its corresponding memory should deallocate.
Here, Memory will be deallocated after executing obj->Take_Screenshot(); or should i include delete[] obj;
Please suggest me..

Thank you all..
Posted

1 solution

It is not a "real" GC, it is just a smart pointer, like the shared_ptr from C++/tr1 extension.

So, when the counter is released to zero, the memory will be revoked.

The problem is -- your code list above is not thread safe, try to use InterlockedIncrement /InterlockedDecrement instead.
 
Share this answer
 
Comments
Guru_C++ 29-Sep-12 6:03am    
Thanks Wang.. I was not knowing it was not safe.. Can u suggest me How to use InterlockedIncrement /InterlockedDecrement ??
Guru_C++ 29-Sep-12 6:10am    
Is this Correct..?? What am using here ?
void AddRef()
{
InterlockedIncrement (&refVal);
}
void ReleaseRef()
{
InterlockedDecrement (&refVal);
if(refVal == 0)
delete [] this;
}
Jerry.Wang 29-Sep-12 6:14am    
InterlockedDecrement returns the result value, so you should do like this:

void ReleaseRef()
{
if(InterlockedDecrement (&refVal)== 0)
delete [] this;
}

Espen Harlinn 29-Sep-12 6:24am    
delete [] this; delete this;
OP should not use array delete here.
Jerry.Wang 29-Sep-12 6:25am    
yes, I made a mistake, just copied the code above :)

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900