Lazarus

Programming => General => Topic started by: egsuh on October 27, 2020, 06:45:39 am

Title: Cannot find reason
Post by: egsuh on October 27, 2020, 06:45:39 am
I have little bit strange problem.

0. I defined some class types, and its descendants.

     type
          TMyObject= class (TPersistent) ;
          TMyObj1 = class(TMyObject);
           .........

1. I instantiate several objects, and add them to TFPGList. (Or TList).

2. Then I link them to Treeview, by AddObject. So the objects are accessible via TTreeNode.Data. e.g.,
 
     MyObject := TreeView.Selected.Data;

3. My objects' properties are linked to RTTI controls, so that they are directly editable.

4. If I select a tree node, the Link of RTTI controls are changed to newly selected treenode's data. That is,

          MyObject  := TreeView.Selected.Data;
          RTTIControls.Link.TIObject := MyObject;

5. I can insert new Treenodes --- first create MyObject, then add it to the TFPGList, and linking to TreeNode's data.


In this way, I can edit many items by clicking treenode. No problem in insert and changeing selected items, and then freeing them later. (It is freed within TFPGList). 

Problem occurs when I try do destroy any object directly by deleting a TreeNode. Codes would be :

            NextFocusTreeNode := TreeView.Selected.GetNextSibling;
            MyObj1 := TreeView.Selected.Data;
            MyObj1.free;                          // <-------------- Error occurs  HERE
            TreeView.Selected.Delete;
            TreeView.Selected := NextFocusTreeNode;                       // OR
            MyObj1 := Treeview.Selected.Data;
            RTTIControls.Link.TiObject := MyObj1;    //   <----------- HERE


If I do not delete anything and terminate application, there are no problem. There are no heap memory leakages.

I don't know why. I had no problem with similar way in the past. I'm adding small changes continuously. So not sure what is possible causes. The most recent modification is some changes within the TMyObject methods and memory management. But if this is the cause, errors should occur in many cases --- not just at deletion. What can you think of? Could deleting TPersistent be problem?

//////////////////// ADDITION /////////////////////

If I do not Free MyObj1, and just delete TreeNode, the program runs fine, but there are unfreed memory blocks at termination.



Title: Re: Cannot find reason
Post by: egsuh on October 27, 2020, 07:05:43 am
I could find a go-around by creating a new FGPList for deleted items, adding all the deleted items there and freeing all of them later.
TinyPortal © 2005-2018