PDA

View Full Version : QTextEdit::keyPressEvent leads to 0xC0000005 Access Violation Error



android_
12th March 2010, 12:05
I'm really stuck on this one, so i'll explain the situation and hopefully one of the professional programmers here will easily detect the error.

I recently updates from Qt4.4 to Qt4.6. The code was not changed, worked before but now leads to an error in debug.

I defined a custom TextEdit class with parent QTextEdit. This class has an overloaded keyPressEvent-function, which checks for a few keys and otherwise calls the parent keyPressEvent with: QTextEdit::keyPressEvent(event);
I build the program, run it, and if i typed in something everything worked fine.
However after the update there is an Access Violation Error apearing while executing this line.
The error appears in the file free.c, line 109: retval = HeapFree(_crtheap, 0, pBlock);
It only appears when the Backspace-Key or the Delete-Key are pressed, maybe on other keys which i haven't found yet. And it fails not everytime, but around 90%. If i type in letters or use the tab it works fine.

If i remove the line in which the parent function is called there is no error (and no QTextEdit::keyPressEvent(event) called). If i remove the overloaded function completely so only the QTextEdit::keyPressEvent(event) is called the error still appears.

My thought was that the keyPressEvent may be called multiple times and that is conflicting with the memory, but that is not the case. the keyPressEvent is only called once, in this line. The problem appears independant on the computer it's executed on. Maybe someone knows what i could have done wrong, or if it's just a Qt bug with the version update. I looked through the Changelist for QTextEdit but didn't find anything useful which has changed the way a keyPressEvent is used.

high_flyer
12th March 2010, 14:01
Maybe someone knows what i could have done wrong, or if it's just a Qt bug with the version update.
If it is a Qt bug, it will apear if you use a regular QTextEdit.
If it only apears with your custom class, then its a bug in your code.

retval = HeapFree(_crtheap, 0, pBlock);
You have a heap corruption.
The nasty thing about heap corruption is that it very often crashes NOT where the corruption really occurs , but usually when some memory is baying freed (and thus checked, which triggers the assertion).
Look at all the heap allocated variables in your class, and make sure you are not reading or writing beyond their allocated address.
Dangling references (that is, reference on a variable out of scope) can also have similar behavior.
This also fits well to the fact that it happens not always, and different between systems.

OR/AND - if you are using threads, then it could be a any number of thread related problems.

wysota
12th March 2010, 14:43
Showing us some code would be helpful too.

android_
14th March 2010, 15:07
.............

wysota
14th March 2010, 15:17
0xC... address is on the stack, not on the heap. Most likely your application tries to delete a stack-based object. Please show us the complete backtrace after the crash.

android_
14th March 2010, 15:21
I traced it down to two functions which were not members of the custom QTextEdit, but connected to specific signals. Thanks for your help.

If i cannot get it to run i'll work out how to do a complete backtrace and post it.

android_
17th March 2010, 10:53
Apparently the modification of the QTextCursor was the source of the error.
I don't know why it was not working (probably unvalid position because of the backspace?) but i changed a few things and now it works fine.
Strange nevertheless.