c++ - 在构造函数之后立即编程 SegFaults。 C++

标签 c++ class constructor segmentation-fault

我目前在编写的程序中遇到一个小问题,它在构造函数之后出现段错误。

这是一些代码:

void    GameEngine::createMap(std::vector<std::string> &parse)                                                                                                                                                       
{                                                                                                                                                                                                                    
  int   x;                                                                                                                                                                                                           
  int   y;                                                                                                                                                                                                           
  std::string strx;                                                                                                                                                                                                  
  std::string stry;                                                                                                                                                                                                  

  strx = parse.at(0);                                                                                                                                                                                                
  stry = parse.at(1);                                                                                                                                                                                                
  x = atoi(strx.c_str());                                                                                                                                                                                            
  y = atoi(stry.c_str());                                                                                                                                                                                            
  std::cout << "okokok" << std::endl;
  //_gMap is a Graphmap*;                                                                                                                                                                              
  this->_gMap = new GraphMap(x, y);                                                                                                                                                                                  
  std::cout << "okokokabc" << std::endl;                                                                                                                                                                             
}

createMap() 是当我连接的服务器向我发送“msz X Y\n”时调用的函数,它肯定会被调用。具有有效的 X Y。

所以这是一个调用我的类(GraphMap)构造函数的函数。 X 和 Y 为有效数字

这是 GraphMap 类。

.hh

#ifndef GRAPHMAP_HH_
#define GRAPHMAP_HH_

class GameEngine;

class GraphMap
{
private:
  int   _height;
  int   _width;
  int   _squareSize;
public:
  GraphMap(int, int);
  ~GraphMap();
  void  draw(SDL_Renderer *);
};

#endif

还有.cpp:

#include "GameEngine.hh"
#include "GraphMap.hh"

GraphMap::GraphMap(int width, int height)
{
  std::cout << "testouilleee1" << std::endl;
  _width = width;
  std::cout << "testouilleee2" << std::endl;
  _height = height;
  std::cout << "testouilleee3" << std::endl;
  _squareSize = 1000 / width;
  std::cout << "testouilleee4" << std::endl;
}

GraphMap::~GraphMap() {}

void    GraphMap::draw(SDL_Renderer *renderer)
{
  int   i;
  int   j;
  for(i = 1 ; i <= _width ; i++)
    {
      for (j = 1 ; j <= _height ; j++)
        {
          SDL_Rect rect;
          rect.x = ((i - 1) * _squareSize);
          rect.y = ((j - 1) * _squareSize);
          rect.w = _squareSize - 1;
          rect.h = _squareSize - 1;
          SDL_SetRenderDrawColor(renderer, 0, 255, 0, 255);
          SDL_RenderFillRect(renderer, &rect);
        }
      j = 1;
    }
}

我无法理解的是输出是:

$ ./run.sh 
okokok
testouilleee1
testouilleee2
testouilleee3
testouilleee4
./run.sh: line 12: 10414 Segmentation fault      (core dumped) ./zappy_graph 127.0.0.1 4242

这意味着它转到构造函数的最后一行,然后出现 segFaults,并且不打印我无法理解的“okokokabc”

以下是来自 GDB 的一些调试信息:

0x0000000000404556 in GameEngine::createMap (this=0x0, parse=...) at GameEngine.cpp:90
90    this->_gMap = new GraphMap(x, y);
(gdb) bt
#0  0x0000000000404556 in GameEngine::createMap (this=0x0, parse=...) at GameEngine.cpp:90
#1  0x000000000040561b in Command::msz (this=0x712360, cmd=..., game=0x0) at Command.cpp:32
#2  0x000000000040647c in Command::Parse (this=0x712360, command=..., game=0x0) at Command.cpp:138
#3  0x000000000040949b in Socket::selectSocket (this=0x7120a0) at Socket.cpp:67
#4  0x000000000040441e in GameEngine::update (this=0x712010) at GameEngine.cpp:58
#5  0x00000000004046f0 in GameEngine::run (this=0x712010) at GameEngine.cpp:110
#6  0x000000000040968e in main (ac=1, av=0x7fffffffdd78) at main.cpp:22

(gdb) print x
$1 = 20
(gdb) print y
$2 = 20

如果您需要更多信息/代码,请告诉我,我会尽快发布。

最佳答案

GDB 显示 GameEngine 地址为 0x0:

GameEngine::createMap (this=0x0...

所以问题是由于某种原因 GameEngine 指针错误(未初始化或损坏)。

注意这个指针好像至少来自

Command::Parse (this=0x712360, command=..., game=0x0)

(我想最后一个参数是 GameEngine 本身?)

另请注意,下面的几行是正确的 GameEngine 对象:

GameEngine::update (this=0x712010) at GameEngine.cpp:58

如果您的程序中只有一个 GameEngine(我假设是这种情况),那么这意味着 GameEngine 地址在 GameEngine 之间以某种方式丢失: :update()Command::Parse()

关于c++ - 在构造函数之后立即编程 SegFaults。 C++,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/31001835/

相关文章:

java - 继承中的静态 block vs 初始​​化 block vs 构造函数

C++检测用户按下的ENTER键

c++ - Linux 的 header - VS Unix 扩展

c++ - 多个构造函数的内存使用c++

c++ - C++中的默认构造函数

php - 如何在 PHP (Java)Doc 中标记数组值类型?

c++ - 重建项目而不停止调试

c++ - 基于使用 make 命令或 makefile 运行预处理器

c++ - C++ 类中的构造函数

css - 我怎么能:hover over different links in one line while getting the spacing correct?