Results 1 to 12 of 12

Thread: QList<char*> dies

  1. #1
    Join Date
    Nov 2006
    Posts
    86
    Thanks
    6
    Thanked 14 Times in 5 Posts
    Qt products
    Qt4
    Platforms
    MacOS X Unix/X11 Windows

    Default QList<char*> dies

    I'm trying to make a list of pointers to character arrays. But when I try and append a new item, the darn thing gives a runtime error. Some times I even see: QList: out of memory... What am I doing wrong here?
    Qt Code:
    1. QList *mp_list = new QList<char*>();
    2. //Adding an item
    3. void foo(char *pData)
    4. {
    5. mp_list->append( pData); //Crash
    6. .....
    To copy to clipboard, switch view to plain text mode 
    Paul

  2. #2
    Join Date
    Jan 2006
    Location
    Ljubljana
    Posts
    687
    Thanks
    111
    Thanked 4 Times in 4 Posts
    Qt products
    Qt5 Qt/Embedded
    Platforms
    MacOS X Unix/X11 Windows Android

    Default Re: QList<char*> dies

    Which error do you get excatly?
    Qt 5.3 Opensource & Creator 3.1.2

  3. #3
    Join Date
    Nov 2006
    Posts
    86
    Thanks
    6
    Thanked 14 Times in 5 Posts
    Qt products
    Qt4
    Platforms
    MacOS X Unix/X11 Windows

    Default Re: QList<char*> dies

    "Unhandled Exception" .... otherwise known as a crash ;p

  4. #4
    Join Date
    Feb 2006
    Location
    Oslo, Norway
    Posts
    6,264
    Thanks
    36
    Thanked 1,519 Times in 1,389 Posts
    Qt products
    Qt4
    Platforms
    MacOS X Unix/X11 Windows Symbian S60 Maemo/MeeGo

    Default Re: QList<char*> dies

    How about using QByteArray?

    PS. Looks like you declare and use a member variable but assign to a local variable.
    J-P Nurmi

  5. #5
    Join Date
    Nov 2006
    Posts
    86
    Thanks
    6
    Thanked 14 Times in 5 Posts
    Qt products
    Qt4
    Platforms
    MacOS X Unix/X11 Windows

    Default Re: QList<char*> dies

    I was just trying to quickly show how I was creating the QList. Its a member variable. And all I want to do is store a list of character pointers in a QList. I really don't want to use any other items like a QByteArray, QString, etc..

    Paul

  6. #6
    Join Date
    Jan 2006
    Location
    Warsaw, Poland
    Posts
    33,359
    Thanks
    3
    Thanked 5,015 Times in 4,792 Posts
    Qt products
    Qt3 Qt4 Qt5 Qt/Embedded
    Platforms
    Unix/X11 Windows Android Maemo/MeeGo
    Wiki edits
    10

    Default Re: QList<char*> dies

    Could you show us how you are calling the foo() function (I mean the context, not a single line)?

  7. #7
    Join Date
    Feb 2006
    Location
    Oslo, Norway
    Posts
    6,264
    Thanks
    36
    Thanked 1,519 Times in 1,389 Posts
    Qt products
    Qt4
    Platforms
    MacOS X Unix/X11 Windows Symbian S60 Maemo/MeeGo

    Default Re: QList<char*> dies

    Then at least consider allocating the QList object on the stack. It's pretty much pointless allocating it on the heap because QList is an implicitly shared class.
    J-P Nurmi

  8. #8
    Join Date
    Nov 2006
    Posts
    86
    Thanks
    6
    Thanked 14 Times in 5 Posts
    Qt products
    Qt4
    Platforms
    MacOS X Unix/X11 Windows

    Default Re: QList<char*> dies

    Heres how it gets called:
    Qt Code:
    1. char *pBuf = new char[BUFFER_SIZE];
    2. if ( ! pBuf)
    3. return;
    4.  
    5. int len = 0,
    6. space = BUFFER_SIZE -1; //Space that our buffer has (leaving room for NULL
    7.  
    8. char *pBufIter;
    9. pBufIter = pBuf; //Pointer to use as an iterator
    10.  
    11. do{
    12. len = libssh2_channel_read( (*mp_channelList)[channelNumber]->pChannel, pBufIter, space);
    13. if (len > 0)
    14. {
    15. space = space - len; //Mark down how much space is left to write in
    16. if (space > 0)
    17. pBufIter += len; //Move the pBuf pointer to the beginning of blank space
    18. }
    19. } while (len > 0 && space > 0);
    20.  
    21. if (space == BUFFER_SIZE-1)
    22. {
    23. delete [] pBuf;
    24. return;
    25. }
    26. else
    27. {
    28. *pBufIter = 0x0; //Put our NULL terminator in place
    29. if ( mp_decoder)
    30. mp_decoder->addToQueue( pBuf);
    31. }
    To copy to clipboard, switch view to plain text mode 
    Now the mp_decoder is a class that lives in another thread. Heres its complete function def:
    Qt Code:
    1. void
    2. Decoder::addToQueue(char *pData)
    3. {
    4. m_mutex.lock();
    5. mp_queue->push_back( pData);
    6. printf("%d items in queue\n", mp_queue->size() );
    7. m_mutex.unlock();
    8. }
    To copy to clipboard, switch view to plain text mode 
    Note that I'm just trying to store a pointer to the character string, not the character string. The idea here is that I'm reading this data from a socket. Then I'm adding it to a list (which resides in another thread) that will then process these character strings when it can.

    What blows me away is why can't just stuff that char pointer in the list. Theres actually been a few occasions where I can stick the pointer in, but the majority of the time the thing does dome sort of runtime error.

    Paul

  9. #9
    Join Date
    Jan 2006
    Location
    Warsaw, Poland
    Posts
    33,359
    Thanks
    3
    Thanked 5,015 Times in 4,792 Posts
    Qt products
    Qt3 Qt4 Qt5 Qt/Embedded
    Platforms
    Unix/X11 Windows Android Maemo/MeeGo
    Wiki edits
    10

    Default Re: QList<char*> dies

    Could you provide a backtrace from the debugger?

  10. #10
    Join Date
    Nov 2006
    Posts
    86
    Thanks
    6
    Thanked 14 Times in 5 Posts
    Qt products
    Qt4
    Platforms
    MacOS X Unix/X11 Windows

    Default Re: QList<char*> dies

    Um... I'm not really sure how to give a backtrace with visual studio.

    I can't help but wonder if the reason that its crapping out isn't caused by a pointer being deleted or something. That would make sense...but I'm not deleting it. This is just strange.

  11. #11
    Join Date
    Jan 2006
    Location
    Warsaw, Poland
    Posts
    33,359
    Thanks
    3
    Thanked 5,015 Times in 4,792 Posts
    Qt products
    Qt3 Qt4 Qt5 Qt/Embedded
    Platforms
    Unix/X11 Windows Android Maemo/MeeGo
    Wiki edits
    10

    Default Re: QList<char*> dies

    In VS this is called a "call stack" or something like that - I'm sure you'll find it.

  12. #12
    Join Date
    Nov 2006
    Posts
    86
    Thanks
    6
    Thanked 14 Times in 5 Posts
    Qt products
    Qt4
    Platforms
    MacOS X Unix/X11 Windows

    Default Re: QList<char*> dies

    Woot! Stupidity! My thread was stopping itself when it shouldn't have, which was deleting the QList object. No wonder it sometimes worked and sometimes didn't

    Paul

Similar Threads

  1. QList<A*> indexOf
    By Moppel in forum General Programming
    Replies: 6
    Last Post: 8th August 2017, 15:17
  2. Detect when a TCP Server dies
    By jimroos in forum Qt Programming
    Replies: 1
    Last Post: 4th July 2007, 00:40
  3. QList<myObject*> myObjectList count() error
    By morty in forum Qt Programming
    Replies: 4
    Last Post: 20th October 2006, 16: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.