编程接收信号SIGSEGV,getter方法

时间:2015-08-04 16:21:49

标签: c++ sdl

我正在使用Code :: Blocks制作游戏,并遇到了Segmentation错误。我在这里看了其他问题,找不到任何有用的东西。我有一个代表游戏中基本对象的Sprite类和一个控制游戏屏幕显示的Window类。我运行了调试器,它在下面的return image;方法的getImage()行给了我错误。

SDL_Surface* Sprite::getImage() {
    return image;
}

我在setImage()的构造函数中调用了以下Sprite方法,并且我确认SDL_ConvertSurface没有返回NULL

void Sprite::setImage(string path) {
    SDL_Surface* loadedSurface = IMG_Load( path.c_str() );
    if( loadedSurface == NULL ) {
        printf( "Unable to load image %s! SDL_image Error: %s\n", path.c_str(), IMG_GetError() );
    } else {
        image = SDL_ConvertSurface( loadedSurface, surface->format, NULL );
        if( image == NULL ) {
            printf( "Unable to optimize image %s! SDL Error: %s\n", path.c_str(), SDL_GetError() );
        }

        SDL_FreeSurface( loadedSurface );
    }
}

我在getImage()的{​​{1}}中调用方法mainloop(),如下所示,其中Window被声明为sprites

Sprite* sprites[10];

1 个答案:

答案 0 :(得分:4)

你可能在这里索引超出范围

for (int i = 0; i < spritesNum; i++) {
    SDL_BlitScaled( sprites[spritesNum]->getImage() , NULL, screenSurface, sprites[spritesNum]->getRect() );
}

我假设你想使用你的计数器i

for (int i = 0; i < spritesNum; i++) {
    SDL_BlitScaled( sprites[i]->getImage() , NULL, screenSurface, sprites[i]->getRect() );
}

否则,如果您的数组sprites的长度为spritesNum,则您的索引范围超出范围,并且spritesNum次数为sprites[i]->getImage()

在任何情况下,你至少应该确认你没有在这里取消引用空指针

Private Sub cboProvince_SelectionChangeCommitted(ByVal sender _
   As Object, ByVal e As EventArgs) Handles cboProvince.SelectionChangeCommitted

' verify a something was selected 
  If cboProvince.selectedindex>-1 Then
    FillCitiesCombo(cboProvince.selectedValue)
  End If 
End Sub

Private Sub FillCitiesCombo(Province As String)
  dim tmpConn as new sqlconnection(connectionstring)
  tmpConn.open
  try
     Dim sql as string("SELECT CITY FROM CITIES WHERE PROVINCE='"  & Province.tostring & ";")
     Dim adpt As New SQLDataAdapter(sql,tmpConn)
     Dim dt as new datatable
     adpt.fill(dt)
     cboCities.DataSource=dt
     cboCities.ValueMember="City"
     cboCities.DisplayMember="City"
  catch ex as exception

  finally
     tmpConn.Close
  End try
End Sub