Memory leak with FREngine::OpenBitmap

  • 230 Views
  • Last Post 2 weeks ago
Leif posted this 4 weeks ago

I am writing unit tests for our code, and I am unable to remove one particular memory leak. The unit test is very simple. I create an instance of FREngine (in-proc COM server) and load an image file to create a HBITMAP resource. I then repeatedly call FREngine::OpenBitmap, passing in the HBITMAP followed by setting the returned value to null as follows:

    FREngine::IImageDocumentPtr frImageDocumentPtr = iEnginePtr->OpenBitmap((long long)hBitmap, bitmap.bmWidth, bitmap.bmHeight, nullptr);
    if (frImageDocumentPtr == NULL)
    {
        throw L"Unable to open the supplied bitmap";
    }
    frImageDocumentPtr = nullptr;

After each iteration of the above, I get the total memory used by the application, and output a message if the memory has changed from the previous iteration. I find that the memory always increases substantially. I cannot figure out what to do to remove this leak.

Incidentally the above might seem a silly thing to do, but the leak was originally found in a slightly more realistic unit test:

   // This variable is created at the start of the units test

    // FREngine::IEngine *iEnginePtr
   

    // Now loop over the following steps, and each time check for a memory leak

    FREngine::IFRDocumentPtr frDocument = iEnginePtr->CreateFRDocument();

    // This causes a bug!
    FREngine::IImageDocumentPtr frImageDocumentPtr = iEnginePtr->OpenBitmap((long long)hBitmap, bitmap.bmWidth, bitmap.bmHeight, nullptr);
    if (frImageDocumentPtr == NULL)
    {
        throw L"Unable to open the supplied bitmap";
    }

    frDocument->AddImageDocument(frImageDocumentPtr);

    // Now free up the documents

    frImageDocumentPtr = nullptr;
    frDocument->Close();
    frDocument = nullptr;

 

Order By: Standard | Newest | Votes
Leif posted this 4 weeks ago

 I've done some more testing and I am not convinced this is a memory leak. I created a loop where each iteration does the following:

    FREngine::IImageDocumentPtr frImageDocumentPtr = iEnginePtr->OpenBitmap((long long)hBitmap, bitmap.bmWidth, bitmap.bmHeight, nullptr);
    if (frImageDocumentPtr == NULL)
    {
        throw L"Unable to open the supplied bitmap";
    }
    frImageDocumentPtr = nullptr;

Over 100,000 iterations the memory initially varies greatly but soon tends towards a stable value with a fluctuation (maximum minus mimimum) of about 0.5 MB with no obvious upward or downward trend.

The memory usage was measured as follows: 

    PROCESS_MEMORY_COUNTERS memCounter;
    BOOL result = GetProcessMemoryInfo(GetCurrentProcess(), &memCounter, sizeof(memCounter));
    std::cout << "Memory usage = {" << memCounter.WorkingSetSize << "}" << std::endl;

I can only assume that this is related to the way ABBYY allocates memory. Perhaps it creates collections (such as lists) where the number of elements allocated has a granularity and is much larger than needed to avoid too many reallocations, and it only occasionally frees up memory? 

I tried to attach a document but the attach button does nothing.

Attached Files

Leif posted this 3 weeks ago

Unfortunately I am no longer able to test this as it has used up the allowed number of pages in the licence! And ABBYY now want us to pay for the licence despite us still doing development work.

Leif posted this 3 weeks ago

I can do some testing without an allocation of pages. It would appear that this line causes the leak:

    documentPtr->Pages->Item(0)->PreprocessAnalyzeRecognize(pageProcessingParamsPtr);

So I assume there is something that I should free after I read the extracted text.

Anna Savinova posted this 3 weeks ago

We'll check this info with the R&D and let you know about the results as soon as possible.

Leif posted this 3 weeks ago

Thank you.

Also the attach feature does not work. I clicked on attach, then clicked on the browse button to select a file, uploaded the file, and nothing! I am using Firefox 54.01 (32 bit) on Windows 10.

Anna Savinova posted this 3 weeks ago

 

Let's discuss the problem with attachs here http://forum.ocrsdk.com/thread/attach-button-on-forum-doesn-t-work/

Leif posted this 3 weeks ago

I have attached a document to my second post above. It worked under IE with a .PDF file.

Anna Savinova posted this 2 weeks ago

When you are iterating by pages, you should call the frPage->Flush() method when the page isn't used anymore. You don't call it, that's why you can see the memory usage growth during the cyclic PreprocessAnalyzeRecognize() method call.

 

I wasn't able to reproduce the memory usage growth for the scenario with Bitmap opening. Please specify how do you get the bitmap handler.

Leif posted this 2 weeks ago

Hello Anna

Thank you. I will add the Flush() call. I will take a look at the BitmapOpening too, but not today as I am busy.

Close