简体   繁体   中英

Boost signals : Connect fails

I have a public method in a C++ class (let's call it classA) which is defined as:

void someFunction(someOtherCppClassB* arg);

This 'someFunction' method does the following:

theConnection = registerFunction(aPtrToAnInstanceOfCStruct1, boost::bind(&classA::callbackFunction, this, _1));

where 'theConnection' is a private member variable in ClassA and it has the type boost::signals2::connection

and where 'callbackFunction' is a private method in classA defined as:

int callbackFunction(cStruct2** doublePtr);

and where 'aPtrToAnInstanceOfCStruct1' is a pointer to an instance of this C struct:

typedef struct cStruct1T
{
       boost::signals2::signal<int (cStruct2**)> signalVariable;
} cStruct1

and cStruct2 is defined as:

typedef struct cStruct2T
{
   /* Variables in this struct */
       char someDummyVariable;
} cStruct2

The 'registerFunction' is defined as:

boost::signals2::connection registerFunction(cStruct1* aPtrToAnInstanceOfCStruct1, boost::function<int (cStruct2**)> someCallbackFunction)
{
        /* THIS FAILS FOR SOME REASON */
        return aPtrToAnInstanceOfCStruct1->signalVariable.connect(someCallbackFunction);

}

For some reason the .connect() generates the following on Android 4.3:

F/libc ( 8556): Fatal signal 11 (SIGSEGV) at 0x0000000c (code=1), thread 8556

I can't figure out what causes the error. So I'm hoping that there's a Boost expert out there who can take a look at the code above and tell me what I'm doing wrong.

Thank you.

I'm sorry but I find your example hard to follow. But since it compiles, I suspect that you may have a null pointer, judging by the error code.

I recommend encapsulating the signals code in the class containing the signal and provide a function to connect your slot eg:

typedef struct cStruct1T
{
   typedef boost::signals2::signal<int (cStruct2**)> SignalTypeName;
   SignalTypeName signalVariable;

   void connectSlot(const SignalTypeName::slot_type& slot)
   { signalVariable.connect(slot); }

} cStruct1;

You can then register your slot within your someFunction like this:

aPtrToAnInstanceOfCStruct1->connectSlot(boost::bind(&classA::callbackFunction, this, _1));

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM