5

Sometimes I get this "Debug Assertion Failed" error running my Qt project in debug mode (image). I don't know where I wrong because the compiler says nothing and I don't know what to do to find my error.

I program under Windows Vista, using Qt Creator 2.4.1, Qt 4.8.1.

My program has to read some informations from a laser device and save them into a file with a code similar to this:

void runFunction()
{
    configure_Scanning(...);

    while(...)
    {
        // do something
        scanFunction();
        // do something
    }
}

and this is my "incriminated" function (where I think the problem is)

void scanFunction()
{
    file.open();

    data = getDataFromDevice();

    if(flag)
    {
        if(QString::compare(lineB,"")!=0)
        {
            QTextStream out(&file);
            out << lineB << endl;
            lineB = "";
        }
        lineA.append(data+"\t");
    }
    else
    {
        if(QString::compare(lineA,"")!=0)
        {
            QTextStream out(&file);
            out << lineA << endl;
            lineA = "";
        }
        lineB.prepend(data+"\t");
    }

    file.close();
}

Where lineA and lineB are initially two void QString: the idea is that I make a bidirectional scanning to save informations in a 2D matrix (from -X to +X and viceversa, while Y goes to a specified target). lineA memorizes the (-)to(+) reading; lineB memorizes the (+)to(-) reading. When the scanning direction changes, I write lineA (or lineB) to the file and I proceed with the scanning.

Do you understand what I said? Could you suggest me a solution?

Thanks and sorry for my English :P

Marco Carletti
  • 318
  • 2
  • 5
  • 16
  • 2
    click on Retry in the dialog to debug the program, just as it says. Then check the call stack or add it to this question and you should get a better idea of what goes wrong. On a sidenote, constantly opening and closing a file is probably not needed and causes some overhead. – stijn May 30 '12 at 15:49
  • I know that opening and closing a file on each step is not the best strategy, but I have to do it to prevent any loss of data in case of unexpected interruption. – Marco Carletti May 31 '12 at 07:27
  • isn't that what `flush()` does? – stijn May 31 '12 at 07:39
  • If I insert the code `file.open()` and `file.close()` inside the second `if`, before `QTextStream`, I open my file only when I'm sure I have something to write in. Am I wrong? Is this a possible strategy to prevent the heap corruption? – Marco Carletti May 31 '12 at 07:45
  • Mmmh... Yes, it could be (I had not thought to `flush()`). But I think that my problem remains... – Marco Carletti May 31 '12 at 07:49
  • Does this answer your question? [Why do I get \_CrtIsValidHeapPointer(block) and/or is\_block\_type\_valid(header->\_block\_use) assertions?](https://stackoverflow.com/questions/64418624/why-do-i-get-crtisvalidheappointerblock-and-or-is-block-type-validheader-b) – ead Oct 28 '20 at 21:37

2 Answers2

11

_CrtIsValidHeapPointerUserData means, that you have a heap corruption, which is noticed by debug heap checker. Suspect everybody who can write any information into any deleted dynamic object. And yes, you'll receive heap corruction not immideately on rewrite occurs, but on the next heap check, which will be performed on any next memory allocation/deallocation. However should be simply tracked by a call stack in single threaded applications.

Forgottn
  • 563
  • 3
  • 11
  • I didn't say that "my" Qt Creator doesn't permit me to run the application with the debugger. So I can't check any error at runtime. Could you explain me how to check the heap? – Marco Carletti May 31 '12 at 07:30
  • Cannot really say. Brute force way is to replace ALL the pointers with smart pointer classes and set them to fire error messages when the problem is detected. – Forgottn May 31 '12 at 17:21
  • Good idea! But... I don't know when the problem is detected. Anyway, I'll try this way. Thanks for the idea! – Marco Carletti Jun 01 '12 at 08:56
  • Finally I found how to change my debugger (now I use GDB); now it returns this message: `HEAP[myApp.exe]: Heap block at 00330BA48 modified at 0330BA78 post requested size of 28`; anyway I try to proceed and the second message is `HEAP[myApp.exe]: Invalid address specified to RtlValidateHeap (026F0000, 0330BA50)`. Could you help me to decifrate these messages? – Marco Carletti Jun 01 '12 at 13:03
  • 1
    Yes, classic array overflow. You have an array (or a pointer) at address 0x0330BA48 with 0x28 bytes size. Some writer however managed to write something after the 0x28 bytes. The bytes at 0x0330BA78 are spoiled. On my point of view that looks like you're writing '\0' just after the last charachter into the wide (or multibyte) string. Check these facts at first. Also check all dynamic arrays, especially handwritten ones. The second message says that the problem way found when the heap block from 0x026F0000 to 0x0330BA50 was checked. – Forgottn Jun 01 '12 at 19:07
  • I fixed some errors in my code. There was a thread that I initialized but I did not start it, so (probably) some pointers were uncontrolled. I tried to run a lot of times my program and it never crashed. Thank you for the help! – Marco Carletti Jun 04 '12 at 15:27
1

In our case, the program worked perfectly in DEBUG mode and crashed with the similar error trace in RELEASE mode.

In my case, the RELEASE mode was having msvscrtd.dll in the linker definition. We removed it and the issue resolved.

Alternatively, adding /NODEFAULTLIB to the linker command line arguments also resolved the issue.

Pavan Dittakavi
  • 3,013
  • 5
  • 27
  • 47