Veronic
Veronic

Reputation: 45

Segmentation fault, shared library

I get Segmentation Fault when I try to run my program. Can someone please help me find out what Im doing wrong?

Compiling with this:

    g++ sms_out.cpp -o sms_out
    g++ -c -fPIC SMSDispatch.cpp
    g++ -shared SMSDispatch.o -o libSMSDispatch.so

It should be a shared library and dynamic linking. I get Segmentation Fault when I try to run sms_out.

//sms_out.cpp

#include <iostream>
#include<cstdlib>
#include<fstream>
#include<sstream>
#include<string>
#include "SMSDispatch.h"
using namespace std;

string sms = "";

void sendSMS(string sms)
{
  SMSDispatch* sPtr=0;
  sPtr->sendSMS(sms);
}

int main(int argc, char *argv[])
{
  if(argv[1])
  {
    string input = argv[1];
    string test = "--author";


 if(input == test)
    {
      cout << "s149113" << endl;
      return 0;
    }
  }

  string line = "";
  string file = "sms_out.txt";
  ifstream myfile(file.c_str());


while(getline(myfile, line))
{
      string idnr, landcode, number, error;
      istringstream linestream(line);

      unsigned short errorcode;

      //Split the sentence
      getline(linestream, idnr, '\t');
      getline(linestream, landcode, ':');
      getline(linestream, number, '\t');
      getline(linestream, error);

  if(idnr == "") break;

  //Make string to int
    try
  {
    errorcode = atoi(error.c_str() );
  }
  catch (exception &)
  {
  }
  //Put together landcode and tlfnumber
  string nr = landcode + number;

  string txt = "Thank you for your vote!";
  if(errorcode == 100) txt = "Invalid question, please try again";
  else if(errorcode == 110) txt = "Sorry, only one vote pr. number";
  else if(errorcode == 200) txt = "Invalid alternative, please try again";
  else if(errorcode == 300) txt = "Missing a statement after other, please try again";
  else if(errorcode == 999) txt = "An error occurred, please try again";

  sms += "{\"ID\":" + idnr + ",\"nr\":" + nr + ",\"txt\":" + "\"" + txt + "\"" + "}\n";
  }
  cout << sms << endl;
  sendSMS(sms);

}

//SMSDispatch.h

#include <string>
#ifndef SMSDISPATCH_H
#define SMSDISPATCH_H
using namespace std;

class SMSDispatch{
  public:
  virtual void sendSMS(string json);
  };
#endif

//SMSDispatch.cpp

 #include <iostream>
 #include <fstream>
 #include "SMSDispatch.h"
 using namespace std;

/*virtual*/void SMSDispatch::sendSMS(string json)
{
  ofstream myfile;
  myfile.open ("sms_out.log");
  myfile << json;
  myfile.close();
}

int main()
{

}

Upvotes: 1

Views: 1923

Answers (2)

Attila
Attila

Reputation: 28762

In your sendSMS function in sms_out.cpp you declare a pointer and initialize it to null-poiter (0). The next line tries to access an object via that pointer and call a member function. Since the pointer is null (which means it does not point to a valid object), this operation fails wilth a seg.fault

void sendSMS(string sms) 
{ 
  SMSDispatch* sPtr=0; // this sets pointer to null
                       // assign the address of a valid `SMSDispatch` object instead
  sPtr->sendSMS(sms); 
} 

To fix it, forst you need an instance of that type.

Depending on your need you can do

  • SMSDispatch dp; sPtr=&dp; or
  • sptr=new SMSDispatch;

In the first case, you could just as well do

SMSDispatch dp;
dp.sendSMS(sms);

In the secons case you will need to call delete sptr; after you don't need the object any more.

Also, note that in order to compile the program, the compiler will need the definition of the SMSDispatch::sendSMS function. By including the SMSDispatch.h header, you are only supplying the declaration.

You will either need to

  • add SMSDispatch.cpp to the g++ invokation for direct bundling of the code or
  • link agains the shared library by adding -lSMSDispatch to g++ options after building the shared library from SMSDispatch.cpp

Upvotes: 3

hmjd
hmjd

Reputation: 121961

Dereferencing a NULL pointer will cause a segmentation fault:

void sendSMS(string sms)
{
  SMSDispatch* sPtr=0;
  sPtr->sendSMS(sms);
}

I can't see a reason for using a dynamically allocated object so suggest changing to:

void sendSMS(string sms)
{
  SMSDispatch sPtr;
  sPtr.sendSMS(sms);
}

Upvotes: 9

Related Questions