Watch, Follow, &
Connect with Us
Public Report
Report From: Kylix/VCLX/TDirectoryTreeView    [ Add a report in this area ]  
Report #:  4574   Status: Open
Big memory leak in TDirectoryTreeView.SetRootDir
Project:  Kylix Build #:  4.186
Version:    3.0 Submitted By:   Andreas Hausladen
Report Type:  Minor failure / Design problem Date Reported:  5/26/2003 10:48:39 AM
Severity:    Commonly encountered problem Last Updated: 3/20/2012 2:24:39 AM
Platform:    All platforms Internal Tracking #:   166802
Resolution: None  Resolved in Build: : None
Duplicate of:  None
Voting and Rating
Overall Rating: No Ratings Yet
0.00 out of 5
Total Votes: None
Description
There is a small memory leak in DirectoryTreeView.SetRootDir.
It allocates memory for a TFileInfo record for the first node. This reference is lost on a Items.Clear. The destructor uses the right code to free the PFileInfo reference but in SetRootDir this code is missing.

Because the Loaded method calls SetRootDir twice (direct and indirect) you do not need to change RootDir on execution for detecting the memory leak.
Steps to Reproduce:
1. In QFileCtrls see (c&p from the K3 source)

procedure TDirectoryTreeView.SetRootDir(const Value: WideString);
[...]
try
     Items.Clear;

      RootNode := Items.Add(nil, Tmp);
      RootNode.Data := AllocFileInfo(SR);
      RootNode.ImageIndex := 0;

Items.Clear does, of course, not free the fileinfo allocated earlier.
Workarounds
Copy the code from TDirectoryTreeView.Destroy into SetRootDir:

...
try
  if (Items.Count > 0) and (Items[0].Data <> nil) then   // inserted
    Dispose(PFileInfo(Items[0].Data));  // inserted
  Items.Clear;

  RootNode := Items.Add...
Attachment
None
Comments

None

Server Response from: ETNACODE01