【发布时间】:2012-05-28 01:05:08
【问题描述】:
在谷歌搜索后,我发现当程序被指向使用它无权访问的内存时会出现分段错误。
我最近在尝试创建一个自定义按钮类后开始遇到这些错误,该类将在其 clicked() 信号中包含一个整数。
这是自定义按钮类:
.h:
#include <QtGui>
#ifndef CUSTOMBUTTON_H
#define CUSTOMBUTTON_H
class CustomButton : public QPushButton //This simple class allows us to map arguments in the Widget's cicked() signal.
{ //In the project, it's used in the delete-edit buttons so the program knows which part of the Movie/Theater/Screening list we're referring to
Q_OBJECT
public:
CustomButton(QString name,int num, QWidget *parent = 0);
signals:
void clicked(int pos);
private:
QSignalMapper *signalMapper;
};
#endif // CUSTOMBUTTON_H
.cpp:
#include "custombutton.h"
CustomButton::CustomButton(QString name,int num = 0, QWidget *parent) //Our constructor
: QPushButton(name,parent)
{ //Our button's now created and shown, through the superconstructor. Let's take care of its clicked() signal.
signalMapper = new QSignalMapper(this);
connect(this, SIGNAL(clicked()), signalMapper, SLOT(map()));
signalMapper->setMapping(this, num);
connect(signalMapper, SIGNAL(mapped(int)),this, SIGNAL(clicked(int)));
}
然后在我的主代码中:
CustomButton *edit_button = new CustomButton("Edit",i,0);
edit_button->setFixedWidth(30);
connect(edit_button,SIGNAL(clicked(int)),this,SLOT(edit_movie(int))); //When the user decides to edit a movie, we set the class'es working_with var to the position of the film in the MovieList and we set the screen to the appropriate one.
CustomButton *del_button = new CustomButton("Delete",i,0); //We pass i as an argument to the button, so it can relate to the movie it's set next to.
del_button->setFixedWidth(45);
connect(del_button,SIGNAL(clicked(int)),this,SLOT(del_movie(int)));
其中 i 是我希望它在信号中包含的数字。
问题是,通过调试器,如果我按下删除按钮,它不会给我一个段错误。这一切都发生在编辑中。
由于我仍然很困惑,甚至不知道该问什么,如果您需要我的其他任何东西,请说出来,我会提供。
这是回溯,我不知道如何阅读:
0 QHash<QObject*, QString>::findNode qhash.h 884 0×69e98594
1 QHash<QObject*, QString>::contains qhash.h 874 0×69e98568
2 QSignalMapper::map qsignalmapper.cpp 267 0×69debe0c
3 QSignalMapper::map qsignalmapper.cpp 257 0×69debda2
4 QSignalMapper::qt_static_metacall moc_qsignalmapper.cpp 64 0×69debfce
5 QMetaObject::activate qobject.cpp 3547 0×69de9baf
6 QAbstractButton::clicked moc_qabstractbutton.cpp 220 0×10cb4b8
7 QAbstractButtonPrivate::emitClicked qabstractbutton.cpp 548 0xe2e517
8 QAbstractButtonPrivate::click qabstractbutton.cpp 541 0xe2e495
9 QAbstractButton::mouseReleaseEvent qabstractbutton.cpp 1123 0xe2f941
10 QWidget::event qwidget.cpp 8362 0xae63de
11 QAbstractButton::event qabstractbutton.cpp 1082 0xe2f7cc
12 QPushButton::event qpushbutton.cpp 683 0xecfeba
13 QApplicationPrivate::notify_helper qapplication.cpp 4554 0xa9c020
14 QApplication::notify qapplication.cpp 4097 0xa9a26a
15 QCoreApplication::notifyInternal qcoreapplication.cpp 876 0×69dd3b76
16 QCoreApplication::sendSpontaneousEvent qcoreapplication.h 234 0×113137e
17 QApplicationPrivate::sendMouseEvent qapplication.cpp 3163 0xa98ad6
18 QETWidget::translateMouseEvent qapplication_win.cpp 3363 0xb03171
19 QtWndProc qapplication_win.cpp 1696 0xafdf66
20 USER32!IsDialogMessageW C:\Windows\syswow64\user32.dll 0 0×76726238
21 USER32!RegisterSystemThread C:\Windows\syswow64\user32.dll 0 0×767278b0
22 ?? 0 0×30000
23 USER32!AllowForegroundActivation C:\Windows\syswow64\user32.dll 0 0×767268ea
24 qt_is_translatable_mouse_event qapplication_win.cpp 1463 0xafd465
25 USER32!GetMessageExtraInfo C:\Windows\syswow64\user32.dll 0 0×76727d31
26 ?? 0
有什么可能导致问题的原因吗?正如我所说,只有当我通过调试器运行程序并按下 CustomButton 类的“编辑”按钮时,才会发生段错误。 当我正常构建和运行时,程序运行 9/10 次。有时,单击编辑按钮会导致崩溃。这种不稳定的行为导致我在这里寻求帮助。
当我单击按钮时,我被带到一个新屏幕,我怀疑问题可能出在析构函数上?空白析构函数是否正确解构 CustomButton 类对象的元素,因为实际对象已传递给基类构造函数?也许我那里有泄漏?
【问题讨论】:
-
欢迎来到 StackOverflow!我认为我们需要更多代码,因为我在您的课程中看不到任何错误。请在您使用课程的地方添加更多代码。也许将父指针传递给您的 CustomButton 实例会更好。 (我猜你在另一个小部件中创建它们。)
-
@Dimitris:堆栈跟踪显示它正在 QSignalMapper 中退出,我第一眼看不出有什么问题,请问您为什么使用它?
-
@leemes 我的整个项目非常大,我最不想做的就是让你们接触到我的大量代码而疏远或厌倦你们。我已将问题范围缩小到这里。如果您想以任何方式查看更多内容,这就是全部内容。发生这种情况的情况是 showScreen() 中切换的情况 3:pastebin.com/raw.php?i=Eygywtn1 尝试将“this”作为父级传递,问题仍然存在。也感谢您的欢迎。
-
实际上我可以大致看到这个问题,QSignalMapper 在它自己的上下文中是一个有用的工具,但我认为它不适合你在创建多个映射器时尝试使用它的方式,因此,不是为多个按钮使用一个映射器,而是为一个按钮获得多个映射器。
-
@NicholasSmith 为什么我使用了 QSignalMapper?我是整个信号和插槽系统的新手,在谷歌搜索和阅读教程之后,似乎用信号传递参数的唯一方法(在我的项目中通过按钮的 clicked() 信号传递数字)是映射使用 signalMapper 对象自己自定义信号的值。我搞砸了,不知何故?还有其他方法吗?
标签: c++ qt segmentation-fault