*** glibc检测到*** free():无效的指针:

*** glibc detected *** free(): invalid pointer:

本文关键字:无效 指针 glibc 检测 free      更新时间:2023-10-16

我对上述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;
  }
}

由于我没有明确调用任何删除操作员,所以我不知道为什么会发生错误。

很可能是缺少的返回语句。

然后,在您的主要方法中,Settings本地对象永远不会初始化。然后,它的范围结束了,其中矢量的驱动器被称为,他们认为自己有指针(因为它们是在记忆中使用垃圾的初始化),并在其指针上调用删除。

添加-Wall启用所有警告将来会告诉您有关此信息的信息。