Seg Fault In My Texture Loading Functions

This is a discussion on Seg Fault In My Texture Loading Functions within the C++ Programming forums, part of the General Programming Boards category; I have two functions, one takes a file name and a pointer to a GLuint. It appears the textures get ...

  1. #1
    Registered User IdioticCreation's Avatar
    Join Date
    Nov 2006
    Location
    Lurking about
    Posts
    229

    Seg Fault In My Texture Loading Functions

    I have two functions, one takes a file name and a pointer to a GLuint. It appears the textures get loaded OK, but I get a segmentation fault in a call to glBindTexture();

    Code:
    #include "textureloader.h"
    
    bool loadTexture(const char* fname, GLuint *texid, GLuint internalF, GLuint byteF)
    {
    	SDL_Surface* imgFile;
    	if ((imgFile = IMG_Load(fname)) == NULL) //Load Image to an SDL surface
    	{
    	    return false;
    	}
        glPixelStorei(GL_UNPACK_ROW_LENGTH,0);
        glGenTextures(1,texid);
        glBindTexture(GL_TEXTURE_2D,*texid); //Segementation Fault NOT here
        glTexParameteri(GL_TEXTURE_2D,GL_TEXTURE_MIN_FILTER,GL_LINEAR);
        glTexParameteri(GL_TEXTURE_2D,GL_TEXTURE_MAG_FILTER,GL_LINEAR);
        glTexImage2D(GL_TEXTURE_2D,0,internalF,imgFile->w, imgFile->h,0,byteF,GL_UNSIGNED_BYTE,imgFile->pixels);
        SDL_FreeSurface(imgFile);
        return true;
    }
    
    bool loadEmUp(char* fname, GLuint *texsToLoad)
    {
        std::string texpath;
        int texnum;
        GLuint internalformat, byteformat;
        int texcount = 0;
        std::ifstream texfile (fname);
        if (texfile.is_open())
        {
            texfile >> texnum; //Now holds the number of textures
            texsToLoad = new GLuint[texnum]; //Create room for the new textures
            while (texcount < texnum)
            {
                texfile >> texpath;
                texfile >> internalformat;
                texfile >> byteformat;
                if (!loadTexture(texpath.c_str(), &texsToLoad[texcount], internalformat, byteformat))
                {
                    std::cout << "Failed to load image: " << texpath << std::endl;
                    return false;
                }
                else std::cout << "Loaded " << texpath << std::endl;
                texcount++;
            }
            texfile.close();
        }
        else return false;
        std::cout << texnum << " Textures Loaded" << std::endl;
        return true;
    }
    The file with textures names in it is something like this:
    Code:
    2
    media/img1.png 3 3
    media/img2.png 3 3
    Segmentation fault does not occur in the the loadTexture() function, it's in a different one. Also: GLuint *texsToLoad is declared as a global where the loadEmUp() functions is called from, but it's named myTextures.
    Last edited by IdioticCreation; 12-26-2007 at 11:04 PM.

  2. #2
    Registered User IdioticCreation's Avatar
    Join Date
    Nov 2006
    Location
    Lurking about
    Posts
    229
    OK, I made a very simplified version of the above code.

    Code:
    #include <iostream>
    
    void func_two(int *toChange)    //like loadTexture()
    {
        *toChange = 7;
    }
    
    void func_one(int **myArray)    //like loadEmUp()
    {
        *myArray = new int[2];
        func_two(myArray[0]);
        func_two(myArray[1]);
    }
    
    int main()
    {
        int *myArray;
        func_one(&myArray);
        std::cout << myArray[0] << std::endl;
        std::cout << myArray[1] << std::endl;
    
        return 0;
    }
    I find it very strange that it outputs:
    7
    0

    I would expect it to output
    7
    7

  3. #3
    C++ Witch laserlight's Avatar
    Join Date
    Oct 2003
    Location
    Singapore
    Posts
    21,717
    In func_one(), myArray is a pointer to an array of ints. So, myArray[0] is *(myArray + 0), i.e., *myArray, which is the array itself. So what you passed to func_two() is the array itself.

    In func_two, toChange is now a pointer to the array. *toChange, therefore, is the first element of the array, thus it is changed to 7.

    Back to func_one(). myArray[1] is *(myArray + 1), i.e., the next array. However, myArray only points to one array, thus this is a mistake. What you should write is:
    Code:
    *myArray = new int[2];
    func_two(&(*myArray)[0]);
    func_two(&(*myArray)[1]);
    Better yet, use pass by reference:
    Code:
    #include <iostream>
    
    void func_two(int& toChange)    //like loadTexture()
    {
        toChange = 7;
    }
    
    void func_one(int*& myArray)    //like loadEmUp()
    {
        myArray = new int[2];
        func_two(myArray[0]);
        func_two(myArray[1]);
    }
    
    int main()
    {
        int *myArray = 0; // To safely delete[] if new[] is not done.
        func_one(myArray);
        std::cout << myArray[0] << std::endl;
        std::cout << myArray[1] << std::endl;
        delete[] myArray; // Remember to delete[] what you new[] !
    }
    C + C++ Compiler: MinGW port of GCC
    Version Control System: Bazaar

    Look up a C++ Reference and learn How To Ask Questions The Smart Way

  4. #4
    Registered User IdioticCreation's Avatar
    Join Date
    Nov 2006
    Location
    Lurking about
    Posts
    229
    Thanks laser. I never understood well how passing by reference is different from passing a pointer. Like function(int* x) to function(int& x). Do you have a link where it's explained well, or would you mind doing it yourself?

    Thanks!

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Access Violation / Seg Fault
    By Korhedron in forum C++ Programming
    Replies: 31
    Last Post: 09-06-2008, 11:47 AM
  2. seg fault problems
    By nadamson6 in forum C++ Programming
    Replies: 11
    Last Post: 12-27-2005, 02:26 PM
  3. Found a seg fault, but can't explain why i get it
    By misplaced in forum C++ Programming
    Replies: 11
    Last Post: 08-29-2005, 02:58 AM
  4. BMP Loading Questions, More OpenGL!!! :D
    By Shamino in forum Game Programming
    Replies: 13
    Last Post: 05-08-2005, 02:31 PM
  5. Pointer To Functions = Seg Fault
    By misplaced in forum C++ Programming
    Replies: 3
    Last Post: 04-05-2005, 08:03 AM

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21