我目前在我正在编写的程序上有一个小问题,它在构造函数之后才出现segFaults。

这是一些代码:

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;
}


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

所以这是一个调用我的类的(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()之间丢失了。

09-06 03:40