0
votes

I'm trying to learn SDL 2.0 and I've been following lazyfoo tutorials. The problem is that those tutorials keeps everything in one file. So I tried to split some things up when starting a new game-project. My problem is that when I seperated my texture class from the rest it sometimes crashes the program, not always but pretty often.

I have a global window and a global renderer which I'm using in my class. I get segfault at difference places in the program but always in one of the following functions.

bool myTexture::loadFromFile(string path) {
  printf("In loadFromFile\n");

  //Free preexisting texture
  free();
  //The final texture
  SDL_Texture *l_newTexture = NULL;

  //Load image at specified path
  SDL_Surface *l_loadedSurface = IMG_Load(path.c_str());
  if(l_loadedSurface == NULL) {
    printf("Unable to load image %s! SDL_image Error: %s\n", path.c_str(),      IMG_GetError());
  } else {
    //Color key image for transparancy
    //SDL_SetColorKey(l_loadedSurface, SDL_TRUE, SDL_MapRGB(l_loadedSurface->format, 0,  0xFF, 0xFF));
    //Create texture from surface pixels
    l_newTexture = SDL_CreateTextureFromSurface(g_renderer, l_loadedSurface);

    if(l_newTexture == NULL) {
      printf("Unable to create texture from %s! SDL Error: %s\n", path.c_str(),   SDL_GetError());
    } else {
      //Get image dimensions
      m_width = l_loadedSurface->w;
      m_height = l_loadedSurface->h;
    }
    //Get rid of old loaded surface
    SDL_FreeSurface(l_loadedSurface);
  }
  m_texture = l_newTexture;
  //return success
  printf("end from file \n");

  return m_texture != NULL;
}

void myTexture::free() {
  printf("In myTexture::free\n");
  //Free texture if it exist
  if(m_texture != NULL) {
    cout << (m_texture != NULL) << endl;
    SDL_DestroyTexture(m_texture);
    printf("Destroyed m_texture\n");
    m_texture = NULL;
    m_width   = 0;
    m_height  = 0;
  }
  printf("end free\n");
}

After reading on SDL and other stuffs I understood that there might be some thread trying to deallocate something which it isn't allowed to deallocate. However I haven't threaded anything yet.

1
Is m_texture initialized to a null pointer in the myTexture constructor?rems4e
Yes here are the constructor myTexture::myTexture() { //Initialize m_texture = NULL; m_width = 0; m_height = 0; }Redhat
Did you get it working as it shows in LazyFoo's tutorial before you started to modify it?Zammalad
Code looks ok. Run it in debugger to see where exactly you have a crash and what values variables have at that moment.keltar
Yes zammalad. The problem is that it crashes at different points in these functions so I'm having problem founding out what makes it crash. And sometimes it runs great. Could it some other part of the program that makes it crash even though it always crashes in one of these functions?Redhat

1 Answers

0
votes

I managed to solve this by my own. It turned out that I never created a new myTexture object which was really idiotic. But I still don't understand how it managed to render it sometimes... for me it don't make any sense at all. I never created it but I could still call its render function sometimes...