c++ - 释放内存时出错

标签 c++ memory-leaks destructor free

将具有动态内存的自定义类添加到 vector 中时出现奇怪的错误。

错误是

error for object 0x7fee9ac000e0: pointer being freed was not allocated *** set a breakpoint in malloc_error_break to debug

尝试添加带有动态内存的Symbol 会产生错误,在析构函数中删除delete[] 参数 会消除错误,但我认为会导致内存泄漏。

代码如下

//test.cpp
#include <iostream>
#include <cstdio>
#include <cstdlib>
#include "symbol.h"

int main(int argc, char const *argv[])
{
    Symbol a('A');
    std::cout << a << std::endl;
    Symbol b('B',2);
    std::cout << b << std::endl;
    double p[3] = {1.2, 2.4, 4.8};
    Symbol c('C',3,p);
    std::cout << c << std::endl;
    Symbol* d = new Symbol('D',3,p);

    ////
    std::cout << *d << std::endl;
    std::vector<Symbol> axiom;
    axiom.push_back(a)
    axiom.push_back(b); // This lines produces the error

    delete d;
    return 0;
}

//symbol.h
#ifndef SYMBOL_H
#define SYMBOL_H

#include <iostream>

class Symbol{
    friend std::ostream& operator<<(std::ostream& output, const Symbol& s);

    private:
        char character;
        int numpar;
        double* parameters;

    public:
        Symbol();
        Symbol(const char c);
        Symbol(const char c, const int n);
        Symbol(const char c, const int n, const double p[]);
        ~Symbol();

        bool operator == (const Symbol &other) const;

};

#endif


//symbol.cpp
#include "symbol.h"

Symbol::Symbol()
{
    character = 0;
    numpar = 0;
}

Symbol::Symbol(const char c)
{
    character = c;
    numpar = 0;
}

Symbol::Symbol(const char c, const int n)
{
    character = c;
    if(n > 0)
    {
        numpar = n;
        parameters = new double[numpar];
        std::fill(parameters, parameters+numpar, 0.0);
    }
    else
    {
        numpar = 0;
    }
}

Symbol::Symbol(const char c, const int n, const double p[])
{
    character = c;
    if(n > 0)
    {
        numpar = n;
        parameters = new double[numpar];
        std::copy(p,p+numpar,parameters);
    }else{
        numpar = 0;
    }

}

Symbol::~Symbol()
{
    if(this->numpar > 0)
    {
        delete[] parameters; //If I comment this line the code runs smoothly but I think it produces memory leaks
    }
}

bool Symbol::operator==(const Symbol &other) const {
    if (character == other.character)
        return true;
    return false;
}

std::ostream& operator<<(std::ostream& output, const Symbol &s){
    output << s.character;
    if(s.numpar > 0)
    {
        output << '(';
        for(int i = 0 ; i < s.numpar-1 ; i++)
        {
            output << s.parameters[i] << ", ";
        }
        output << s.parameters[s.numpar-1] << ')';
    }
    return output;
}

最佳答案

您使用的是 vector,因此当您 push_back 一个 Symbol 时,它实际上创建了一个新的 Symbol,并且由于您没有定义复制构造函数,它只是简单地复制字段而不会询问任何内容。

所以你实际上有两个 Symbol 实例,它们的指针指向同一个参数数组,当一个被销毁时,另一个尝试释放已经释放的东西。

参见:http://www.tutorialspoint.com/cplusplus/cpp_copy_constructor.htm

关于c++ - 释放内存时出错,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/33968817/

相关文章:

c++ - 获取用户输入的代码不在 C++ 中执行/跳过

c# - 如何修复 IE WebBrowser 控件中的内存泄漏?

reactjs - Redux 存储会导致内存泄漏吗?

c++ - 当构造函数是私有(private)的时使用公共(public)析构函数

c++ - C++中构造函数和析构函数的内联用法

c++ - 整数和 const char[N] 上的模板未编译(无法推断模板参数 'N' )

c++ - map<string1, map<string2, map<string3, string>>> 是否比将字符串连接到 map<string1string2string3, string> 慢?

c++ - 用户定义的 Copy ctor 和链下的 copy-ctors - 编译器错误?程序员脑残?

ios - 如何避免 subview Controller 的内存泄漏

c++ - 如何在 C 中模拟构造函数和析构函数行为(针对特定数据类型)