2

I am using a QWebFrame to visualize some data and I use evalueateJavascript method to update data on Javascript. Here is my function to do this.

QWebFrame * webFrame;
void setValue(int idx, double value){

   webPage->page()->mainFrame()->evaluateJavaScript(QString("setDataValue(%1,%2)").arg(QString::number(idx)).arg(QString::number(value)));   
}

In QT application I can call this function via a button call back as many time without causing an error.

I want to call this setValue function from a separate thread to visualize incoming data. When I call setValue function from a separate thread, after few or first iteration application crash. I tried with both QThread and boost threads, but results are same.

void dummyTest(){
  for(int i = 0; i < 1000; i++)
    setValue(0,rand() % 150);
}

This dummyTest function is also working without problems when called via a button callback, but crash on running on a separate thread.

Here is the code for thread initialization

 void startSerialProcessing() {
     boost::thread_attributes attr;
     attr.set_stack_size(1024);

     std::cout << "dummy processor started. \n";
     serialThread= new boost::thread(&MavLinkAL::dummyTest, this);
 }

My observation is this crash only happens when setValue is called from a separate thread. Here is important lines from coredump file viewed from gdb.

Program terminated with signal SIGSEGV, Segmentation fault.

#0  0x00007f3f0d6f361f in WTF::StringImpl::~StringImpl() ()
   from /usr/lib/x86_64-linux-gnu/libQtWebKit.so.4
#1  0x00007f3f0d6583f8 in JSC::JSValue::toStringSlowCase(JSC::ExecState*) const
    () from /usr/lib/x86_64-linux-gnu/libQtWebKit.so.4
#2  0x00007f3f0d68a396 in ?? () from /usr/lib/x86_64-linux-gnu/libQtWebKit.so.4
#3  0x00007f3f0d55a9f1 in ?? () from /usr/lib/x86_64-linux-gnu/libQtWebKit.so.4
#4  0x00007f3f0d56313f in ?? () from /usr/lib/x86_64-linux-gnu/libQtWebKit.so.4

Any help to solve this problem is really appreciated. Thanks.

1 Answer 1

2

QWebFrame functions are neither reentrant nor thread safe. They can only be invoked from main GUI thread. Qt allows you to deliver a message to another thread quite easily. Make setValue function a slot, and connect it to a signal emitted from the processing thread.

EDIT: As Vladimir Bershov suggested in the comment below, one can also use QMetaObject::invokeMethod() with Qt::QueuedConnection to achieve the same result.

Sign up to request clarification or add additional context in comments.

2 Comments

He can also use QMetaObject::invokeMethod() with Qt::QueuedConnection connection type
@Dinesh, could you mark this as answer in that case? ;)

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.