PDA

View Full Version : QStringList segmentation fault



oliver_mpt
5th February 2018, 15:10
Hi !

I have a strange bug that I can't track down :
I have a qt server which receive, using a TCPclient class, a text file sent line by line by a remote client device. Every line of text is stored, upon reception in a QStringList variable, which is a member variable of the MainWindow main class of the application. This variable is declared as public and is written directly from the TcpClient class, which holds a pointer m_mainWnd to the MainWindow class. The code snippet from TcpClient class which receive the line is given below :

case TCP_HARDWARE_CONFIG_LINE:
{
char* p = new char[m_blockSize];
in.readRawData(p, m_blockSize - 4);
QString sconf = QString::fromUtf8(p);
if (!sconf.isEmpty()) m_mainWnd->m_listConfig.append(sconf);
delete p;
}
break;


When executing the m_listConfig.append, a segmentation fault (SIGSEV) is raised. The string sconf is perfectly valid, but the debugger states "no such value" when I set an expression evaluator to my m_mainWnd pointer.
The MainWindow class header is included in the TcpClient.h file, the compiler doesn't complain nor show warnings.

Any idea why my MainWindow pointer is invalid ?

Ginsengelf
5th February 2018, 16:22
Hi, is m_mainWnd valid and not NULL?

Another thing: if you create a buffer with new[] you need to use "delete []" to free the memory, otherwise you get undefined behaviour and/or a memory leak.

Ginsengelf

oliver_mpt
5th February 2018, 16:51
HI Ginsengelf

Thx for the reply!
The pointer is not null but contains a value which seems to be nonsense.
I have solved the problem by using signal/slot mechanism to send the data from class TcpClient to class MainWindow, but still doesn't understand the origin of the problem with public pointer.

Thanks for the delete [] info, I missed that one !

Oliver

d_stranz
5th February 2018, 21:04
The pointer is not null but contains a value which seems to be nonsense. ...still doesn't understand the origin of the problem with public pointer.

Likely you never set the value of this pointer in your TcpClient class, so you are trying to make a call through an uninitialized (and therefore invalid) pointer.


I have solved the problem by using signal/slot mechanism

This is almost always the better way to go. Different classes should never need to know the details how something is implemented (and especially the names of the member variables used to implement them).