[英]*** 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.