Click here to Skip to main content
Rate this: bad
good
Please Sign up or sign in to vote.
See more: C++ MFC
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..
}
 
 
//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 29-Sep-12 0:14am
Guru_C++1.2K

1 solution

Rate this: bad
good
Please Sign up or sign in to vote.

Solution 1

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.
  Permalink  
Comments
Guru_C++ at 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++ at 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 at 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 at 29-Sep-12 6:24am
   
delete [] this; delete this;
OP should not use array delete here.
Jerry.Wang at 29-Sep-12 6:25am
   
yes, I made a mistake, just copied the code above :)
Espen Harlinn at 29-Sep-12 6:32am
   
Assumed as much, just thought I should mention it.
 
Anyway, OPs' smart pointer will currently crash and burn in so many ways that he should drop the idea and just use shared_ptr ...
Jerry.Wang at 29-Sep-12 6:16am
   
For InterlockedIncrement, you need check the result as well.
If InterlockedIncrement returns 1 for the 2nd time, that means the memory is already released.
Espen Harlinn at 29-Sep-12 6:22am
   
?? If memory has been released there is no way of knowing how the application will behave.
Espen Harlinn at 29-Sep-12 6:18am
   
No, InterlockedDecrement has a return value and you should that value when you test whether the reference count is 0
Espen Harlinn at 29-Sep-12 6:26am
   
5'ed, using shared_ptr would save OP from a lot of pain ...
Guru_C++ at 29-Sep-12 6:38am
   
Thanks Wang.. But am facing a problem over here.. In Take_Screenshot() 'm just displaying a message box for testing purpose. But 'm unable to run it continuously. I mean only one time i used to get the message box, next time when control comes to gcPtr obj = new Cmfc2Dlg(); I used to get the error called:
 
Windows has triggered a breakpoint in mfc2.exe.
This may be due to a corruption of the heap, which indicates a bug in mfc2.exe or any of the DLLs it has loaded.
This may also be due to the user pressing F12 while mfc2.exe has focus.
The output window may have more diagnostic information.
 
Is there any problem with this code ?

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

  Print Answers RSS
0 Sergey Alexandrovich Kryukov 390
1 OriginalGriff 205
2 Zoltán Zörgő 124
3 Dave Kreskowiak 120
4 BillWoodruff 110
0 OriginalGriff 7,610
1 DamithSL 5,559
2 Sergey Alexandrovich Kryukov 5,324
3 Maciej Los 4,961
4 Kornfeld Eliyahu Peter 4,539


Advertise | Privacy | Mobile
Web02 | 2.8.141223.1 | Last Updated 29 Sep 2012
Copyright © CodeProject, 1999-2014
All Rights Reserved. Terms of Service
Layout: fixed | fluid

CodeProject, 503-250 Ferrand Drive Toronto Ontario, M3C 3G8 Canada +1 416-849-8900 x 100