如何解决`CRT检测到应用程序在堆缓冲区结束后写入内存`

问题描述 投票:0回答:0

看这个,当我调用构造函数时,是否可能发生内存泄漏?

class Containter{
  public:
    unchar * data;
    std::shared_ptr<uchar> data_;
    Containter::Containter(int _rows, int _cols, int type){
        init(_rows, _cols, type);
    }
    void ::init(int _rows, int _cols, int type){
        rows = _rows;
        cols = _cols;
        size_t buf_size = rows * cols;
        data_.reset(new uchar[buf_size], [](uchar* p) {
            delete[] p;
            });

        data = data_.get();

      }

事实上,当我多次调用构造函数时,我不知道为什么会发生内存泄漏。 我知道当我写入超出我分配的内存大小的数据时,会发生“CRT 检测到应用程序在堆缓冲区结束后写入内存”,但在

init
函数中。我从来不写数据到内存,怎么会发生内存泄漏?

c++ class constructor memory-leaks heap-memory
© www.soinside.com 2019 - 2024. All rights reserved.