I am using the TImage class in C++ to grab an image of a pad (using FromPad()). I then also use the GetArgbArray() method to get the bitmap’s ARGB pixel array. However, I am unsure about how to clean up the TImage after using it.
The code basically looks like this:
TObject *aClone
TImage *imgH = TImage::Create();
while()
{
aClone = aPad1->Clone(); // Clone from a pad
aPadClone = (TPad*)aClone;
aPadClone->SetPad(0,0,1,1);
aPad2->cd();
aPadClone->Draw(); // Draw the clone
imgH->FromPad(aPadClone); // Get image
imgH->Mirror();
UInt_t *histImage1RGBA; // Get image's ARGB pixels
histImage1RGBA = imgH->GetArgbArray();
/*
Use the array histImage1RGBA
*/
delete aClone;
}
This code is called repeatedly. I’ve included a script that almost does what I got in C++. In both implementations (C++ and the script) using ROOT 5.25 under Windows XP, and Window’s PerfMon using Private Bytes of the processes, the memory allocation keeps creeping up.
When I debugged the code, it is the memory allocation in FromPad() and GetArgbArray() methods that is not being released.
When I looked at the code for the FromPad() method - I see a call to the private method DestroyImage(), so I assumed that the FromPad() would destroy/delete the previous memory allocated to an image and it’s associated arrays (such as the ARGB array).
Is there something I need to add to explicitly delete the image’s allocated memory? I even tried to SetImage() to a blank array, but that still had memory leaks. TestImage2.C (1.06 KB)
Sorry about that I have added a h->Reset() before that statement, but I’ve posted the wrong version of the script.
Either way, I still see a memory leak. Even with the h->FillRandom() commented out.
I’ve attached a snapshot of PerfMon showing private bytes for the root process. This is with the FillRandom() commented out, and I’ve increased the delay to 2 seconds to show the memory allocation steps more clearly.
In the C++ program, it simply runs out of memory and then crashes.
Well, I’m not sure about the Mac. I’m using the Win32 libraries on Windows XP, and I’ve narrowed the memory leak down to the TImage FromPad() and GetArgbArray().
Can you confirm that the code (as represented in the script above) uses TImage correctly? As in, there are no other explicit ways to clean up after using TImage?
I ran the script you provided on a Windows XP with 3GB of physical RAM, and after a couple of minutes I got this in CINT:
ROOT 5.25/02 (trunk@30530, Sep 29 2009, 15:28:19 on win32)
CINT/ROOT C/C++ Interpreter version 5.17.00, Dec 21, 2008
Type ? for help. Commands must be C++ statements.
Enclose multiple statements between { }.
root [0] .x TestImage2.C
Exception: bad allocation
Error: Symbol G__exception is not defined in current scope C:\TestImage\TestImage2.C(23)
Error: type G__exception not defined FILE:C:\TestImage\TestImage2.C LINE:23
*** Interpreter error recovered ***
root [1]
I’ve attached the graph from PerfMon, and the Task Manager to show how the memory allocation is not freed up.
I’ve also ran the script on another Windows XP, a Windows Vista, and on a Windows 7 installation - all had the same problem!
Could someone with a Windows installation help with this please?
It seems obvious to me that there is something wrong here.
I’m willing to help in whatever way…