簡體   English   中英

“沒有分配被釋放的指針”

[英]“pointer being freed was not allocated”

#include <iostream>
#include <fstream>
#include <cmath>
#include <string>
#include <sstream>

using namespace std;

class CFile {
public:
    CFile(string filename); 
    ~CFile();               
    void ReadFile();         
    void WriteFile(string outputFilename);   
    void Calculate();   
    string  m_filename;
    int     m_numberInput;
    double* m_xData;         
    double* m_yData;
    int     m_numberOutput;
    double* m_xDataOut;     
    double* m_yDataOut;
};

CFile::CFile(string filename)
{
    m_filename = filename;
    string line;             
    ifstream myfile(m_filename.c_str());

    if (myfile.is_open())     
    {
        getline(myfile,line);    
        myfile.close();          
        stringstream Str;        
        Str << line; 
        Str >> m_numberInput;
        m_xData = new double[m_numberInput];  
        m_yData = new double[m_numberInput];
        cout << sizeof(m_xData) / sizeof(m_xData[0]) << endl;
    }
    else cout << "Unable to open file.\n";  
}

CFile::~CFile()
{ 
    delete[] m_xData; 
    delete[] m_yData;                                  
    m_xData = 0;                                 
    m_yData = 0;
    delete[] m_xDataOut; 
    delete[] m_yDataOut;                                  
    m_xDataOut = 0;                                 
    m_yDataOut = 0;
}

void CFile::ReadFile()
{
    ifstream infile(m_filename.c_str());
    string line;

    if (infile.is_open())
    {
        int x, y, i = 0;

        while (getline(infile,line))
        {
            infile >> x >> y;
            m_xData[i] = x;
            m_yData[i] = y;
            i++;
        }
        infile.close();
    }
    else cout << "Unable to open file.\n"; 
}

void CFile::WriteFile(string outputFilename)
{
    ofstream outfile(outputFilename.c_str());

    if (outfile.is_open())
    {
        for(int i=0; i < m_numberInput; i++)
            outfile << m_xDataOut[i] << " " << m_yDataOut[i] << endl;

        outfile.close();
    }
    else cout << "Unable to open file.\n"; 
}

void CFile::Calculate()
{
    m_xDataOut = new double[m_numberInput]; 
    m_yDataOut = new double[m_numberInput];

    for(int i=0; i < m_numberInput; i++)
    {
        m_xDataOut[i] = m_xData[i];
        m_yDataOut[i] = sqrt(m_yData[i]);
    }
}


int main()
{
    CFile file("Input.dat");
    file.ReadFile(); 
    file.Calculate(); 
    file.WriteFile("Output.dat");
    file.~CFile();
}

錯誤消息如下:

main(11915,0x7fff77d3d310) malloc: *** error for object 0x7f8a99403940: pointer 
being freed was not allocated
*** set a breakpoint in malloc_error_break to debug
Abort trap: 6

這是幾周前具有諷刺意味的代碼。 我改變了OS和編譯器,現在它不再了。 我在其他線程中讀到了這個錯誤,但無法真正理解如何在我的代碼中使用解決方案。 代碼讀入數據文件,確實如此。 使用它然后將更改的值寫入另一個文件。 分配錯誤在哪里? 非常感謝您的幫助! :-)

您的析構函數被調用兩次。 您可以顯式調用它,並在退出main()時調用它。 你應該刪除你的顯式destrcutor調用。

兩個問題:

  • 您不必初始化所有四個指針,在這種情況下,對它們應用delete是不安全的。 您可以在執行任何操作之前將它們初始化為null來解決此問題; 或者,更好的是,用std::vector<double>替換它們,這樣你就不必亂用delete了。 這也將解決該類的無效復制語義(這將打破規則 ),並且如果構造失敗,則可能導致內存泄漏。
  • 你手動調用析構函數( file.~CFile() ,在main的末尾)。 不要那樣做:當程序離開變量的作用域時會自動調用析構函數,並且調用它兩次是錯誤的。

正如@claptrap指出你的記憶管理也是錯誤的。

  1. 您應該遷移到std::vector<double>而不是原始的double數組和指針。 您可以將vec.resize(N)視為new double[N]的變體,它永遠不需要顯式delete[]

  2. 如果您使用的是Linux,則最好在valgrind下運行程序,該程序會自動跟蹤內存分配/取消分配並指出任何無效的內存操作。 我打賭你的Linux發行版有一個預編譯的包。

這是造成這個問題的原因

  delete[] m_xDataOut; 
  delete[] m_yDataOut;    

您尚未在構造函數中初始化指針

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM