LinuxQuestions.org

LinuxQuestions.org (/questions/)
-   Programming (https://www.linuxquestions.org/questions/programming-9/)
-   -   im getting segmentation fault (https://www.linuxquestions.org/questions/programming-9/im-getting-segmentation-fault-902963/)

shamjs 09-14-2011 07:12 AM

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..........

johnsfine 09-14-2011 08:15 AM

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.

ta0kira 09-14-2011 08:46 AM

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

shamjs 09-15-2011 12:14 AM

stil im having seg fault problem
 
Quote:

Originally Posted by johnsfine (Post 4471442)
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.

thanx a lot johnsfine for your inputs ,but has you said i inserted a cout(with endl) before and after the usage of msg, then when i finally compiled i got the same error as
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..............

paulsm4 09-15-2011 12:48 AM

Hi -

Quote:

I'm waiting for your input.
My input is to do exactly what was already suggested:
Quote:

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.
Carefully step through each of your functions. I looked at them briefly - I don't see anything blatantly wrong. But the debugger will tell you for sure.

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

dwhitney67 09-15-2011 06:09 AM

Quote:

Originally Posted by shamjs (Post 4471390)
plz let me know where i have to make changes in code
thanks in advance..........

Get into the habit of compiling your C++ code with both the -Wall and -pedantic options. You will see that there are some unresolved issues with your original code that produce a couple of significant warnings, and a couple of non-significant ones.

Here's the solution to the significant ones:
Code:

...

  *(void**) (&msg) = GetFunction(hDLL, "Display");

  if (!msg)
      return 2;


...

  *(void**) (&AddFn) = GetFunction(hDLL, "fnAdd");

  if (!AddFn)
      return 3;


...

Make sure that the functions you are attempting to reference from the library match the signature you have indicated in your code.

shamjs 09-15-2011 06:13 AM

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***********************/
#if defined(_MSC_VER)
    #include <windows.h>
#endif
#if defined(__GNUC__)
        #include <dlfcn.h>
#endif
#define ADD_API __declspec(dllexport)
#if defined(_MSC_VER)
ADD_API
#endif
#if defined(__GNUC__)
#ifdef __cplusplus
extern "C" {
#endif
 int fnAdd(int, int);
 int Display(int,int);
#ifdef __cplusplus
}
#endif
#endif


even after using extern "C" im not able to use the exported functios. is im declaring extern "C" wrong way plz suggest me

ta0kira 09-15-2011 06:56 AM

Quote:

Originally Posted by shamjs (Post 4472338)
even after using extern "C" im not able to use the exported functios. is im declaring extern "C" wrong way plz suggest me

Do you #include "pgm.h" in the file that defines fnAdd and Display? You need the extern "C" declarations prior to the definitions to prevent name mangling.
Kevin Barry

shamjs 09-15-2011 07:13 AM

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*************/
#include<stdio.h>
#include<stdlib.h>
#include<iostream>
#include "pgm.h"
using namespace std;
#if defined(_MSC_VER)
        bool APIENTRY DllMain( HANDLE hModule,
                      DWORD  ul_reason_for_call,
                      LPVOID lpReserved
                                )
#elif defined(__GNUC__)
        bool DllMain( void * hModule,
                      unsigned long  ul_reason_for_call,
                      void *lpReserved
                                )

#endif
{
    switch (ul_reason_for_call)
        {
                case 1://DLL_PROCESS_ATTACH:
                case 2:// DLL_THREAD_ATTACH:
                case 3://DLL_THREAD_DETACH:
 case 0://DLL_PROCESS_DETACH:
                        break;
    }
    return true;
}

int fnAdd(int a, int b)
{
        return (a+b);
}

int  Display(int m,int n)
{
  int c =m*n;
cout<<"hai linux and windows<<endl;
return m;
}


dwhitney67 09-15-2011 09:33 AM

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.

ta0kira 09-15-2011 10:43 AM

Quote:

Originally Posted by dwhitney67 (Post 4472484)
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.

It wouldn't be getting to GetFunction without the correct library name. It could be the OP has chosen to name it without "lib".
Kevin Barry

shamjs 09-16-2011 02:30 AM

thanks
 
thanks for your help...............


All times are GMT -5. The time now is 07:47 PM.