簡體   English   中英

***檢測到glibc *** free():無效的指針:

[英]*** glibc detected *** free(): invalid pointer:

對於上述glibc錯誤的原因,我完全感到困惑。 我必須缺少明顯的東西,但每次退出以下程序(由4個文件組成)時,它就會出現:

TankSim.h:

#ifndef TANKSIM_WDECAY_TANKSIM_H_
#define TANKSIM_WDECAY_TANKSIM_H_

#include <cstdlib>
#include <iostream>

#include "Parser.h"

int main();

#endif

TankSim.cpp:

#include "TankSim.h"

using std::cout;
using std::endl;

int main()
{
     const Settings gGlobals = ParseConfig();

     return(0);
}

Parser.h:

#ifndef TANKSIM_WDECAY_PARSER_H_
#define TANKSIM_WDECAY_PARSER_H_

#include <cstdlib>
#include <vector>
#include <fstream>
#include <sstream>
#include <iostream>

struct Settings {
  double speedlight;
  double refractiveindex;
  double absorptionchance;
  double pmtradius;
  double photspermetre;
  double tankradius;
  double tankheight;
  long unsigned int randomseed;
  double scatterangle;
  std::vector<double> entrypoint;
  std::vector<double> entrydrn;
};

Settings ParseConfig();

#endif

Parser.cpp:

#include "Parser.h"

using std::string;
using std::vector;
using std::cout;
using std::endl;

Settings ParseConfig()
{
  Settings settings;

  std::ifstream configfile("settings.dat");

  string line;
  while(getline(configfile,line)){
    //Comments start with %
    if(line.find("%") != string::npos){
      cout << "Comment: " << line << endl;
      continue;
    }

    //Read in variable name.
    std::stringstream ss;
    ss << line;
    string varname;
    ss >> varname;
    string value = line.substr(varname.length()+1);
    cout << varname << "-" << value << endl;
  }
}

由於我沒有明確調用任何刪除運算符,因此我不知道為什么會發生錯誤。

最有可能是缺少的return語句。

然后,在您的主要方法中,永遠不會初始化Settings本地對象。 然后其作用域結束,並調用其中的向量的析構函數,他們認為它們有一個指針(因為它們是用內存中的垃圾初始化的),並對其指針調用delete。

添加-Wall以啟用所有警告將在以后告訴您有關此的信息。

暫無
暫無

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

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