简体   繁体   English

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

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

I'm completely stumped as to the cause of the above glibc error. 对于上述glibc错误的原因,我完全感到困惑。 I must be missing something obvious, but it appears every time the following program (consisting of 4 files) exits: 我必须缺少明显的东西,但每次退出以下程序(由4个文件组成)时,它就会出现:

TankSim.h: TankSim.h:

#ifndef TANKSIM_WDECAY_TANKSIM_H_
#define TANKSIM_WDECAY_TANKSIM_H_

#include <cstdlib>
#include <iostream>

#include "Parser.h"

int main();

#endif

TankSim.cpp: TankSim.cpp:

#include "TankSim.h"

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

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

     return(0);
}

Parser.h: 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: 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;
  }
}

Since I'm not explicitly calling any delete operators, I have no idea why the error occurs. 由于我没有明确调用任何删除运算符,因此我不知道为什么会发生错误。

Most likely its the missing return statement. 最有可能是缺少的return语句。

Then in your main method the Settings local object is never initialized. 然后,在您的主要方法中,永远不会初始化Settings本地对象。 Then its scope ends and the destructor for the vectors in it are called and they think they have a pointer (because they are initialized with junk in memory) and call delete on their pointer. 然后其作用域结束,并调用其中的向量的析构函数,他们认为它们有一个指针(因为它们是用内存中的垃圾初始化的),并对其指针调用delete。

Adding -Wall to enable all warnings will tell you about this in the future. 添加-Wall以启用所有警告将在以后告诉您有关此的信息。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM