Recent

Author Topic: AVLTree  (Read 1888 times)

Amir61

  • New Member
  • *
  • Posts: 45
    • http://Amir.Aavani.net
AVLTree
« on: May 04, 2025, 07:10:15 am »
Hi there,

 What is the role of TAVLTreeNodeMemManager in the avl_tree.pp and how I can disable it (should I disable it)?


Amir61

  • New Member
  • *
  • Posts: 45
    • http://Amir.Aavani.net
Re: AVLTree
« Reply #1 on: May 04, 2025, 07:32:18 am »
To give more context, I have a code that creates several AVLTrees, and my code fails when I am freeing my AvlTrees (because TAVLTreeNodeMemManager.DisposeNode says "'%s.DisposeNode: FCount (%d) is negative. Should not happen.'").
But when I add, `AVL_Tree.NodeMemManager := nil;` (disabling the NodeManager), the code runs fine.

cdbc

  • Hero Member
  • *****
  • Posts: 2561
    • http://www.cdbc.dk
Re: AVLTree
« Reply #2 on: May 04, 2025, 08:03:20 am »
Hi
I would leave it in and learn how to use it.
It helps your application's speed and prevents 'Heap-Shredding' (a lot of small allocs & deallocs of mem).
Don't free the nodes, but just return them to the nodemanager, it will dispose of them in the end.
Regards Benny
If it ain't broke, don't fix it ;)
PCLinuxOS(rolling release) 64bit -> KDE6/QT6 -> FPC Release -> Lazarus Release &  FPC Main -> Lazarus Main

marcov

  • Administrator
  • Hero Member
  • *
  • Posts: 12593
  • FPC developer.
Re: AVLTree
« Reply #3 on: May 04, 2025, 01:27:34 pm »
The nodememorymanager is a cache for nodes, mass allocating and administrating them.  It has been in since nearly forever.

Probably your problems stem from a bug in your code (like passing nodes from one avltree to another, crossing nodememorymanager domains). It might be wise to try to fix the bug rather than circumventing it by disabling nodemanager.

Amir61

  • New Member
  • *
  • Posts: 45
    • http://Amir.Aavani.net
Re: AVLTree
« Reply #4 on: May 05, 2025, 06:28:05 am »
You are right I was able to fix the bug. But still curious about what TAVLTreeNodeMemManager whether I should disable it or not:
I use the AVLTree to create an inverted index and then store the sorted key, values on file. I do not have any delete/dispose operation on the nodes (before end of the program).

Thank you,

cdbc

  • Hero Member
  • *****
  • Posts: 2561
    • http://www.cdbc.dk
Re: AVLTree
« Reply #5 on: May 05, 2025, 07:57:53 am »
Hi
To summarize marco's and my posts:
"The nodememorymanager is a cache for nodes, mass allocating and administrating them.  It has been in since nearly forever."
+
"It helps your application's speed and prevents 'Heap-Shredding' (a lot of small allocs & deallocs of mem)."

Just leave it ENABLED and let it do its job!
Regards Benny
If it ain't broke, don't fix it ;)
PCLinuxOS(rolling release) 64bit -> KDE6/QT6 -> FPC Release -> Lazarus Release &  FPC Main -> Lazarus Main

scribly

  • Jr. Member
  • **
  • Posts: 82
Re: AVLTree
« Reply #6 on: September 25, 2025, 02:30:52 am »
Small bump, but just wanted to point out that the default TAVLTreeNodeMemManager is not thread safe (at least not in 4.2).  So if you where to clear out a tree in another thread you'll get some weird errors when at the same time some GUI controls resize(e.g update a label showing progress of deletion), as they also make use of AVLTree. 

This also affects TMap controls and similar as those also rely on TAVLTree.
So make sure you assign a custom NodeMemManager each time you make a new tree in another thread (Can be iffy with TMap )


 

TinyPortal © 2005-2018