Important: Please read the Qt Code of Conduct - https://forum.qt.io/topic/113070/qt-code-of-conduct

[Solved]How to prevent this crash?



  • In my code, at a certain line I have this condition:

    @if(imageData->myImage==NULL)
    {
    ...
    }
    @

    However, my code crashes at the "if" line for a certain reason. It gives "read access violation" error. When I debug the code, I see in the locals and expressions that the value of the "imageData->myImage" is Memory access error. Could you please how I can put an if condition where I check if the memory is reachable for this variable or not?
    What I want to do is:

    @if(imageData->myImage== "Condition for memory access error")
    {
    ...
    }
    @

    Thanks.


  • Lifetime Qt Champion

    Hi and welcome to DevNet.

    Did you check that imageData is a valid pointer ?


  • Moderators

    Your myImage is probably declared as private, hence it's not accesible. Either make it public, or use a getter to get the pointer. Then the pointer == NULL will work (make sure you explicitly set it to null in the constructor or somewhere. Pointers, just like variables in c++, are not automatically initialised to 0).



  • [quote author="SGaist" date="1363868284"]Hi and welcome to DevNet.

    Did you check that imageData is a valid pointer ?[/quote]

    How can I check it?

    [quote author="sierdzio" date="1363868421"]Your myImage is probably declared as private, hence it's not accesible. Either make it public, or use a getter to get the pointer. Then the pointer == NULL will work (make sure you explicitly set it to null in the constructor or somewhere. Pointers, just like variables in c++, are not automatically initialised to 0).[/quote]

    It's not declared as private. The code runs perfect for some time & passes million times over this line but it crashes there fore some reason.


  • Moderators

    Ah, ok. Then check for validity of imageData before using it:
    @
    if (imageData && imageData->myImage==NULL) {
    ...
    }
    @


  • Lifetime Qt Champion

    As sierdzio said: check for imageData == NULL before doing imageData->myImage == NULL



  • Of course. Why could I have thought about this before. Thanks guys. How do you mark the thread as solved?


  • Moderators

    Edit your initial post and add "[Solved]". I've already tagged it for you.


  • Lifetime Qt Champion

    You're welcome

    Simply edit the thread title and add [Solved]



  • Thanks guys. You two are parallel processing here :D


  • Moderators

    Hehe, yes it would seem so :)


Log in to reply