LinuxQuestions.org
Help answer threads with 0 replies.
Go Back   LinuxQuestions.org > Forums > Non-*NIX Forums > Programming
User Name
Password
Programming This forum is for all programming questions.
The question does not have to be directly related to Linux and any language is fair game.

Notices


Reply
  Search this Thread
Old 09-14-2011, 08:12 AM   #1
shamjs
Member
 
Registered: Sep 2011
Posts: 88

Rep: Reputation: Disabled
Lightbulb 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..........
 
Old 09-14-2011, 09:15 AM   #2
johnsfine
LQ Guru
 
Registered: Dec 2007
Distribution: Centos
Posts: 5,286

Rep: Reputation: 1191Reputation: 1191Reputation: 1191Reputation: 1191Reputation: 1191Reputation: 1191Reputation: 1191Reputation: 1191Reputation: 1191
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.
 
Old 09-14-2011, 09:46 AM   #3
ta0kira
Senior Member
 
Registered: Sep 2004
Distribution: FreeBSD 9.1, Kubuntu 12.10
Posts: 3,078

Rep: Reputation: Disabled
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
 
1 members found this post helpful.
Old 09-15-2011, 01:14 AM   #4
shamjs
Member
 
Registered: Sep 2011
Posts: 88

Original Poster
Rep: Reputation: Disabled
stil im having seg fault problem

Quote:
Originally Posted by johnsfine View Post
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..............
 
Old 09-15-2011, 01:48 AM   #5
paulsm4
LQ Guru
 
Registered: Mar 2004
Distribution: SusE 8.2
Posts: 5,863
Blog Entries: 1

Rep: Reputation: Disabled
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
 
Old 09-15-2011, 07:09 AM   #6
dwhitney67
Senior Member
 
Registered: Jun 2006
Location: Maryland
Distribution: Kubuntu, Fedora, RHEL
Posts: 1,533

Rep: Reputation: 335Reputation: 335Reputation: 335Reputation: 335
Quote:
Originally Posted by shamjs View Post
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.
 
Old 09-15-2011, 07:13 AM   #7
shamjs
Member
 
Registered: Sep 2011
Posts: 88

Original Poster
Rep: Reputation: Disabled
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
 
Old 09-15-2011, 07:56 AM   #8
ta0kira
Senior Member
 
Registered: Sep 2004
Distribution: FreeBSD 9.1, Kubuntu 12.10
Posts: 3,078

Rep: Reputation: Disabled
Quote:
Originally Posted by shamjs View Post
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
 
Old 09-15-2011, 08:13 AM   #9
shamjs
Member
 
Registered: Sep 2011
Posts: 88

Original Poster
Rep: Reputation: Disabled
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;
}
 
Old 09-15-2011, 10:33 AM   #10
dwhitney67
Senior Member
 
Registered: Jun 2006
Location: Maryland
Distribution: Kubuntu, Fedora, RHEL
Posts: 1,533

Rep: Reputation: 335Reputation: 335Reputation: 335Reputation: 335
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.
 
Old 09-15-2011, 11:43 AM   #11
ta0kira
Senior Member
 
Registered: Sep 2004
Distribution: FreeBSD 9.1, Kubuntu 12.10
Posts: 3,078

Rep: Reputation: Disabled
Quote:
Originally Posted by dwhitney67 View Post
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
 
Old 09-16-2011, 03:30 AM   #12
shamjs
Member
 
Registered: Sep 2011
Posts: 88

Original Poster
Rep: Reputation: Disabled
thanks

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


Reply


Thread Tools Search this Thread
Search this Thread:

Advanced Search

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is Off
HTML code is Off



Similar Threads
Thread Thread Starter Forum Replies Last Post
Segmentation fault chobo Linux - Newbie 2 10-30-2008 05:13 AM
yast segmentation fault, system freezing - nvidia driver at fault? BaltikaTroika SUSE / openSUSE 2 12-02-2005 10:34 AM
segmentation fault r_x Linux - Software 3 04-26-2005 06:22 AM
Segmentation fault Varadharajan Programming 5 04-22-2005 11:26 AM
Segmentation Fault XPediTioN Slackware 2 09-18-2003 09:16 AM

LinuxQuestions.org > Forums > Non-*NIX Forums > Programming

All times are GMT -5. The time now is 02:27 AM.

Main Menu
Advertisement
My LQ
Write for LQ
LinuxQuestions.org is looking for people interested in writing Editorials, Articles, Reviews, and more. If you'd like to contribute content, let us know.
Main Menu
Syndicate
RSS1  Latest Threads
RSS1  LQ News
Twitter: @linuxquestions
Facebook: linuxquestions Google+: linuxquestions
Open Source Consulting | Domain Registration