View Full Version : Getting a SIGSEGV error when loading a pixmap

17th May 2017, 19:52
I'm using Qt 5.8.0 MinGW 32 bit from Qt Creator 4.2.1 on an HP desktop with a Intel Xeon CPU in Windows & Professional 64 bit service pack1 OS.
I keep getting a segmentation fault when running this code (in debug) at the point where I attempt to load an image using QPixmap.

I believe my pointers are initialized correctly nor can I notice any uninitialized pointers or out of bound arrays (unless I'm looking right at it and can't see it). In the following code, some of the documented out lines are various ways I've attempted to load the image.

Header file...

#ifndef OVERVIEW_H
#define OVERVIEW_H

#include <QDialog>
#include <QWidget>
#include <QGeoRectangle>
#include <QGraphicsScene>
#include <QGraphicsObject>

#include "aircraft.h"

namespace Ui {
class OverView;

class OverView : public QDialog

explicit OverView(QWidget* pParent = 0);

private slots:
void showRange(int range); // Setup background
void on_closeBtn_clicked();

Ui::OverView* ui;

void setUpWindow();

QString sMessage;
QString sRangeFileNm; // Holds selected range file name

// QPixmap static rangePic; // The selected range image
QGraphicsScene* pRangeScene; // Graphic scene pointer

//__________________________________________________ __________
// Range background images from the resource file data.qrc
QString sBothFileNm = ":/Data/Images/BothRanges2000.gif";
QString sNorthFileNm = ":/Data/Images/NorthRange2000_2000.gif";
QString sSouthFileNm = ":/Data/Images/SouthRange2000_2000.gif";

#endif // OVERVIEW_H

cpp file...

#include "overview.h"
#include "ui_overview.h"
#include "ui_rddswindow.h"

OverView::OverView(QWidget* pParent) :
ui(new Ui::OverView)

showRange(0); // Set the default background

/*---- setUpWindow -------------------------------------------------------------------
Sets up the elements of the range view window. The combination box
allows user to select the range to use as background and connects the
combination box signal to the showRange slot.
void OverView::setUpWindow()
setWindowTitle("Range View");

QStringList selectedRange;
selectedRange << "Both Ranges" << "North Range"<< "Echo Range";

QObject::connect(ui->rangeCmbBox, SIGNAL(currentIndexChanged(int)),
this, SLOT(showRange(int)));


/*---- showRange --------------------------------------------------------------------------
Sets the background image and scaling based on an index value provided
by the rangeCmbBox in the range view.
0 - Both north and south ranges
1 - North range
2 - South range
void OverView::showRange(int range)
QBrush rangeBackground;

case 0: // Default image is both ranges
case 1:
sRangeFileNm.append(sNorthFileNm); // North range image
sRangeFileNm.append(sSouthFileNm); // South range image
// QPixmap const rangePic(sRangeFileNm);
QPixmap const rangePic = QPixmap(sRangeFileNm);

QRectF rangeRect = QRectF(QPointF(0,0), rangePic.size());
if(rangePic.load(sRangeFileNm)){ // Load the range picture
QRectF rangeRect = QRectF(QPointF(0,0), rangePic.size());
sMessage.append("Successfully loaded range image");
sMessage.append("Failed to load range image");
qDebug() << sMessage;

I'd appreciate anyone who can spot why I'm getting a SIGSEV error on this and thank you in advance.

18th May 2017, 07:05
Instead of using the QPixmap constructor with the filename, try using QPixmap::load() and checking the return status to see whether you have actually loaded the pixmap successfully? It also isn't clear why you are assigning the pixmap to another pixmap, only to have both of them go out of scope when the method exits.

18th May 2017, 11:01
you can also run the application in a debugger and see on which line it segfaults.

18th May 2017, 15:57
Yeah, assigning a pixmap to another pixmap is the result of having tried a bunch of different things and that just kinda hung on for a bit. I believe I have tried your suggestion (note the commented out sections). Seeing as how I had read that a static pixmap might help, I have also tried making the pixmap static within the function that I'm trying to load it in using the following lines of code...

QPixmap static rangePic;
qDebug() << "Failed to load the image";

In any event, I am running it in a debugger, and it blows up at whatever line that has the load statement in it. The code never has the opportunity to reach the qDebug() statement. The "locals and expressions" window in the debugger, shows that rangePic (the name of the pixmap), has a size of 2000 x 2000 which is the size of the image I'm trying to load. So I am assuming that the image is at least getting loaded, and that the problem occurs at some point between the load and the qDebug() statement. That would indicate that the error doesn't occur at one of my lines of code, so you can see why I'm tempted to say that it's not a problem with what I've written. However, my experience with SIGSEGV errors is that the problem could be in the code at an earlier point than where the error is indicated. I'm still unsure what is causing it though.

19th May 2017, 01:27
As an addendum to my previous post, I previously had discovered in the Qt documentation, that all pixmaps are placed in a QpixmapCache and the default size for the QpixmapCache is 10240 KB. I wanted to load 2 images with a combined file size of 1846 KB which falls well within the default QPixmapCache size limit. Therefore, one would think that was not the problem. However I have made a smaller version (lower resolution) of the largest file (the one used in the above code) and have found that I no longer receive the SIGSEGV error, and it works as expected. It's not clear to me at this point if the QPixmapCache size limit is the issue or not. Qt provides a way to increase the limit, yet I have yet to find a discussion on the cost of doing so. Unfortunately, I have a requirement for the higher resolution image. In any event, if anyone knows of a discussion or any kind of information that covers this topic, I'd be happy to check it out.
Thanks again to any who may reply.

19th May 2017, 18:16
If I do the math, a 2000 x 2000 pixel image is 4 million pixels. If each pixel is a 32-bit RGBA value, this is 16 MB per image, more than the size of the cache for just one image. They are obviously compressed to a smaller size on disk.

22nd May 2017, 23:22
I thought you might be correct d_stranz, so I dissected the image and determined that it only has a depth of 8.

So, in the Qt 5.9 documentation under the Qt 5.8 - Qt GUI - C++ Classes – QpixmapCache description, it states in the ‘Detailed Description’ section, that “The initial cache limit is 10240 KB (10 MB); you can change this by calling setCacheLimit() with the required value. A pixmap takes roughly (width * height * depth)/8 bytes of memory.”

If an image is 2000 pixels by 2000 pixels and the depth is 8 pixels, I get 4,000,000 divided by a depth of 8 bits per pixel = 500,000. Divide that by 8 bytes of memory (or should I divide by 64 bits of memory) I get 62,500. Therefore, two images would require a cache size of 125,000 bits, bytes or kilobytes?????

Based on the above documentation, I am assuming that (width (in pixels) * height (in pixels) * depth (bits per pixel)) / bytes of memory returns a value that is in bytes. However it could just as easily be bits, bytes or KB. If someone could provide clarification on this, it would be highly appreciated.

23rd May 2017, 00:43
Try the math again: (width * height * depth) / 8 = (2000 * 2000 * 8)/8 = 4 million bytes. "depth" is always in bits / pixel (not pixels), and the final division by 8 converts bits to 8-bit bytes.

After you load the image, what does QPixmap::depth() say?

23rd May 2017, 19:15
Yeah you're right, thanks. I divided instead of multiplying.

As to the depth of the pixmap, it fails when loading so I can never get to the point to use the QPixmap::depth() function. The only data that the Locals and Expressions window displays for the image information is the following; width: 2000, height: 2000, nbytes: 16000000, format: 456327200, data: 0x6bcce780. Would I be wrong to assume that nbytes is the number of bytes? In any event, not sure that any of these values represent the depth.

But, thanks to your assistance, I've eliminated the SIGSEGV issue (at least for now) and now receive "can't find linker symbol for virtual table for `QPixmap' value" in the application output. I'm going to do some searching to see I if can figure this new one out. I'm sure I'll post again if I can't. So d_stranz, thank you very much for your support to date.