-1

i think this is mostly a syntax error ( though not captured during compilation, only at runtime does the error happen)

i am trying to develop an event driven architecture , where i keep a vector of eventReceiver and try to call them from the vector.

CommonIO.h

    class CommonIO {
    public:
        typedef void (*incomingMsgReceiver)(char*);
            void registerForIncomingMsg(incomingMsgReceiver);
            void incomingMsgReceived(char*);
    }

CommonIO.cpp

    std::vector<void (*)(char*)> listeners;

    void CommonIO::registerForIncomingMsg(incomingMsgReceiver receiverFunction) {
        listeners.push_back(receiverFunction);
    }

    void CommonIO::incomingMsgReceived(char* buf) {
        for (unsigned int j = 0; j < listeners.size(); j++) {
                listeners[i](buf);  //error, segmentation fault..how do i call it?
            }
    }

Main.h

class Main {
public:
     static void msgReceived(char*);
     void mainLoop();

}

Main.cpp

void Main::msgReceived(char* msg)
{
    printf("\n\n --------->>>>> %s \n\n" , msg);
}

void Main::mainLoop()
{
     CommonIO::incomingMsgReceiver receiver = &Main::msgReceived;
     CommonIO::getInstance()->registerForIncomingMsg( receiver );
}

incomingMsgReceived function is called by an asynchronous process

the program compiles fine..however breaks down at : listenersi; line. what is the proper syntax for this ?

Shrouk Khan
  • 1,440
  • 6
  • 27
  • 53
  • I've no idea how you can get a segmentation fault inside `CommonIO::incomingMsgReceived` when this function is never called. – CB Bailey Jan 01 '13 at 16:28
  • 1
    I don't believe that's your real code (what is `i`?). Please consider creating an [SSCCE](http://sscce.org). – Oliver Charlesworth Jan 01 '13 at 16:28
  • In your `incomingMsgReceived`, what's the name of the argument, where does `buf` come from, and why does it need casting? – DCoder Jan 01 '13 at 16:29
  • What is buf? Or i for that matter? – Mats Petersson Jan 01 '13 at 16:29
  • hi you guys are right..this is part of a quite large program..and difficult to paste the whole thing here. however, i have edited the code a bit for better understanding. - incomingMsgReceived is called asynchronously by another process. - buf is the received msg that is to be passed along however,all these works, except the call to listeners[i](buf); which is where i am not using proper syntax ( most likely ). – Shrouk Khan Jan 01 '13 at 16:40
  • Look at that function again. The loop uses `unsigned int j`, but you're indexing the array using `i`. You either have a typo there, or you have a higher-scoped variable called `i`. – DCoder Jan 01 '13 at 16:45
  • arghh..you are absolutely correct..thats what caused the issue! thankyouthankyou! – Shrouk Khan Jan 01 '13 at 16:57
  • A syntax error not captured by compilation? There is no such thing... – K-ballo Jan 01 '13 at 23:55

1 Answers1

2

Why not use inheritance and a common base class? Makes simpler code and also you are detailing with objects so you have data, other methods etc.

i.e. something like this

class incomingMsgReceiver {
   public:
      virtual void msgReceived(char *msg) = 0;
};

class MyMessage : public incomingMsgReceiver {
   public virual void msgReceieved(char *msg);
}

class OtherMyMessage : public incomingMsgReceiver {
   public virual void msgReceieved(char *msg);
}

void MyMessage::msgReceived(char *msg)
{
   /// Do stuff here

}

void msgReceived::OtherMyMessage(char *msg)
{
   /// Do stuff here

}

Then for your common.h:

class CommonIO
{
   private:
      std::vector<incomingMsgReceiver *> listeners;
   public:
      void addListner(incomingMsgReceiver *reveiver) { listners.push_back(reveiver); }

      void incomingMsgReceived(char*msg)
      {
         for (unsigned int j = 0; j < listeners.size(); j++)
         {
            listeners[j]->msgReceived((msg);
         }
      }
};
Brooks Moses
  • 9,267
  • 2
  • 33
  • 57
Ed Heal
  • 59,252
  • 17
  • 87
  • 127
  • the error was pointed out correctly by DCoder . the problem was a very silly index issue. however, before i noticed his reply..i also tried out your suggestion and this also works fine! – Shrouk Khan Jan 01 '13 at 16:59