?
Solved

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

Posted on 2003-03-18
7
Medium Priority
?
515 Views
Last Modified: 2013-11-20
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.
0
Comment
Question by:tgersur
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
7 Comments
 
LVL 11

Expert Comment

by:KurtVon
ID: 8159205
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
 
LVL 1

Expert Comment

by:keitha1
ID: 8161081
Override WM_DESTROY on CMainWnd using class wizard. You will have a chance to release resources there.
0
 
LVL 3

Expert Comment

by:aphillips
ID: 8164824
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
Learn how to optimize MySQL for your business need

With the increasing importance of apps & networks in both business & personal interconnections, perfor. has become one of the key metrics of successful communication. This ebook is a hands-on business-case-driven guide to understanding MySQL query parameter tuning & database perf

 
LVL 1

Accepted Solution

by:
keitha1 earned 60 total points
ID: 8166983
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
 

Author Comment

by:tgersur
ID: 8210404
Thanx keitha1. It is working.
0
 
LVL 3

Expert Comment

by:aphillips
ID: 8212630
> 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
 
LVL 1

Expert Comment

by:keitha1
ID: 8212868
<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

Featured Post

Will your db performance match your db growth?

In Percona’s white paper “Performance at Scale: Keeping Your Database on Its Toes,” we take a high-level approach to what you need to think about when planning for database scalability.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Introduction: Hints for the grid button.  Nested classes, templated collections.  Squash that darned bug! Continuing from the sixth article about sudoku.   Open the project in visual studio. First we will finish with the SUD_SETVALUE messa…
Introduction: Dialogs (1) modal - maintaining the database. Continuing from the ninth article about sudoku.   You might have heard of modal and modeless dialogs.  Here with this Sudoku application will we use one of each type: a modal dialog …
This video will show you how to get GIT to work in Eclipse.   It will walk you through how to install the EGit plugin in eclipse and how to checkout an existing repository.
This tutorial will teach you the special effect of super speed similar to the fictional character Wally West aka "The Flash" After Shake : http://www.videocopilot.net/presets/after_shake/ All lightning effects with instructions : http://www.mediaf…

777 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question