c++ - Valgrind 发现了 3 个内存泄漏,但我不知道它们在哪里

标签 c++ valgrind

我尝试自己实现基本的 String 类并且它工作正常,但 Valgrind 说有 3 处内存泄漏,我无法弄清楚泄漏的位置和原因。我真的试图删除不再使用的所有内容(我今天开始使用 Valgrind)。 现在我真的很关心我的基本 C/C++ 内存管理知识。我对代码中 Valgrind 发现泄漏的地方进行了评论 (//VALGRIND)。我还上传了此错误消息的屏幕截图 click to see the screenshot .

编辑:我更新了截图,所以你可以看到完整的输出。

字符串T.h

template<typename char_type = char>
class StringT {
public:

 explicit StringT(const char_type *str) {
        if (str != nullptr) {
            size_t len = strlen(str);
            m_str = new char_type[len + 1]; //VALGRIND: 6 bytes in 1 blocks are definitely lost in loss record 1 of 3
            strcpy(m_str, str);
        }
    }

   ~StringT() {
        delete [] m_str;
    }


StringT(const StringT & other) {
        size_t len = 0;
        if (other.m_str) len = strlen(other.m_str);
        m_str = new char_type[len + 1]; //VALGRIND: 6 bytes in 1 blocks are definitely lost in loss record 2 of 3
        strcpy(m_str, other.m_str);
    }

    StringT(StringT && other) noexcept {
        m_str = other.m_str;
        other.m_str = nullptr;
    }


     StringT & operator+=(const StringT &other) {
        if (other.m_str == nullptr) //when other str is empty just return current Str
            return *this;

        const size_t mysize{m_str ? strlen(m_str) : 0}; // check if not null then call strlen
        const size_t osize{other.m_str ? strlen(other.m_str) : 0};

        char *newStr = new char_type[osize + mysize + 1]; //VALGRIND: 11 bytes in 1 blocks are definitely lost in loss record 3 of 3
        newStr[0] = '\0'; //strcat searches for '\0', so newStr has to be a valid String

        if (m_str) strcat(newStr, m_str);
        if (other.m_str) strcat(newStr, other.m_str);

        delete[] m_str; //delete old string
        m_str = newStr; //set member to new concatenated str

        return *this;
    }

    size_t length() const {
        if (!m_str) return 0;
        return strlen(m_str);
    }


    friend
    std::ostream &operator<<(std::ostream &out, StringT<> &other) {
        if (other.m_str) out << other.m_str;
        return out;
    }

private:
    char_type *m_str{nullptr};
};

主要.cpp

int main() {

    const char *cArr = "Hello";
    const char *cArr2 = "World";
    StringT<char> hello(cArr);
    StringT<char> world(cArr2);
    StringT<char> emptyStr;

    std::cout << "hello: " << hello << std::endl;
    std::cout << "world: " << world << std::endl;
    std::cout << "emptyStr: " << emptyStr << std::endl;

    StringT<char> hCopy(hello);
    StringT<char> wMove(std::move(world));

    std::cout << "hCopy: " << hello << std::endl;
    std::cout << "hCopy: " << hCopy << std::endl;
    std::cout << "world: " << world << std::endl;
    std::cout<<  "wMove: " << wMove << std::endl;
    std::cout<<  "lenMove: " << wMove.length() << std::endl;
    std::cout<<  "lenEmptyStr: " << emptyStr.length() << std::endl;

    hello += wMove;
    std::cout<<  "hello += world: " << hello << std::endl;

    return 0;
}

最佳答案

你的删除在这里:

StringT() {
    delete [] m_str;
}

但那是构造函数,不是析构函数。 😉

关于c++ - Valgrind 发现了 3 个内存泄漏,但我不知道它们在哪里,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/56396882/

相关文章:

c - 无效写入 Valgrind

基于未初始化值的条件移动,未初始化值由堆分配创建

c - 将结构从 x->y 转换为 x.y 并删除 malloc 时出现 Valgrind 错误。艰难地学习 C(前 16)- 额外学分

c - Valgrind 神秘的无效写入大小 8 错误

c++ - 系统调用参数 socketcall.recvfrom(buf) 指向不可寻址的字节

Python 和 C++ 集成。动态库问题

c++ - 支持函数转换为引用类型、标准中的漏洞或编译器错误?

c++ - 为什么 int* ptr_arr_int = {1,2};在 C/C++ 中不起作用?

c++ - 解释频率[toupper(new_letter) - 'A' ]++;

c++ - 重用后 DLL 使应用程序崩溃