Results 1 to 4 of 4

Thread: QStringList segmentation fault

  1. #1
    Join Date
    Nov 2012
    Posts
    11
    Qt products
    Qt4 Qt5
    Platforms
    Unix/X11 Windows

    Default QStringList segmentation fault

    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 :
    Qt Code:
    1. case TCP_HARDWARE_CONFIG_LINE:
    2. {
    3. char* p = new char[m_blockSize];
    4. in.readRawData(p, m_blockSize - 4);
    5. QString sconf = QString::fromUtf8(p);
    6. if (!sconf.isEmpty()) m_mainWnd->m_listConfig.append(sconf);
    7. delete p;
    8. }
    9. break;
    To copy to clipboard, switch view to plain text mode 

    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 ?

  2. #2
    Join Date
    Jul 2008
    Location
    Germany
    Posts
    506
    Thanks
    11
    Thanked 76 Times in 74 Posts
    Qt products
    Qt4 Qt5
    Platforms
    Unix/X11 Windows

    Default Re: QStringList segmentation fault

    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

  3. #3
    Join Date
    Nov 2012
    Posts
    11
    Qt products
    Qt4 Qt5
    Platforms
    Unix/X11 Windows

    Default Re: QStringList segmentation fault

    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

  4. #4
    Join Date
    Jan 2008
    Location
    Alameda, CA, USA
    Posts
    5,230
    Thanks
    302
    Thanked 864 Times in 851 Posts
    Qt products
    Qt5
    Platforms
    Windows

    Default Re: QStringList segmentation fault

    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).
    <=== The Great Pumpkin says ===>
    Please use CODE tags when posting source code so it is more readable. Click "Go Advanced" and then the "#" icon to insert the tags. Paste your code between them.

Similar Threads

  1. a segmentation fault
    By yaohao@qtcentre in forum Qt Programming
    Replies: 4
    Last Post: 17th March 2011, 09:01
  2. segmentation fault!!
    By Yayati.Ekbote in forum Qt Programming
    Replies: 4
    Last Post: 24th March 2010, 15:10
  3. why qstringlist *ps "Segmentation fault"?
    By lanmanck in forum Qt Programming
    Replies: 6
    Last Post: 23rd September 2009, 07:46
  4. segmentation fault
    By uchennaanyanwu in forum Newbie
    Replies: 3
    Last Post: 31st July 2008, 16:52
  5. Segmentation Fault
    By merry in forum General Programming
    Replies: 4
    Last Post: 12th March 2007, 04:08

Bookmarks

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  
Digia, Qt and their respective logos are trademarks of Digia Plc in Finland and/or other countries worldwide.