0

I have a very strange problem with QPixmap in Qt. I'm coding in C++ btw. Anyways the problem is, that as soon as I want to create a 9th QPixmap pointer in my main window class, the program crashes. so this works:

class MainWindow : public QMainWindow
{
    Q_OBJECT
    QPixmap *doorOpened, *doorClosed, *dirUp, *dirDown, *dirNone, *timePause, *timePlay, *timeStop;
     //QPixmap *doorOpen;
 public:
    explicit MainWindow(QWidget *parent = 0);
    ~MainWindow();
private:
    Ui::MainWindow *ui;
};

and this crashes:

class MainWindow : public QMainWindow
{
    Q_OBJECT
    QPixmap *doorOpened, *doorClosed, *dirUp, *dirDown, *dirNone, *timePause, *timePlay, *timeStop;
     QPixmap *doorOpen;
 public:
    explicit MainWindow(QWidget *parent = 0);
    ~MainWindow();
private:
    Ui::MainWindow *ui;
};

This stuff is partially done with the Qt Creator and window designer, as you might have guessed by the code.

So what am I doing wrong here that causes this strange behaviour?

Thanks in advance!

rfreytag
  • 1,203
  • 11
  • 18
  • 1
    How does it crash? Do you have some error in stderr? Feel free to share it :) – bartimar Jun 01 '13 at 19:04
  • @bartimar it just segfaults. anyways, I fixed it by instantiating the QTimer I had in the constructor after everything QPixmap related. Odd though. – rfreytag Jun 01 '13 at 19:51
  • As an ["implicit shared class"](http://qt-project.org/doc/qt-4.8/implicit-sharing.html#implicit-data-sharing) `QPixmap` is already a kind of smart pointer, so it shouldn't be used with a pointer anyway. – alexisdm Jun 02 '13 at 22:04

1 Answers1

1

Try initializing all of your pointers to zero first in your constructor before you initialize or access them.

Also QPixmap has a function isNull() . This can be useful for checking for errors with them.

Also if you use the default constructor (doorOpen = new QPixmap();) for a pixmap and then call load() with the filename you want to use, you are able to check the return value of load() to perform error checking.

http://qt-project.org/doc/qt-4.8/qpixmap.html#isNull

http://qt-project.org/doc/qt-4.8/qpixmap.html#load

Hope that helps.

phyatt
  • 18,472
  • 5
  • 61
  • 80
  • thanks for the fast answere, but the problem is that it crashes even if I don't use it. just the presence of the pointer variable causes the crash. – rfreytag Jun 01 '13 at 19:13
  • Do you use the other pointers? Something that you do is crashing the program. As bartimar already asked, how does the program crash and where? You have run the program in the debugger, haven't you? –  Jun 01 '13 at 19:23
  • oh well, I found the issue. For whatever reason I have to instantiate the QTimer I used in the constructor of Mainwindow after I everything QPixmap related. I have no idea why, but now it works. – rfreytag Jun 01 '13 at 19:50