I am working on getting rid of shortstring. One of the many places shortstring is currently used within our programs is in records. Alot of these records are kept in AVL trees.
The AVL tree used is a generic one, holding a pointer to a number of bytes (ElemSize), which have worked well so far.
The memory for each record in the AVL tree is allocated with GetMem, and copied with Move.
However, with string being a pointer to a reference-counted structure, copying back the memory to a record no longer works, as the sting referenced is often freed (automatically by reference count).
With only a pointer and a size of the "data block", I assume it is not possible to have the reference count of the strings increased.
I'm looking for a way to get the reference count of the stings to be taken into account when storing the record in a AVL tree.
Can I pass the record type to the tree constructor, then cast the pointer to this type and thus get the references increased? Or a similar fix, where I can isolate the changes to primarily be in the AVL unit and calls to it's constructor.
Current code for allocation of space to store the record in AVL; XData is a pointer to the record to be stored:
New(RootPtr); { create new memory space }
GetMem(RootPtr^.TreeData, ElemSize);
WITH RootPtr^ DO BEGIN
{ copy data }
Move(XData^, RootPtr^.TreeData^, ElemSize);
stringwhich is autorefcounted, and the best approach would be to get rid of manual memory allocation for your records (eg New, GetMem) and use a new approach. I'd make nodes store object references personally. It's probably a lot more efficient than the memory moving you're currently doing too. - DavidTreeData(it contains the records data, but is not declared as a record). 2. The record retrieved from the tree is often used for temporary calculations, this should not be reflected in the proper record, thus we need to copy the record out each time it is asked for, rather than pass a reference. - MGH