Page 1 of 1

Properly destroying components created with set_ref

Posted: Thu Jun 01, 2017 12:06 am
by jyoung
Is there a "proper" way to destroy components that were created with set_ref?

I am managing the lifetime of objects that inherit from CMGRPTReportList where the object is defined like:

Code: Select all

define_com class(#CMGRPTReportList) name(#ReportList) reference(*DYNAMIC)
uTerminate and uInitialize are mthroutines defined in CMGRPTReportList that provides the object a place to get initialized and clean up before getting killed.

When I create a new object and kill the current object I am doing this: (STD_TEXT contains the identifier of the object to be created)

Code: Select all

#STD_TEXT := #ReportListMap<#reportId>

if (#ReportList *IsNot *NULL)
#ReportList.uTerminate
set_ref com(#ReportList) to(*NULL)
endif

set_ref com(#ReportList) to(*CREATE_FROM #STD_TEXT)

if (#ReportList *IsNot *NULL)
#ListLayoutItem.Manage <= #ReportList
#ListLayoutItem.Sizing := FitBoth
#ReportList.Parent <= #ListPanel
#ReportList.uInitialize
endif
Is setting the object to *NULL the correct way to handle destroying the object?

Re: Properly destroying components created with set_ref

Posted: Thu Jun 01, 2017 7:56 am
by MarkD
That looks good to me.
If there are no references to #ReportList left anywhere it should get destroyed.
You can make sure by testing with a temporary #Com_Owner.DestroyInstance event handler in #ReportList with a message box.
If that event fires then you are sure that it got destroyed.

Re: Properly destroying components created with set_ref

Posted: Thu Jun 01, 2017 8:16 am
by MarkD

Re: Properly destroying components created with set_ref

Posted: Thu Jun 01, 2017 8:48 am
by Stewart Marshall
Here's a more in depth look at resource handling in VL.

Regards

Re: Properly destroying components created with set_ref

Posted: Thu Jun 01, 2017 10:27 am
by Stewart Marshall
Source code for the examples in the previous document

Regards