
Originally Posted by
cqubed
Is my worker thread accessing main window subclass?
It is.
Furthermore if you pass a string pointer to the constructor of the event, it will potentially cause a double delete because you are storing a pointer to the string as-is, without making a copy of the string that you later delete in the destructor.
I suggest you simplify your code as much as possible, then it will be much simpler to find the problem. For instance this:
if (( m_PtrWorkerThread
!= NULL) && (QThread::currentThread()== m_PtrWorkerThread
)) { while(m_MustWait) {
#ifdef WIN32
Sleep(1);
#else
usleep(10);//1,000,000 in sec
#endif
}
{
m_MustWait = true;
m_PtrMsg = msg;
PostMessageEvent *ptr_msgEvent = new(PostMessageEvent);
ptr_msgEvent
->m_ptrPostingThread
=QThread::currentThread();
ptr_msgEvent->setMsg(msg);
}
} else
{
// m_MustWait = true;
PostMessageEvent *ptr_msgEvent = new(PostMessageEvent);
ptr_msgEvent
->m_ptrPostingThread
=QThread::currentThread();
ptr_msgEvent->setMsg(msg);
}
};
void writeWin(QString *msg) {
if (( m_PtrWorkerThread != NULL) && (QThread::currentThread()== m_PtrWorkerThread)) {
while(m_MustWait) {
#ifdef WIN32
Sleep(1);
#else
usleep(10);//1,000,000 in sec
#endif
}
{
m_MustWait = true;
m_PtrMsg = msg;
PostMessageEvent *ptr_msgEvent = new(PostMessageEvent);
ptr_msgEvent->m_ptrPostingThread=QThread::currentThread();
ptr_msgEvent->setMsg(msg);
QCoreApplication::postEvent ( this, ptr_msgEvent);
}
} else
{
// m_MustWait = true;
PostMessageEvent *ptr_msgEvent = new(PostMessageEvent);
ptr_msgEvent->m_ptrPostingThread=QThread::currentThread();
ptr_msgEvent->setMsg(msg);
QCoreApplication::postEvent ( this, ptr_msgEvent);
}
};
To copy to clipboard, switch view to plain text mode
can be written as this (I don't know why you sleep for a couple of seconds here so I ignored that part):
void writeWin(const QString& msg) {
PostMessageEvent *ptr_msgEvent = new(PostMessageEvent);
ptr_msgEvent->setMsg(msg);
ptr_msgEvent
->m_ptrPostingThread
=QThread::currentThread();
// optionally, if you really need it};
void writeWin(const QString& msg) {
PostMessageEvent *ptr_msgEvent = new(PostMessageEvent);
ptr_msgEvent->setMsg(msg);
ptr_msgEvent->m_ptrPostingThread=QThread::currentThread(); // optionally, if you really need it
QCoreApplication::postEvent ( this, ptr_msgEvent);
};
To copy to clipboard, switch view to plain text mode
or better yet just use signals and slots (properly). If you have to "wait", then use a wait condition instead of a busy loop.
Bookmarks