15 мая 2023 года "Исходники.РУ" отмечают своё 23-летие!
Поздравляем всех причастных и неравнодушных с этим событием!
И огромное спасибо всем, кто был и остаётся с нами все эти годы!

Главная Форум Журнал Wiki DRKB Discuz!ML Помощь проекту


Problem in the destructor of a class derived from CTreeView

Anujit Sarkar -- anujit@vedika.ernet.in
Wednesday, July 03, 1996

Environment:Win95,MSVC 4.1

I have derived a class DerivedTreeView from CTreeView.I am
inserting treeitems in which I am storing a pointer to a structure
in lParam of TV_ITEM .The pointer is being allocated at runtime and
should be freed to avoid memory leakage.So I tried to deallocate
the storage in the destructor of DerivedTreeView.But unfortunately
I found that the GetTreeCtrl() is not returning a valid TreeCtrl.It
seems that the destructor of DerivedTreeView is being called after
the destructor of CTreeView has been called.In other words the
destructor of a derived class is being called after the invocation
of the destructor of the base class.Is this a bug ? Bad
implementation?How to get around the problem.

Thanks in advance.

-------------------------------------------------------------------
Anujit Sarkar(e-mail address:anujit@vedika.ernet.in)
-------------------------------------------------------------------
Resi: 102 Southern Avenue        Office:Vedika International
      'Sarobar' 7  N.E.                 21 Lansdowne Court
      Calcutta -700029                  5B Sarat Bose Road
      INDIA                             Calcutta-700020 INDIA
      Phone No.+91-33-4661473           Phone No.+91-33-2473810
-------------------------------------------------------------------



Klaus Guetter -- 100031.1400@CompuServe.COM
Monday, July 08, 1996

[Mini-digest: 6 responses]

Anujit,

Probably you should add a WM_DESTROY handler to your derived class and free the
resources in this function.

- Klaus

-----From: "K.V.R.M.Rao" 

Windows' "tree control" attached to the CTreeView is already destroyed 
by the time destructor for the CTreeView is called. All the pointers 
for dynamically allocated memory assosiated with nodes are stored 
within the "tree control". You should free all the memory in the 
"OnDestroy" handler of CTreeView derived class.

Rammohan
Optima Software Services,
#503, Maitrivanam, HUDA complex,
Ameerpet,
Hyderabad - 500 038
INDIA
-----From: joew@statsoftinc.com (Joe Willcoxson)

Create an OnDestroy handler, and clean up your memory there.  However, be
careful, since you must make sure that the Windows Window is destroyed
before your C++ object.  Otherwise, your message map to your OnDestroy
handler will be deleted and won't ever get called.  One way to do this is to
call DestroyWindow in your DerivedTreeView destructor.
Joe Willcoxson (joew@statsoftinc.com)
http://www.statsoftinc.com
http://users.aol.com/chinajoe


-----From: Mark Koehler 

I have found that this sort of clean-up is usually best handled in response
to the WM_DESTROY message for the appropriate window. In this case, it be in
the class derived from CTreeView. Make certain you perform the cleanup
before calling the inherited OnDestroy. You can't do this sort of cleanup in
the destructor because the window handle is no longer valid.

--
Mark Koehler
Atlanta, GA
-----From: Bharat Gogia 

In such cases you should always use OnClose() to free the resources
associated with window


-----From: Zafir Anjum 


The windows are destroyed much before the C++ object destructor is called.
Override OnClose() and deallocate memory there.

Zafir




Gordon Weakliem -- gweakl@metronet.com
Wednesday, July 10, 1996

At 10:25 AM 7/3/96 IST, you wrote:
>So I tried to deallocate the storage in the destructor of
DerivedTreeView.But >unfortunately I found that the GetTreeCtrl() is not
returning a valid >TreeCtrl.It seems that the destructor of DerivedTreeView
is being called after
>the destructor of CTreeView has been called.In other words the
>destructor of a derived class is being called after the invocation
>of the destructor of the base class.Is this a bug ? Bad
>implementation?How to get around the problem.

MFC CWnd derived classes create the window object in Create() and destroy it
in OnDestroy().  You should override OnDestroy() to destroy things like
image lists, etc.
Gordon Weakliem
gweakl@metronet.com




Gordon Weakliem -- gweakl@metronet.com
Wednesday, July 10, 1996

At 10:25 AM 7/3/96 IST, you wrote:
>So I tried to deallocate the storage in the destructor of
DerivedTreeView.But >unfortunately I found that the GetTreeCtrl() is not
returning a valid >TreeCtrl.It seems that the destructor of DerivedTreeView
is being called after
>the destructor of CTreeView has been called.In other words the
>destructor of a derived class is being called after the invocation
>of the destructor of the base class.Is this a bug ? Bad
>implementation?How to get around the problem.

MFC CWnd derived classes create the window object in Create() and destroy it
in OnDestroy().  You should override OnDestroy() to destroy things like
image lists, etc.
Gordon Weakliem
gweakl@metronet.com





| Вернуться в корень Архива |