I am having some issues with a segmentation fault being caused by access to a member variable in a Qt application. My experience of segmentation faults have shown them generally to be a result of accessing a memory location that is out of bounds. However, I'm struggling to figure out why this would be the case in my code.
The class in question is a subclass of QFrame and contains a number of
member variables, including one originally called m_Zoom
of type
unsigned int
. When the frame is created, a number of functions are called
that cause the zoom level to be set and then the window is drawn. This
all works fine. There are then a number of ways that the zoom level can
be changed: opening a new file or pressing either + or -. These read
the existing zoom level and then decide whether to change it. Simply
reading the existing zoom level causes a segmentation fault.
Note that this variable is not a pointer or in an array or anything unusual. The definition was along the lines of:
class MyClass : public QFrame
{
Q_OBJECT
public:
MyClass(QWidget *parent = 0);
void SetZoomLevel(unsigned int zoom);
void ZoomIn();
protected:
void mousePressEvent(QMouseEvent *event);
void mouseReleaseEvent(QMouseEvent *event);
void mouseMoveEvent(QMouseEvent *event);
void paintEvent(QPaintEvent *event);
private:
// A number of private functions for performing calculations
// Some variables including:
double m_OriginShift;
unsigned int m_Zoom;
// Some more variables
};
The function paintEvent
and all the mouse*
event functions access
m_Zoom
and these work fine (the mouse is used to move the view
around). The constructor calls SetZoomLevel
(which is basically
m_Zoom = zoom
). Again, this works with no problems. ZoomIn
starts
with:
void MyClass::ZoomIn()
{
qDebug() << "Zoom In";
unsigned int existingZoom = m_Zoom;开发者_如何学运维 // [1]
qDebug() << "Existing zoom is " << existingZoom;
...
The segmentation fault happens in this function, before the second
qDebug
reports anything.
I've tried stepping through the application in cygwin's gdb and I get to
the line marked [1]
and type stepi
and get an immediate segfault.
Having moved the variable around (in case something else was overwriting
it's location), changed to using a pointer with malloc
d memory, moved
it into a separate structure, moved it into a separate class (both with a member instance and with a member pointer with a new
line in the constructor) and
probably a few other random and unsuccessful acts of desperation, I've
completely run out of ideas.
Please can anyone suggest any good ways that I could go about debugging this issue?
I'm using Qt 4.6.3 on Windows XP compiling with GNU Make 3.81 and g++ version 4.4.0 (from the mingw that came with the Qt installer).
While this is a wild guess, there's a possibility that you're calling that method on a null object.
Note that:
unsigned int existingZoom = m_Zoom;
is equivalent to
unsigned int existingZoom = this->m_Zoom;
If you called this method from a null pointer:
someUninitializedPtr->ZoomIn();
then this is probably the result you'd get - a segmentation fault upon first try of dereferencing this
pointer, having the value of null
.
精彩评论