im getting segmentation fault
im getting segfault in msg=(Msg)GetFunction(hDLL<Display); line
but i think i have not done any memory leaks and moreover the same code is compiling successfuly in windows but not in linux ,plz give me an solution for this. /**********client.cpp****************/ #include "os_api.h" #include <iostream> using namespace std; typedef int (*AddFnPtr)(int,int); typedef int (*Msg)(int,int); int main(int argc, char* argv[]) { int i; AddFnPtr AddFn; Msg msg; void *hDLL; hDLL = LoadSharedLibrary("pgm"); if(hDLL == 0) return 1; msg=(Msg)GetFunction(hDLL,"Display");//here im getg err i=msg(5,6); cout<<i; AddFn = (AddFnPtr)GetFunction(hDLL,"fnAdd"); int iTmp = AddFn(8,5); cout<<"8 + 5 = "<<iTmp; FreeSharedLibrary(hDLL); cin.get(); return 0; } /**************os_api.h***************/ #ifndef os_call_h #define os_call_h #if defined(_MSC_VER) #include <windows.h> #elif defined(__GNUC__) #include <dlfcn.h> #else #error define your copiler #endif #include<string> void* LoadSharedLibrary(char *pcDllname, int iMode = 2) { std::string sDllName = pcDllname; #if defined(_MSC_VER) sDllName += ".dll"; return (void*)LoadLibrary(pcDllname); #elif defined(__GNUC__) sDllName += ".so"; return dlopen(sDllName.c_str(),iMode); #endif } void *GetFunction(void *Lib, char *Fnname) { #if defined(_MSC_VER) return (void*)GetProcAddress((HINSTANCE)Lib,Fnname); #elif defined(__GNUC__) return dlsym(Lib,Fnname); #endif } bool FreeSharedLibrary(void *hDLL) { #if defined(_MSC_VER) return FreeLibrary((HINSTANCE)hDLL); #elif defined(__GNUC__) return dlclose(hDLL); #endif } #endif plz let me know where i have to make changes in code thanks in advance.......... |
Please use CODE tags to post more readable source code.
Are you sure about where the seg fault occurs in your code? I don't see a bug that fits that symptom. If the seg fault were on the next line, it would simply indicate the function had not been found in the .so file. Try inserting a cout line to display the value of msg after getting it and before using it. On any cout line inserted for debugging, remember to end with endl, so the output won't be lost to buffering in a seg fault that occurs after the cout. |
You should compile with -g and run it in gdb. Then you can get a backtrace when it crashes, which should tell you where exactly the segfault happens.
Kevin Barry |
stil im having seg fault problem
Quote:
value of msg before using is 1 Segmentation fault means after that line(has i previously told)its not executing,i have commented each part of the pgm keenly and i have came to that conclusion that ,that the line i have mentioned previously as having segfault. im waiting for your input.............. |
Hi -
Quote:
Quote:
The debugger (for example, gdb) is your friend. Honest :) Here's a a very (very!) short tutorial that tells you everything you should need to get started: http://cs.baylor.edu/~donahoo/tools/gdb/tutorial.html 'Hope that helps .. PSM |
Quote:
Here's the solution to the significant ones: Code:
... |
i have fixed it,it was due to name mangling,but.....
thanx for your kind support, finally i have traced the problem using gdb debugger,the problem is name mangling is done on that function so i was getting segfault(i have passed the mangled name itself as parameter to the function GetFunction) but i dont want to use this c++ mangled name so i have changed my pgm.h file
Code:
/****************pgm.h***********************/ even after using extern "C" im not able to use the exported functios. is im declaring extern "C" wrong way plz suggest me |
Quote:
Kevin Barry |
i hav included pgm.h
ya i have included #include"pgm.h" in the pgm.cpp(which has defination of the functions) here is my pgm.cpp
Code:
/*********pgm.cpp*************/ |
For Linux (GNU), it is "libpgm.so" that you want to open, not "pgm.so".
Fix your os_api.h LoadSharedLibrary() function so that the correct library name is used. |
Quote:
Kevin Barry |
thanks
thanks for your help...............
|
All times are GMT -5. The time now is 07:47 PM. |