PDA

View Full Version : error message appears when function is called



babygal
11th June 2010, 07:58
An error message prompt appears when function, processInputData(param1, param2, ....) is called.
The application exits when 'Don't Send' button on the message prompt is clicked on.
How do I resolve this issue?

Here is the code:

New::New(QWidget *parent) :
QDialog(parent)
{
okButton = createButton(tr("&OK"), SLOT(messageOk()));

createCMRFilesMessageGroupBox();
createEDMessageGroupBox();
createESMessageGroupBox();

createmainLayout();

setWindowTitle(tr("New Case"));
resize(450, 100);
}
void New::messageOk()
{

QMessageBox::about(this,tr("OK clicked"),tr("OK"));
processInputData(endocardialfileName.toAscii(), //convert 'QString' to 'const char*'
epicardialfileName.toAscii(), //convert 'QString' to 'const char*'
1,
1.1,
1.1,
1,
1.1,
1.1);


}

Definition of processInputData( ):


processInputData(const char*, const char* ,
const int,
const float, const float,
const int,
const float, const float );

ChrisW67
11th June 2010, 08:06
QString::toAscii() does not return a const char pointer.

babygal
11th June 2010, 09:32
How do I resolve the issue?

babygal
14th June 2010, 07:05
The error mentioned is as seen in the attachment. How do I resolve it quickly?
I don't get any error when i compile. I am only getting this error during run time...:-(

agathiyaa
14th June 2010, 08:17
I cannot read your doc file. I suspect you may do the following which i guess from the previous reply..

pInputdata=QString::toAscii() .data();

I assume char* pInputData is a Function Argument. If this isn't right please show me what is in your error.doc file in plain text. If my assumption is right let me know. the solution is simple.

babygal
14th June 2010, 08:40
The error is :
Application.exe has encountered a problem and needs to close. We are sorry for the inconvenience.If you were in the middle of something, the information you were working on might be lost.

Please tell Microsoft about this problem.We have created an error report that you can send to us. We will treat this report as confidential and anonymous. To see what data this error report contains, click here.

ChrisW67
14th June 2010, 08:50
You will get this generic catch-all error from the Windows system if you access a pointer that is invalid or you access more memory than you should through an otherwise valid pointer (and any number of other things). If something is expecting a pointer to a NUL terminated string (as your code is) and you pass something that is not a pointer to a NUL terminated string (as your code does) then this is often the result.

You are passing a QByteArray where the code is expecting a const char*. You need to revisit lines 19 and 20 of your original post after reading the docs for QString::toAscii() and QByteArray::constData()

babygal
14th June 2010, 10:17
Can you please show me clearly? I don't quite understand.

babygal
14th June 2010, 10:18
I cannot read your doc file. I suspect you may do the following which i guess from the previous reply..

pInputdata=QString::toAscii() .data();

I assume char* pInputData is a Function Argument. If this isn't right please show me what is in your error.doc file in plain text. If my assumption is right let me know. the solution is simple.

Can you please show me clearly? I don't quite understand how to implement this way.

babygal
14th June 2010, 10:43
I solved the bug with the code below:

1.
processInputData(endocardialfileName.toAscii().con stData(), epicardialfileName.toAscii().constData(), ....

Other 3 possible solutions I found :

2.
processInputData(endocardialfileName.toAscii().dat a(), epicardialfileName.toAscii().data(), ....


3.
processInputData(endocardialfileName.toLatin1().co nstData(), epicardialfileName.toLatin1().constData(), ....


4.
processInputData(endocardialfileName.toLatin1().da ta(), epicardialfileName.toLatin1().data(), ....

agathiyaa
14th June 2010, 11:17
Good to know that you have solved the issue.

But my recommendation is to use the following


char end[MAX_PATH]={0};
char epi[MAX_PATH]={0};

strcpy(end,endocardialfileName.toAscii().data());
strcpy(epi,epicardialfileName.toAscii().data());

processInputData(end,epi,...);

Because toAscii().data() function uses internal stack which may not be valid all the time.

Any Comments ???.

ChrisW67
14th June 2010, 23:49
The four options babygal pointed out should be safe because the scope of the temporary QByteArray in the:
processInputData(endocardialfileName.toLatin1().co nstData(), ... call extends to the completion of function processInputData() (the enclosing C++ statement). If the value could not be used immediately then you would have to make a persistent copy of it as above (using strncpy or a safer option though), or by making the QByteArray explicit so that you can control the scope:

{
...
QByteArray asciiFileName = endocardialfileName.toAscii();
processInputData(asciiFileName.constData(), ...);
// more uses of the ASCII string
char *s = asciiFileName.data();
somefunc(s);
...
}