PDA

View Full Version : QList<char*> dies



thomaspu
14th March 2008, 03:41
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?

QList *mp_list = new QList<char*>();
//Adding an item
void foo(char *pData)
{
mp_list->append( pData); //Crash
.....
Paul

MarkoSan
14th March 2008, 04:11
Which error do you get excatly?

thomaspu
14th March 2008, 04:28
"Unhandled Exception" .... otherwise known as a crash ;p

jpn
14th March 2008, 06:12
How about using QByteArray?

PS. Looks like you declare and use a member variable but assign to a local variable.

thomaspu
14th March 2008, 15:42
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

wysota
14th March 2008, 16:27
Could you show us how you are calling the foo() function (I mean the context, not a single line)?

jpn
14th March 2008, 17:00
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 (http://doc.trolltech.com/latest/shared.html).

thomaspu
14th March 2008, 18:16
Heres how it gets called:

char *pBuf = new char[BUFFER_SIZE];
if ( ! pBuf)
return;

int len = 0,
space = BUFFER_SIZE -1; //Space that our buffer has (leaving room for NULL

char *pBufIter;
pBufIter = pBuf; //Pointer to use as an iterator

do{
len = libssh2_channel_read( (*mp_channelList)[channelNumber]->pChannel, pBufIter, space);
if (len > 0)
{
space = space - len; //Mark down how much space is left to write in
if (space > 0)
pBufIter += len; //Move the pBuf pointer to the beginning of blank space
}
} while (len > 0 && space > 0);

if (space == BUFFER_SIZE-1)
{
delete [] pBuf;
return;
}
else
{
*pBufIter = 0x0; //Put our NULL terminator in place
if ( mp_decoder)
mp_decoder->addToQueue( pBuf);
}
Now the mp_decoder is a class that lives in another thread. Heres its complete function def:

void
Decoder::addToQueue(char *pData)
{
m_mutex.lock();
mp_queue->push_back( pData);
printf("%d items in queue\n", mp_queue->size() );
m_mutex.unlock();
}
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

wysota
14th March 2008, 18:41
Could you provide a backtrace from the debugger?

thomaspu
15th March 2008, 05:10
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.

wysota
15th March 2008, 08:25
In VS this is called a "call stack" or something like that - I'm sure you'll find it.

thomaspu
15th March 2008, 22:47
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