Results 1 to 5 of 5

Thread: Application crashes when I delete the character pointer array

  1. #1
    Join Date
    Jul 2012
    Location
    India
    Posts
    33
    Thanks
    10
    Qt products
    Qt4
    Platforms
    MacOS X Unix/X11 Windows

    Default Application crashes when I delete the character pointer array

    Qt Code:
    1. try
    2. {
    3. char *testarr = new char[15];
    4. QString strVal1="Test";
    5. QByteArray br = strVal1.toUtf8();
    6. testarr = br.data();
    7.  
    8.  
    9. delete[] testarr; //Why my application crashes here.
    10. }
    11. catch(...)
    12. {
    13. QMessageBox::information(0, "", "Error");
    14. }
    To copy to clipboard, switch view to plain text mode 

    I have two questions

    1) Why my above program crashes when I call delete[] testarr; ?
    2) Why the catch block doesnot hadle that exception ?

    I am using Qt creator 4.8 in ubuntu OS

  2. #2
    Join Date
    Mar 2008
    Location
    Kraków, Poland
    Posts
    1,536
    Thanked 284 Times in 279 Posts
    Qt products
    Qt4
    Platforms
    Unix/X11 Windows

    Default Re: Application crashes when I delete the character pointer array

    Because You are deleting internal data of QByteArray.
    I think line 6 should be :
    Qt Code:
    1. strcpy(testarr,br.data());
    To copy to clipboard, switch view to plain text mode 

  3. The following user says thank you to Lesiok for this useful post:

    arunkumaraymuo1 (12th February 2013)

  4. #3
    Join Date
    Jul 2012
    Location
    India
    Posts
    33
    Thanks
    10
    Qt products
    Qt4
    Platforms
    MacOS X Unix/X11 Windows

    Default Re: Application crashes when I delete the character pointer array

    Thank you very much,that solved the issue. I have one another doubt too, How I can protect from application crash with try catch when any exception occure.

    Quote Originally Posted by Lesiok View Post
    Because You are deleting internal data of QByteArray.
    I think line 6 should be :
    Qt Code:
    1. strcpy(testarr,br.data());
    To copy to clipboard, switch view to plain text mode 

  5. #4
    Join Date
    Nov 2010
    Posts
    315
    Thanked 53 Times in 51 Posts
    Qt products
    Qt4
    Platforms
    Unix/X11 Windows Symbian S60 Maemo/MeeGo

    Default Re: Application crashes when I delete the character pointer array

    Your code has lots of problems:
    Qt Code:
    1. char *testarr = new char[15];
    2. QString strVal1="Test";
    3. QByteArray br = strVal1.toUtf8(); // why? you could use simply QByteArray br("Test"); avoiding some unnecessary conversions.
    4. testarr = br.data(); // here you have memory leak, you are loosing touch with block you allocated in first line, by overriding it with value form QByteArray
    5.  
    6. delete[] testarr; // now you are deleting something what is owned by `br`
    To copy to clipboard, switch view to plain text mode 

  6. The following user says thank you to MarekR22 for this useful post:

    arunkumaraymuo1 (14th February 2013)

  7. #5
    Join Date
    Jan 2008
    Location
    Alameda, CA, USA
    Posts
    5,230
    Thanks
    302
    Thanked 864 Times in 851 Posts
    Qt products
    Qt5
    Platforms
    Windows

    Default Re: Application crashes when I delete the character pointer array

    How I can protect from application crash with try catch when any exception occur.
    The error in your code did not result in a C++ exception; it was most likely a segmentation fault caused when the QByteArray instance on the stack went out of scope and tried to delete the internal memory you had already deleted in line 6. Try/catch blocks do not trap segmentation faults or other C++ signals (not to be confused with Qt "signals"). In order to trap a C++ segmentation fault signal, you need to install a signal handler for SIGSEGV.

    However, once your program's memory get corrupted by something like a segmentation fault, it is not a good idea to simply trap it and go on running as if nothing has happened. It is far better to 1) find and fix these errors in the debugger instead of pretending they don't exist, and 2) if you do trap them with a signal handler, then the best thing to do is to tell the user that it happened and exit quickly. It really isn't safe to do anything else, because you don't know what damage the SIGSEGV has caused.

  8. The following user says thank you to d_stranz for this useful post:

    arunkumaraymuo1 (14th February 2013)

Similar Threads

  1. Replies: 5
    Last Post: 13th March 2021, 09:07
  2. Delete character from file
    By kaszewczyk in forum Newbie
    Replies: 2
    Last Post: 11th March 2010, 18:07
  3. qstringlist array crashes program
    By chrisb123 in forum Newbie
    Replies: 4
    Last Post: 23rd October 2009, 15:03
  4. QVector crashes when array size is big
    By Sheng in forum Qt Programming
    Replies: 49
    Last Post: 27th February 2009, 22:13
  5. How to create QPixmap from unsigned character array?
    By rashidbutt in forum Qt Programming
    Replies: 4
    Last Post: 16th May 2006, 18:25

Tags for this Thread

Bookmarks

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  
Digia, Qt and their respective logos are trademarks of Digia Plc in Finland and/or other countries worldwide.