Crash in function: QImageData * QImageData::create(const QSize &size, QImage::Format format)
-
Hello,everyone!
I'm developing a desktop software with Qt5.15.2. My software rarely will crash. I used .dmp file to track the problem. The Call Stack shows that the crash can happen everywhere while the final crash point is always the same: (the crash function which crashed is: QImageData * QImageData::create(const QSize &size, QImage::Format format) )
Is that a Qt Bug? How can I avoid this crash? Even if I had added try catch block to my code where crashed, it still doesn't work.
Here I post one of the several crashed code and the screenshot of stack:
void CFuncToolSlamWidget::on_btn_add_clicked() { //open file QString qspath = CViewManager::getInstance()->getFileName(tr("/"), "Image Files(*.jpg;*jpeg;*.png;*.bmp;*.gif)", { "*.bmp","*.jpg","*jpeg","*.png","*.gif" }).trimmed(); if (qspath.isEmpty()) { return; } QImage qImaggbk; QImageReader reader(qspath); reader.setAutoTransform(true); qImaggbk = reader.read(); //-------------the crashdump.dmp shows it crashed here------------------- if (qImaggbk.width() > 1080 || qImaggbk.height() > 720) { QImage scaleImage = qImaggbk.scaled(1080, 720, Qt::KeepAspectRatio, Qt::SmoothTransformation); int iWidth = scaleImage.width(); int iheight = scaleImage.height(); bool isSuccess = scaleImage.save(qspath); } emit signals_select_showType(5, 0, qspath); setSignButtonStatus(false); }
-
Look where in your code the crash comes from and post the code (not screenshot). Also make sure the QImage data is valid during the whole lifetime of the QImage object (which I doubt it is) - how do you create your QImage object?
-
@Wenchi
It is probably your code bug rather than Qt code. I believe the screenshot shows that thed
ind->nbytes
is currentlynullptr
.try...catch
won't trap it.You need a stack trace showing where this call to
QImageData::create()
originates from in your code. I do not know whethercrashdump.dmp
can show you that. If you compile for debug and run under whatever debugger for your compiler under Windows you should be able to see that stack trace when it happens? -
@Christian-Ehrlicher @JonB
Thank you for your reply! I have updated the description of my question. Could you see whether the information is enough. Since I haven't read the book, <Advanced Windows Debugging>, I'm afraid I couldn't provide more debug infomation. -
@Wenchi
So the trace shows you clicked a button and it called yourCFuncToolSlamWidget::on_btn_add_clicked()
. At line #263 that callsQImageReader::read()
. So let's see the code around (i.e. leading up to) that line #263. We are probably particularly interested in the lifetime of theQImage
you are using. -
@JonB
I have already uploaded the code of function
void CFuncToolSlamWidget::on_btn_add_clicked()
to the description of the question.
And the crash line is
qImaggbk = reader.read();
Since the crash doesn't appear necessarily, I couldn't figure out why it crashed. -
@Wenchi
Then it looks likeQImageReader::read()
does not like the content of the.png
file you are trying to load. Does it happen depending on which file is chosen? Try the code with a specified filename in some standalone code outside of your project and see how it goes? -
Try to use another tool to load your image and see if the file is corrupt or has some other issues. It may be a good idea to check if read() fails or not all the time like the following:
QImage icon(64, 64, QImage::Format_RGB32); QImageReader reader("icon_64x64.bmp"); if (reader.read(&icon)) { // Display icon }
the sample code is from here:
https://doc.qt.io/qt-5/qimagereader.html#read-1 -
@JoeCFD
Thank you for your reply!
Using another tool to load image can be a way to avoid the crash, but in my situstion, the crash could appear not only inQImageReader.read()
but also in several other code, such asQlist<MyWidget>.push_back()
orQStackWidget.setCurrentIndex()
and so on. -
@Wenchi said in Crash in function: QImageData * QImageData::create(const QSize &size, QImage::Format format):
the crash could appear not only in QImageReader.read() but also in several other code, such as Qlist<MyWidget>.push_back() or QStackWidget.setCurrentIndex() and so on.
Then you should fix your code.
-
@Wenchi said in Crash in function: QImageData * QImageData::create(const QSize &size, QImage::Format format):
Although the code is crashing in various places, but the crash point is always the same:
But that is likely --- or just as possible --- indicating you have a problem elsewhere which only shows up here, such as corrupted/misallocated memory? How else would you explain it "crashing" in e.g.
QList<MyWidget>.push_back()
?by the way, I'm fired. ready to get a new start
I am sorry to hear that, good luck for future.