Invalid parameter passed to C runtime function. When i call QserialPort::read function
-
I have a problem with a application. when i call function Qserialport::read the application crashs with the output
ASSERT failure in QList<T>::operator[]: "index out of range", file C:\work\build\qt5_workdir\w\s\qtbase\include/QtCore/../../src/corelib/tools/qlist.h, line 480
Invalid parameter passed to C runtime function.
Invalid parameter passed to C runtime function.
“Invalid parameter passed to C runtime function.” and with an error box from Microsoft Visual C++ Runtime Library.But in my code , i do not use any QList<T>, when i comment "bufferRead = comPort->read(num);" the app does not crash.But i want to read data from comPort , i debugged my code carefully, so i think the read function in Qserialport library has problem.
please help me to resolve that or if any one have another solution
please help me .AsapHere is my code:
@void serialRead::SLOT_readByteFromBuffer()
{
try
{
BEGIN_PORT_MUTEX_LOCK
if(comPort == NULL && !comPort->isOpen())return;
qint64 num = comPort->bytesAvailable();
if(num > 0){
if(num > 2048)num = 2048;
bufferRead = comPort->read(num);
for (int pos = 0; pos < bufferRead.size(); pos++)
{
unsigned char data = (unsigned char)bufferRead[pos];
switch (state)
{
case 0:
if (data == ( unsigned char)170) state++;
break;
case 1:
if (data == ( unsigned char)202) state++; else state = 0;
break;
case 2:
header = data; cks(header); state++; counter = 0; length = (unsigned char)((((header/4) & 31)*2));
if(length == 0 ){state = 0;}
break;
case 3:
id = data; cks(id); state++; counter = 0; addr = (((header & 3) * 256) | id);
break;
case 4:
buffer[counter] = data;
cks(data); counter++;
if (counter >= length) state++;
break;
case 5:
r_cka = data; state++;
break;
case 6:
r_ckb = data;
if ((cka == r_cka) && (ckb == r_ckb))//203 226 206 240 205 229
{
int *tmpint = new int[length/2];
int j =0;
for (j = 0; j < length; j=j+2)
{
tmpint[ (j/2) ] = ( unsigned char)buffer[j] *256 + ( unsigned char) buffer[j + 1];
}
qDebug() << "addr ->" << addr;
emit SIGNAL_dataByteUpdate(tmpint,length/2,addr);
cka = 0; ckb = 0; counter = 0; state = 0;buffer.clear();
}
else {
cka = 0; ckb = 0; counter = 0; state = 0;
buffer.clear();
}
break;
}
}
bufferRead.clear();
}
END_PORT_MUTEX_LOCK
}
catch(std::bad_alloc &e)
{
Q_UNUSED(e)
}
}@ -
Hi and welcome to devnet,
@if(comPort == NULL && !comPort->isOpen())return;@
Looks suspicious, if comPort is null, you don't want to test if isOpen returns true otherwise.
Also, what does a run through the debugger tells you ?
-
Hi SGaist :
i fixed like that
@ if(comPort == NULL || !comPort->isOpen())return;@this is crash output:
@ASSERT failure in QList<T>::operator[]: "index out of range", file C:\work\build\qt5_workdir\w\s\qtbase\include/QtCore/../../src/corelib/tools/qlist.h, line 480
Invalid parameter passed to C runtime function.
Invalid parameter passed to C runtime function.
@ -
Did you run your application through the debugger ? Where exactly does it crash ?
-
Hi SGaist :
the error appear random , i can't debug step by step to detect error.
but error does not appear when i comment the function comport->read -
Maybe first use the bytesAvailable() function to check if you are able to read data.
How did you declare you bufferRead ? -
[quote author="Jeroentje@home" date="1402907377"]Maybe first use the bytesAvailable() function to check if you are able to read data.
How did you declare you bufferRead ?
[/quote]Hi Jeroentje:
bufferRead is global var, and i declared in header file -
Hi,
Maybe declare bufferRead as a function scope variable. You only use it in this function only, so need for a global scope. You do a clear at the end, so no need to keep the "old" buffer. -
The index out of range on the QByteArray is sometimes very hard to pin point where it is coming from until you start putting in log statements the output to the debugger.
Try including <QDebug> and start putting log statements before and after your loops. If the ASSERT occurs, the debugger is going to jump out of the function / slot. If you don't have super time critical operation requirements in your application, QDebug can be a great tool.
For example:
@
QByteArray data = sPort->read(bytes);
for (int i=0;i<data.size();i++){
qDebug() << "ASSERT TEST 1";// do parsing .... qDebug() << "ASSERT TEST 2";
}
@If ASSERT TEST 1 is printed out to the console only once, you know that there is a bug in your parsing routine that is going outside of the bounds of the array.
Since we can't see all of the code, that probably your best bet. You might find it may not even be in your parsing routine, but somewhere else.
-
Hi Dvez43, thanks for your helps.
I have fixed it and operator correctly.I used Qmutex fucntion to fix it.