ASE Home Page Products Download Purchase Support About ASE
ChartDirector Support
Forum HomeForum Home   SearchSearch

Message ListMessage List     Post MessagePost Message

  Possible memory leak in DrawArea::text()
Posted by David Wilson on Nov-05-2019 03:35
I believe there may be a memory leak in the C++ function

TTFText* DrawArea::text(const char* str, const char* font, double fontSize);

I detected the leak in Visual C++ with MFC.

Specifically, the code


TTFText* ptr = DrawArea()::text(str, font, size);
delete ptr;


seems to leak memory.

If you cannot reproduce, I can supply more details about the exact arguments I was using.

  Re: Possible memory leak in DrawArea::text()
Posted by Peter Kwan on Nov-05-2019 17:49
Hi David,

I have just tried the following code:

for (int j = 0; j < 1000; ++j)
{
    DrawArea* d = new DrawArea();
    for (int i = 0; i < 10000; ++i)
    {
        TTFText* ptr = d->text("abc", "arial.ttf", 10);
        delete ptr;
    }
    delete d;
}

The above code should run the DrawArea::text for around 10 million times. I did not see any memory leak. In Visual Studio, there is a "Diagnostic Tools" page that shows the process memory usage. It does not show any memory increase.

When the MFC application ends, Visual Studio should also display any memory leak it has detected. It does not detect any memory leak in my test case.

Note that if you create a TTFText, then delete it, it will appear some memory is not released. It is because ChartDirector can cache some commonly by unchanging items for speed. One example is the font and and rendered text. If you draw some text such as "120", it is very likely your application will show the same characters "1", "2" or "0", using the same font and same font size again. So it will cache the font and the rendered text. If you repeat the code, the memory will not increase indefinitely, because ChartDirector will reuse the cache. So we test memory leak by running the code many times. If the memory usage is reasonable and does not increase indefinitely, there should be no leak.

Regards
Peter Kwan