通常指针导致分段错误 [英] Usual pointer causing Segmentation Fault

查看:86
本文介绍了通常指针导致分段错误的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我的班级ComponentArea如下所示:

My class "ComponentArea" looks like this:

#ifndef COMPONENTAREA_H
#define COMPONENTAREA_H

#include <QObject>
#include <QWidget>
#include <QScrollArea>
#include <QtDesigner/QtDesigner>
#include <QMouseEvent>
#include <QPainter>

#include <Components/Sockets/socket.h>
class Socket;

class ComponentArea : public QScrollArea
{
    Q_OBJECT

    public:
        ComponentArea(QWidget* parent);

        void connectSockets(Socket* a, Socket* b);
        void childBlock_childSocket_mousePressEvent(Socket* sender, QMouseEvent* event);
        void childBlock_childSocket_mouseReleaseEvent(Socket* sender, QMouseEvent* event);

    private:
        Socket* pressedSocket;

        void mouseReleaseEvent(QMouseEvent *event);
};

#endif // COMPONENTAREA_H





在构造函数中,我将pressedSocket设置为0:



In the constructor, I set pressedSocket to 0:

pressedSocket = 0;





每当我尝试使用pressedSocket时,在其中一个事件中,我会遇到分段错误:



Whenever I try to use pressedSocket, in one of the events, I get a Segmentation Fault:

void ComponentArea::childBlock_childSocket_mousePressEvent(Socket *sender, QMouseEvent *event)
{
    if((pressedSocket == 0) && (event->button() == Qt::LeftButton)) //SIGSEGV here
    {
        pressedSocket = sender;
    }
}





当我尝试在此行检查pressedSocket时,调试器告诉我pressedSocket< ;没有这样的价值>。



我看不出我做错了什么。有什么建议吗?在此先感谢!



When I try to inspect pressedSocket at this line, the Debugger tells me "pressedSocket <no such value>".

I don't see what I am doing wrong. Any advices? Thanks in advance!

推荐答案

我的猜测是你的ComponentArea对象不存在(还)。看看你分配该对象的位置。因此,this指针可能指向nirwana然后访问任何成员将导致分段错误。
My guess is that your ComponentArea object does not exist (yet). Take a look where you allocate that object. Hence, the this pointer is probably pointing into nirwana and then the access to any member will cause a segmentation fault.


最后我想出了对另一个类中的ComponentView的引用,我打电话给事件,没有分配。因此,我能够调用该事件,但没有pressSocket存在。
Finally I figured out that a reference to the ComponentView in another class, from which I called the event, was not assigned. Therefore, I was able to call the event, but there was no pressedSocket existent.


这篇关于通常指针导致分段错误的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆