Mike,
I was kind of thinking of it from a couple of different angles:
1. Keeping code concise, i.e. avoiding having to declare another handle, track it and send destroy to it. And in a more complicated situation, where many and potentially a variable number of dynamic objects are created requiring stuffing them in an array anyway, it might actually be cleaner to leave the object tracking/destruction up to some generic code.
2. Reducing the cognitive load on the programmer/maintainer.
If I ever were to use this it might be in the form of a class like this:
Code:
Class cDynamicObjectScope is a cObject
Procedure Construct_object
Forward Send Construct_Object
Property Handle[] phoObjects
End_Procedure
Procedure DoAddObjectToScope Handle hoObject
Handle[] ahoObjects
Get phoObjects to ahoObjects
Move hoObject to ahoObjects[SizeOfArray(ahoObjects)]
Set phoObjects to ahoObjects
End_Procedure
Function AddObjectToScope Handle hoObject returns handle
Send DoAddObjectToScope hoObject
Function_Return hoHandle
End_Function
Function Create Integer iClassId Returns Handle
Handle hoObject
Forward Get Create iClassId to hoObject
Send DoAddObjectToScope hoObject
Function_Return hoObject
End_Function
Procedure end_construct_object
Handle[] ahoObjects
Integer i iCnt
Forward Send End_Construct_Object
Get phoObjects to ahoObjects
Move (SizeOfArray(ahoObjects)-1) to iCnt
For i from 0 to iCnt
Send Destroy to ahoObjects[i]
Loop
Send Destroy
End_Procedure
End_Class
// usage:
Procedure DoSomething
...
// do some stuff
object oObjects is a cDynamicObjectScope
...
// code in here creates dynamic objects with "get Create (RefClass(class))"
...
end_object
// dynamic objects are automatically destroyed at this point
// as is the oObjects object
...
// do more stuff
...
end_procedure
This doesn't handle cases like cJsonObject that internally creates a new DataFlex object; you would still have to get the handle to a local variable and call DoAddObjectToScope to handle it with this class, or wrap AddObjectToScope around the Member(hoJson,"foo")