3

I started playing with VirtualTreeView and it seems from all the web info and documents that VirtualTreeView manages the memory of the user provided data (records if any). One does not have to New() and Dispose() any user data. One only has to tell VirtualTreeView the size of userdata and assign its field values after calling GetNodeData.

What if I wanted to manage that? Can it be done? Can I create the userdata as I want it and add it to my own lists and only provide a pointer of each record to VirtualTreeView without it interfering?

Blurry Sterk
  • 1,595
  • 2
  • 9
  • 18

2 Answers2

6

One only has to tell VirtualTreeView the size of userdata and assign its field values after calling GetNodeData

This is not entirely correct - one also has to take care finalizing the user data record should it contain data types which require it - ie reference counted types like string and interface. This is done in OnFreeNode event. So if your data record contains string

type
  TMyNodeData = record
    Foo: string;
  end;

then you have to assign empty string to it when node's data is freed:

procedure TForm1.VT_FreeNode(Sender: TBaseVirtualTree; Node: PVirtualNode);
var ND: PMyNodeData;
begin
  ND := Sender.GetNodeData(Node);
  if(ND <> nil)then begin
     ND.Foo := '';
  end;
end;

othervise you'll leak that string.

The siplest way to manage the memory yourself is not to use the VT's user data record at all, or only have a pointer to some other data structure in it.

ain
  • 22,394
  • 3
  • 54
  • 74
2

No you can not. VTW uses fixed size of user data thus it provides for fast calculations and access.

BUT you can request SizeOf(pointer) size of userdata and then store the pointer to your data frames in the userdata provided by VTW. Types of data at those frames and their sizes and their allocation/deallocation you would manage yourself. Still you would have to store pointers to it in VTW-provided userdata, but other than that you would not depend upon it.

In newer Delphi (2010+) you can also use TDictionary<Pointer, you-data-type> object, and retrieve the data using PVirtualNode given by VTW as a key into that container. That way you would be able to set UserData to have zero size, so VTW would only allocate nodes memory pool for its own needs.

But as far as possible you better user UserData - for it was specifically speed-optimized for you by VTW team.

Arioch 'The
  • 15,799
  • 35
  • 62