calling destructor..................................

hi,
   how can i call a destructor when the application gets closed. I have some global variables that are initialized in constructor and want those to release the memory when the application gets closed.  My coding is given below.


#include <afxwin.h>
#include  <stdio.h>
CRect r1;
CRect rcClient;
CDC dcMem;
CBitmap bmMem;
CBitmap* oldBitmap;

class CMainWnd : public CFrameWnd {
   public:
      CMainWnd() {
          Create(0, "Rectangle Change", WS_OVERLAPPEDWINDOW,
          rectDefault,NULL,NULL);
       r1.left   =  80;
       r1.top    =  80;
       r1.bottom =  210;
       r1.right  =  210;
          CPaintDC dc(this);
          dcMem.CreateCompatibleDC(&dc);
          bmMem.CreateCompatibleBitmap(&dc, rcClient.Width(), rcClient.Height());
          oldBitmap = dcMem.SelectObject(&bmMem);
       CBrush cb1(RGB(0,0,255));
          dcMem.FillRect(rcClient,&cb1); // .SetBkColor(RGB(100,100,100));
     }
    ~CMainWnd() {
      AfxMessageBox ("Inside mainwnd destructor");
      delete dcMem;
      delete oldBitmap;
     }        

     void OnPaint(){
       CPaintDC dc(this);  
       CBrush newbrush;
       CBrush* oldbrush;
       CPen newpen;
       CPen* oldpen;
       col = RGB(0,0,255);
       newpen.CreatePen (PS_SOLID,1,col);
       newbrush.CreateSolidBrush (col);
         dc.SetMapMode(MM_HIENGLISH);
         sizeWnd = dc.GetWindowExt();
         sizeView=dc.GetViewportExt();
         dc.SetMapMode(MM_ANISOTROPIC);
         dc.SetWindowExt(sizeWnd);
         sizeView.cx *=  100;
         sizeView.cy *= -100;
         dc.SetViewportExt(sizeView);
         dcMem.FillRect(rcClient, &brBackground);
         dcMem.Rectangle (r1.top,r1.left, r1.bottom,r1.right);
         BitBlt(dc,rcClient.left, rcClient.top, rcClient.Width(), rcClient.Height(), dcMem, 0, 0,
SRCCOPY);
      }
     DECLARE_MESSAGE_MAP()
};

class myframe : public CWinApp {
      public:
      BOOL InitInstance() {
            m_pMainWnd = new CMainWnd();
            m_pMainWnd->ShowWindow(SW_SHOWMAXIMIZED);
            m_pMainWnd->UpdateWindow();
            return  TRUE;
      }
};

BEGIN_MESSAGE_MAP(CMainWnd, CFrameWnd)
         ON_WM_ERASEBKGND()  
         ON_WM_PAINT()
END_MESSAGE_MAP()
myframe a;

Here how can i delete "bmMem" and "dcMem", "oldBitmap"?

Thanx.
tgersurAsked:
Who is Participating?
I wear a lot of hats...

"The solutions and answers provided on Experts Exchange have been extremely helpful to me over the last few years. I wear a lot of hats - Developer, Database Administrator, Help Desk, etc., so I know a lot of things but not a lot about one thing. Experts Exchange gives me answers from people who do know a lot about one thing, in a easy to use platform." -Todd S.

KurtVonCommented:
The destructor for a global object is called automatically when the program closes.

That said, you are really better off making those class variables.  The only reason the code given actually works is because there is only one CMainWnd.

Oh, and the destructors for class variables are called automatically after all code in the class destructor is executed.  You only need to delete pointers explicitly.
0
keitha1Commented:
Override WM_DESTROY on CMainWnd using class wizard. You will have a chance to release resources there.
0
aphillipsCommented:
Basically you should never call destructors.  Destructors for all objects will always be called unless the program calls abort() or there is something seriously wrong with your program.  In any case the C runtime library will release any memory allocated with malloc (and hence new) when the program exits.

However, you are right to be concerned about freeing Windows GDI handles as they will be lost unless you explicitly free them.  Luckilly the destructor for the MFC wrapper class (eg CBitMap) will free the them although you can explictly call DeleteObject().

So you shouldn't have to worry about calling destructors, freeing memory, or handles.

However, you *should* select the old GDI handle back into the DC before the window is destroyed.  You typically do this in the WM_CLOSE handler (OnClose in MFC) for the window where used.

void CMainWnd::OnClose()
{
    dcMem.SelectObject(oldBitmap);
...


0
Cloud Class® Course: Microsoft Exchange Server

The MCTS: Microsoft Exchange Server 2010 certification validates your skills in supporting the maintenance and administration of the Exchange servers in an enterprise environment. Learn everything you need to know with this course.

keitha1Commented:
WM_CLOSE can be dangerous because not all windows recieve this message. Use WM_DESTROY but do not do anything to the window recieving the WM_DESTROY. Just release you GDI resources.
0

Experts Exchange Solution brought to you by

Your issues matter to us.

Facing a tech roadblock? Get the help and guidance you need from experienced professionals who care. Ask your question anytime, anywhere, with no hassle.

Start your 7-day free trial
tgersurAuthor Commented:
Thanx keitha1. It is working.
0
aphillipsCommented:
> WM_CLOSE can be dangerous because not all windows recieve this message. Use WM_DESTROY ...

It is unusual for a window of this type not to get a WM_CLOSE event, but you can use WM_DESTROY instead.

The important points are:
1. You should not call a destructor
2. You do not need to release the resources as the CBitmap destructor will release them automatically.
0
keitha1Commented:
<You do not need to release the resources as the CBitmap destructor will release them automatically.>

But make sure the object is not currently selected in the DC. It can have undesirable effects like crashing.
0
It's more than this solution.Get answers and train to solve all your tech problems - anytime, anywhere.Try it for free Edge Out The Competitionfor your dream job with proven skills and certifications.Get started today Stand Outas the employee with proven skills.Start learning today for free Move Your Career Forwardwith certification training in the latest technologies.Start your trial today
System Programming

From novice to tech pro — start learning today.

Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.