#23716·opencv

Detect memory leaks in cv::putText

Author: GuoYX5Created May 31, 2023Updated Sep 17, 2026
Labelsbugcategory: imgproc

System Information

OpenCV version: 5.x Operating System / Platform: Windows 10 Compiler & compiler version: visual studio 2019

Detailed description

Detect memory leaks in cv::putText

Steps to reproduce

#include <opencv2\opencv.hpp>

#define _CRTDBG_MAP_ALLOC
#include <stdlib.h>
#include <crtdbg.h>

#pragma execution_character_set("utf-8")

int main() { for (int i = 0; i < 100000; i++) { int fontHeight = 100; int thickness = -1; int linestyle = 8;

    cv::Mat img(240, 800, CV_8UC3, cv::Scalar::all(0));

    std::string sStr = "hello你好中文";
    cv::FontFace font;
    
    _CrtSetDbgFlag(_CrtSetDbgFlag(_CRTDBG_REPORT_FLAG) | _CRTDBG_LEAK_CHECK_DF);
    for (int j = 0; j < 10000; j++)
    {
        cv::putText(img, sStr, cv::Point(400, 120), CV_RGB(0, 0, 255), font, 50, -1, cv::PUT_TEXT_ALIGN_RIGHT);
        //cv::putText(img, sStr, cv::Point(400, 120), 1, 50, CV_RGB(0, 0, 255));
    }
    _CrtDumpMemoryLeaks();

    std::cout << "finish" << std::endl;
}

return 0;

}

Issue submission checklist

  • I report the issue, it's not a question
  • I checked the problem with documentation, FAQ, open issues, forum.opencv.org, Stack Overflow, etc and have not found any solution
  • I updated to the latest OpenCV version and the issue is still there
  • There is reproducer code and related data files (videos, images, onnx, etc)