By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
455,515 Members | 1,772 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 455,515 IT Pros & Developers. It's quick & easy.

callback confusion!

P: 42
I am so confused... do you have any idea how I can come over this segmentation fault?

Expand|Select|Wrap|Line Numbers
  1.  
  2. class myProgramTest : public EventInterface
  3. {
  4. public:
  5.     void myMainProg (void);
  6.     static void onMess(Event*, int);
  7. private:
  8.     static int subscriptionID2, subscriptionID,subscriptionID1;    
  9.  
  10. };
  11.  
  12. void myProgramTest::onMess(Event* _event, int subID){
  13. ...
  14. }
  15.  
  16. void myProgramTest::myMainProg(){
  17.  
  18.     try{
  19.  
  20.         callBackFunc receiveTest;
  21.         receiveTest.onMessageFunction=&myProgramTest::onMess;    
  22.  
  23.         EventInterface * myEventInt= new EventInterface(receiveTest);
  24.                 ......
  25.  
  26. }
  27.  
  28. class EventInterface 
  29. {
  30.  
  31. public:
  32.  
  33.     typedef struct{
  34.         void (*onMessageFunction)(Event*, int);
  35.     } callBackFunc;
  36.  
  37.     EventInterface();
  38.     EventInterface(callBackFunc);
  39.     static void * receiveEvent (void *);
  40.         ....
  41. }
  42.  
  43. EventInterface::EventInterface(callBackFunc ptToFunc){
  44.  
  45.     socketAddr= EventManager::getSocketAddr();
  46.  
  47.     if (pthread_create( &receiveThread, NULL, EventInterface::receiveEvent, (void *) &ptToFunc))
  48.         throw EventException ("EVENTINTERFACE_RECEIVE_THREAD_IS_NOT_CREATED");
  49.  
  50. }
  51.  
  52.  
  53. void * EventInterface::receiveEvent(void * arg){
  54.  
  55.         callBackFunc * onMessageFunc;
  56.     onMessageFunc= (callBackFunc*) arg;
  57.         .........
  58.         onMessageFunc->onMessageFunction(notifiedEvent , subscriptionID);
  59.  
  60. }
  61.  
I receive a segmentation fault from this line :
Expand|Select|Wrap|Line Numbers
  1. onMessageFunc->onMessageFunction(notifiedEvent , subscriptionID);
Do you have any idea!!!! PLEASE!

Thanks,

Amir.
May 31 '07 #1
Share this Question
Share on Google+
3 Replies


AdrianH
Expert 100+
P: 1,251
I am so confused... do you have any idea how I can come over this segmentation fault?

Expand|Select|Wrap|Line Numbers
  1.  
  2. class myProgramTest : public EventInterface
  3. {
  4. public:
  5.     void myMainProg (void);
  6.     static void onMess(Event*, int);
  7. private:
  8.     static int subscriptionID2, subscriptionID,subscriptionID1;    
  9.  
  10. };
  11.  
  12. void myProgramTest::onMess(Event* _event, int subID){
  13. ...
  14. }
  15.  
  16. void myProgramTest::myMainProg(){
  17.  
  18.     try{
  19.  
  20.         callBackFunc receiveTest;
  21.         receiveTest.onMessageFunction=&myProgramTest::onMess;    
  22.  
  23.         EventInterface * myEventInt= new EventInterface(receiveTest);
  24.                 ......
  25.  
  26. }
  27.  
  28. class EventInterface 
  29. {
  30.  
  31. public:
  32.  
  33.     typedef struct{
  34.         void (*onMessageFunction)(Event*, int);
  35.     } callBackFunc;
  36.  
  37.     EventInterface();
  38.     EventInterface(callBackFunc);
  39.     static void * receiveEvent (void *);
  40.         ....
  41. }
  42.  
  43. EventInterface::EventInterface(callBackFunc ptToFunc){
  44.  
  45.     socketAddr= EventManager::getSocketAddr();
  46.  
  47.     if (pthread_create( &receiveThread, NULL, EventInterface::receiveEvent, (void *) &ptToFunc))
  48.         throw EventException ("EVENTINTERFACE_RECEIVE_THREAD_IS_NOT_CREATED");
  49.  
  50. }
  51.  
  52.  
  53. void * EventInterface::receiveEvent(void * arg){
  54.  
  55.         callBackFunc * onMessageFunc;
  56.     onMessageFunc= (callBackFunc*) arg;
  57.         .........
  58.         onMessageFunc->onMessageFunction(notifiedEvent , subscriptionID);
  59.  
  60. }
  61.  
I receive a segmentation fault from this line :
Expand|Select|Wrap|Line Numbers
  1. onMessageFunc->onMessageFunction(notifiedEvent , subscriptionID);
Do you have any idea!!!! PLEASE!

Thanks,

Amir.
The problem is you are passing the address of the pointer, instead of the address of the function. Remove the & infront of ptToFunc (line 46).


Adrian
May 31 '07 #2

P: 42
The problem is solved by these changes:

42. EventInterface::EventInterface(callBackFunc * ptToFunc){
19. callBackFunc* receiveTest;
20. receiveTest->onMessageFunction=&myProgramTest::onMess;
55. onMessageFunc= *(callBackFunc*) arg;
46. if (pthread_create( &receiveThread, NULL, EventInterface::receiveEvent, (void *) ptToFunc))
54. callBackFunc onMessageFunc;
55. onMessageFunc= (callBackFunc) arg;
57. onMessageFunc.onMessageFunction(notifiedEvent , subscriptionID);

Thanks Adrian by the way,

Amir.
May 31 '07 #3

AdrianH
Expert 100+
P: 1,251
The problem is solved by these changes:

42. EventInterface::EventInterface(callBackFunc * ptToFunc){
19. callBackFunc* receiveTest;
20. receiveTest->onMessageFunction=&myProgramTest::onMess;
55. onMessageFunc= *(callBackFunc*) arg;
46. if (pthread_create( &receiveThread, NULL, EventInterface::receiveEvent, (void *) ptToFunc))
54. callBackFunc onMessageFunc;
55. onMessageFunc= (callBackFunc) arg;
57. onMessageFunc.onMessageFunction(notifiedEvent , subscriptionID);

Thanks Adrian by the way,

Amir.
Sorry, looks like I misdirected you. I misinterpreted the callBackFunc as a function pointer not as an object. You don't need to typedef a struct in C++, just use struct like you would a class.

Good to hear that you found the problem though.


Adrian
May 31 '07 #4

Post your reply

Sign in to post your reply or Sign up for a free account.