Want to protect your cyber security and still get fast solutions? Ask a secure question today.Go Premium

x
  • Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 534
  • Last Modified:

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.
0
tgersur
Asked:
tgersur
1 Solution
 
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
Concerto's Cloud Advisory Services

Want to avoid the missteps to gaining all the benefits of the cloud? Learn more about the different assessment options from our Cloud Advisory team.

 
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
 
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

Featured Post

VIDEO: THE CONCERTO CLOUD FOR HEALTHCARE

Modern healthcare requires a modern cloud. View this brief video to understand how the Concerto Cloud for Healthcare can help your organization.

Tackle projects and never again get stuck behind a technical roadblock.
Join Now