Click here to Skip to main content
12,895,135 members (50,230 online)
Rate this:
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 
  int refVal; 
  T* p;
  RefCount(){ refVal=0; p=(T*)this;}
  void AddRef() { refVal++; }
  void ReleaseRef() 
    if(refVal == 0) 
      delete [] this; 
  T* operator->(void) { return p; } 
template <class T> class gcPtr
  T* ptr;
  char c;
    c='0'; // called when variable declare // as gcPtr<foo> a;
  gcPtr(T* ptrIn)
    // called when variable declared
    // as gcPtr<foo> a=new foo;
  // 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)
    // called by gcPtr& operator=(gcPtr<T>&pIn) in case of
    { // assignment
      // Decrease refcount for left hand side operand
      // of ‘=’ operator
      ptr = pIn;
      pIn->AddRef(); // Increase reference count for the Right Hand
      // operand of ‘=’ operator
      return *this;
    // 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
      return *this
  ~gcPtr(void) { ptr->ReleaseRef(); } // Decrement the refcount
class Cmfc2Dlg : public CDialog,RefCount<Cmfc2Dlg>
   void Take_Screenshot();
   //other function declarations..

//Source file: Cmfc2Dlg.cpp
void Cmfc2Dlg :: OnBnClickedOK()
//Am running Take_Screenshot function forever: 
    gcPtr <cmfc2Dlg> obj = new Cmfc2Dlg();

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 28-Sep-12 23:14pm

1 solution

Rate this: bad
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.
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;

Jerry.Wang 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 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 29-Sep-12 6:22am
?? If memory has been released there is no way of knowing how the application will behave.
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 :)
Espen Harlinn 29-Sep-12 6:26am
5'ed, using shared_ptr would save OP from a lot of pain ...
Espen Harlinn 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 ...
Guru_C++ 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<cmfc2dlg> 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
Top Experts
Last 24hrsThis month

Advertise | Privacy | Mobile
Web01 | 2.8.170424.1 | Last Updated 29 Sep 2012
Copyright © CodeProject, 1999-2017
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