DCMTK DcmFileFormat memory leak

  c++, dcmtk, qt

DCMTK version: 3.6.3
OS: Ubuntu 20.04
Qt: 5.12.8

I loaded a dicom image 6000 times, and then I found the memory leaked.

A simple example:

#include <dcmtk/dcmdata/dctk.h>
#include <QString>
#include <QVector>
#include <QApplication>
#include <QDebug>

int main(int argc, char *argv[])
{
    QApplication Application(argc, argv);
    QString file_path = "/dicom/files/path/file.dcm";
    DcmFileFormat * dcm_file_format_ = nullptr;
    QVector<DcmFileFormat *> dcmfile_vector;

    for (int i = 0; i < 6000; i++)
    {
        OFCondition status;

        dcm_file_format_ = new DcmFileFormat();
        status       = dcm_file_format_->loadFile(std::string(file_path.toLocal8Bit()).c_str());

        dcmfile_vector.push_back(dcm_file_format_);
    }

    for (int i = 0; i < dcmfile_vector.size(); i++)
    {
        delete dcmfile_vector[i];
    }
    dcmfile_vector.clear();

    int r = Application.exec();
    return r;
}

This program uses 291MB of RAM at the end.
when I loaded the image more times, the program will leak more memory.

why does this happen? what did I do wrong?

Source: Windows Questions C++

LEAVE A COMMENT