Page 1 of 1

Segmentation fault with function pointers

Posted: Fri Aug 12, 2011 9:35 am
by ajtgarber
I've just started playing with creating shared libraries (and libraries in general) and I've come across an issue. In my library I have a function called setInitFunc which takes a function pointer to a function that returns nothing and takes nothing.
void (*initFunc)(void), when I build my test application the program crashes midway through the function. The first line is a debug saying "setInitFunc()", last line says "endInitFunc", the middle line just sets one of my global function pointers to what was passed in. (Relevant code below, if you need more just ask). This is being done in C using Codeblocks on Ubuntu 10.04.

Definition of setInitFunc (in FrozenCore.c)

Code: Select all

void setInitFunc( void (*initFunc)(void) ) {
     printf("setInitFunc()\n");
    init = initFunc;
    printf("endInitFunc\n");
}
main.c

Code: Select all

#include "FrozenCore.h"

void init() {

}
void update() {

}
void render(SDL_Surface* screen) {
    SDL_Rect rect = (SDL_Rect) {25, 25, 25, 25};
    SDL_FillRect(screen, &rect, SDL_MapRGB(screen->format, 255, 255, 0));
}

int main(int argc, char* argv[]) {
    setInitFunc(init);
    setUpdateFunc(update);
    setRenderFunc(render);
    startGame(640, 480, 0);
    return 0;
}

Re: Segmentation fault with function pointers

Posted: Fri Aug 12, 2011 10:11 am
by Falco Girgis
I see nothing wrong with the way you are handling that function pointer. Either 1) I'm missing something 2) Your problem is something else or 3) You need to post more code.

Re: Segmentation fault with function pointers

Posted: Fri Aug 12, 2011 3:05 pm
by Nokurn

Code: Select all

init = initFunc;
It would appear that init is a variable somewhere in your library, yes? Like this:

Code: Select all

void (*init)(void);
But you have another symbol in main.c called init...

Code: Select all

void init() {

}
I don't use shared libraries all that often myself (I prefer static libraries for a number of reasons), so I may be completely off on this, but having two symbols of the same name seems a little suspicious to me. If you were using a static library, you would be getting a linker error. I'd try changing the name of your init variable to something else.

Edit: Or make sure that your function pointer is declared as static.

Re: Segmentation fault with function pointers

Posted: Sat Aug 13, 2011 2:42 pm
by ajtgarber
I renamed the functions in my test program and it ran well, I renamed them back to what they were before and made the function pointers static and it also worked.
Thanks! It always seems to be something really small that trips me up :oops:

Re: Segmentation fault with function pointers

Posted: Sun Aug 14, 2011 3:02 pm
by Falco Girgis
Krolgar wrote:

Code: Select all

init = initFunc;
It would appear that init is a variable somewhere in your library, yes? Like this:

Code: Select all

void (*init)(void);
But you have another symbol in main.c called init...

Code: Select all

void init() {

}
I don't use shared libraries all that often myself (I prefer static libraries for a number of reasons), so I may be completely off on this, but having two symbols of the same name seems a little suspicious to me. If you were using a static library, you would be getting a linker error. I'd try changing the name of your init variable to something else.

Edit: Or make sure that your function pointer is declared as static.
Damn, that was a good fucking catch.

Re: Segmentation fault with function pointers

Posted: Sun Aug 14, 2011 4:01 pm
by Nokurn
ajtgarber wrote:I renamed the functions in my test program and it ran well, I renamed them back to what they were before and made the function pointers static and it also worked.
Thanks! It always seems to be something really small that trips me up :oops:
You're welcome! If you don't know what the static keyword does, you should definitely read up on it. I find them very useful when I'm writing libraries in C. You should have seen how happy I was when I found out that C++ gives a cleaner syntax for accomplishing the same thing (anonymous namespaces). :)
GyroVorbis wrote:Damn, that was a good fucking catch.
I have an eye for faulty code. ;)