Segmentation fault with function pointers

Whether you're a newbie or an experienced programmer, any questions, help, or just talk of any language will be welcomed here.

Moderator: Coders of Rage

Post Reply
ajtgarber
Chaos Rift Cool Newbie
Chaos Rift Cool Newbie
Posts: 97
Joined: Wed Jun 10, 2009 8:56 am

Segmentation fault with function pointers

Post 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;
}
User avatar
Falco Girgis
Elysian Shadows Team
Elysian Shadows Team
Posts: 10294
Joined: Thu May 20, 2004 2:04 pm
Current Project: Elysian Shadows
Favorite Gaming Platforms: Dreamcast, SNES, NES
Programming Language of Choice: C/++
Location: Studio Vorbis, AL
Contact:

Re: Segmentation fault with function pointers

Post 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.
User avatar
Nokurn
Chaos Rift Regular
Chaos Rift Regular
Posts: 164
Joined: Mon Jan 31, 2011 12:08 pm
Favorite Gaming Platforms: PC, SNES, Dreamcast, PS2, N64
Programming Language of Choice: Proper C++
Location: Southern California
Contact:

Re: Segmentation fault with function pointers

Post 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.
ajtgarber
Chaos Rift Cool Newbie
Chaos Rift Cool Newbie
Posts: 97
Joined: Wed Jun 10, 2009 8:56 am

Re: Segmentation fault with function pointers

Post 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:
User avatar
Falco Girgis
Elysian Shadows Team
Elysian Shadows Team
Posts: 10294
Joined: Thu May 20, 2004 2:04 pm
Current Project: Elysian Shadows
Favorite Gaming Platforms: Dreamcast, SNES, NES
Programming Language of Choice: C/++
Location: Studio Vorbis, AL
Contact:

Re: Segmentation fault with function pointers

Post 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.
User avatar
Nokurn
Chaos Rift Regular
Chaos Rift Regular
Posts: 164
Joined: Mon Jan 31, 2011 12:08 pm
Favorite Gaming Platforms: PC, SNES, Dreamcast, PS2, N64
Programming Language of Choice: Proper C++
Location: Southern California
Contact:

Re: Segmentation fault with function pointers

Post 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. ;)
Post Reply