在构造函数之后立即编写SegFaults。 C ++

时间:2015-06-23 11:41:53

标签: c++ class constructor segmentation-fault

我目前在我编写的程序上有一点问题,它只是在构造函数之后的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;                                                                                                                                                                             
}

createMap()是一个函数,当我连接的服务器发送给我&#34; msz X Y \ n&#34;时,它被调用,它被肯定调用。有效的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并且不会打印&#34; okokokabc&#34;我无法理解

以下是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

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

1 个答案:

答案 0 :(得分:3)

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()之间丢失了