0

I am using Solaris. The code where i am getting segmentation fault is inside .so .We have few static functions, basically these functions are callback functions. I set these functions as callback functions by passing its address to another function. Only for one of the function its address before passing as an argument looks correct , but after stepping into the function to set the function pointer, it changed into some junk pointer and calling the function by that pointer crashes. I am putting some sample code, its not the actual code, but it is similar to that.

In A.cpp, i have three callback functions

static void Func1(A* ptr)
{
}

static void Func2(A* ptr)
{
}

static void Func3(A* ptr)
{
}

In one register function, i assign these as callback functions

void vRegisterCallback(void (*ptrToFunc)(void*))
{
}

void Register()
{
    vRegisterCallback(void (*)(void*)Func1);
    vRegisterCallback(void (*)(void*)Func2);
    vRegisterCallback(void (*)(void*)Func3);
}

I register three function as like above.
Func1 pointer is valid, but when it is passed as an argument to vRegisterCallback. vRegisterCallback is having different address than Func1.

If I add a printf statement above it works with valid pointer sent to vRegisterCallback.
If I change the Func1 from static to non static it works...
If I change the .so to .a , it works.

Dont know what is the problem.

6
  • Are you generating position independent code (-fpic)?
    – aggsol
    Apr 17, 2013 at 11:37
  • I would suspect stack corruption. Add a watchpoint to the variable holding the function pointer and run your code within gdb.
    – trojanfoe
    Apr 17, 2013 at 11:41
  • I believe casting from one function pointer to another is a non-standard extension. So you will have to mention which compiler that is used. Perhaps your compiler does not support it.
    – Lundin
    Apr 17, 2013 at 11:43
  • 1
    Your program invokes undefined behavior: function pointers arr expected to be of type void (*)(void *) but actually they're of type void (*)(A *).
    – user529758
    Apr 17, 2013 at 11:48
  • Your example won't even compile. Apr 17, 2013 at 11:56

0

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Browse other questions tagged or ask your own question.