Possible memory leaks when using DVInterface with OPENSLL

All other questions regarding DCMTK

Moderator: Moderator Team

Post Reply
Message
Author
Mihai Neamt
Posts: 1
Joined: Fri, 2019-06-14, 11:08

Possible memory leaks when using DVInterface with OPENSLL

#1 Post by Mihai Neamt »

Hello,
In the past days I have tried to solve a problem that came up after switching on OPENSLL with static libs. To be more specific, when i declare an object as follows:

Code: Select all

  
  DVInterface dvi(opt_cfgName);
to use it for dicom print support, Visual Studio detects memory leaks. I used Visual Leak detector to see where is the actual leak and the result was something like this:

Code: Select all

    .\crypto\mem.c (79): DicomViewer.exe!default_malloc_ex() + 0xD bytes
    .\crypto\mem.c (306): DicomViewer.exe!CRYPTO_malloc() + 0x12 bytes
    .\crypto\objects\o_names.c (195): DicomViewer.exe!OBJ_NAME_add() + 0x11 bytes
    .\crypto\evp\c_alld.c (72): DicomViewer.exe!OpenSSL_add_all_digests() + 0x14 bytes
    .\crypto\evp\c_all.c (90): DicomViewer.exe!OPENSSL_add_all_algorithms_noconf()
    e:\library\dcmtk 3.6.3\dcmtk-3.6.3\dcmtk-3.6.3\dcmsign\libsrc\dcsignat.cc (65): DicomViewer.exe!DcmSignature::initializeLibrary()
    e:\library\dcmtk 3.6.3\dcmtk-3.6.3\dcmtk-3.6.3\dcmpstat\libsrc\dviface.cc (160): DicomViewer.exe!DVInterface::DVInterface()
    g:\mihai\proiecte\pixeldata.dicom.viewer\dcmtk viewer\waitdlg.cpp (6297): DicomViewer.exe!CWaitDlg::SendStoredPrintObject() + 0x11 bytes
    g:\mihai\proiecte\pixeldata.dicom.viewer\dcmtk viewer\waitdlg.cpp (5900): DicomViewer.exe!CWaitDlg::SendImagesToPrintFree() + 0x11 bytes
    g:\mihai\proiecte\pixeldata.dicom.viewer\dcmtk viewer\waitdlg.cpp (5794): DicomViewer.exe!CWaitDlg::DcmPrintFree() + 0xC bytes
After some tries to comment the code before de DVInterface with no luck, I just commend out the DVInterface declaration an the leaks dissapeard. I tried the same thing with a new project, just to be sure that something from the project didn't interfere with the memory allocation and the result was the same.
After some research, I came with the conclusion that the leaks are (maybe) from DCMSignature:

Code: Select all

void DcmSignature::initializeLibrary()
{
  OpenSSL_add_all_algorithms();
  ERR_load_crypto_strings();
}
I searched the file for a cleanup method, like and ::ERR_clear_error() / ERR_remove_state(0) / OPENSSL_cleanup without luck.
After I compiled the latest OPENSSL available (DLL version) I came with the conclusion that Visual Studio, and after some research, Visual Leak detector can't find leaks in DLLs without includind the "vld.h" file in a certain file of the DLL, so the leaks are detected just if I use dcmtk with OPENSSL in static library. So i recompiled the latest OPENSSL library (static version now) and the leaks appeared again.

I don't know if it's a problem that memory from OPENSSL isn't freed in the library or me using the library incorectly. I hope I made my problem clear.

Thank you,
Mihai

Marco Eichelberg
OFFIS DICOM Team
OFFIS DICOM Team
Posts: 1437
Joined: Tue, 2004-11-02, 17:22
Location: Oldenburg, Germany
Contact:

Re: Possible memory leaks when using DVInterface with OPENSLL

#2 Post by Marco Eichelberg »

DCMTK does indeed not call OPENSSL_cleanup() or any other OpenSSL cleanup function. Normally, one would do that right before the application ends, but then the cleanup only makes a difference for a memory leak checker, not for the real application. DCMTK does never unload/reload an OpenSSL dll/shared object. So you could insert OPENSSL_cleanup() at the end of your main() routine to keep your memory leak checker happy.

Marco Eichelberg
OFFIS DICOM Team
OFFIS DICOM Team
Posts: 1437
Joined: Tue, 2004-11-02, 17:22
Location: Oldenburg, Germany
Contact:

Re: Possible memory leaks when using DVInterface with OPENSLL

#3 Post by Marco Eichelberg »

See also the documentation here: https://www.openssl.org/docs/man1.1.1/m ... eanup.html
Normally, a manual call to OPENSSL_cleanup() should not be necessary and is not recommended, at least for newer OpenSSL versions.

Post Reply

Who is online

Users browsing this forum: Ahrefs [Bot] and 1 guest