2

グーグルで調べた後、プログラムがアクセスできないメモリを使用するように指定されている場合、セグメンテーション違反が発生することがわかりました。

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 クラスのオブジェクトの要素を正しく分解しますか? 多分私はそこに漏れがありますか?

4

1 に答える 1

4

オブジェクトを作成した場所を知らなければ、QSignalMapper に segfault がある理由を判断するのは困難です。シグナル マッパーへのポインターは、ある時点で無効になる可能性があるため、不要な削除が行われる可能性があります。

場合によっては、アプリケーションの完全な再構築が役立つことがあります。また、 qmakeを再実行します。segfault がある理由を説明できない場合は、チェックリストに入れておく必要があります ...;)

QSignalMapper の代替手段があります: シグナルのクリック (int) を保持し、プライベート スロットを実装します。

private slots:
    this_clicked() {
        emit clicked(num);
    }

次に、コンストラクターに入れます。

connect(this, SIGNAL(clicked()), SLOT(this_clicked()));
于 2012-05-21T14:30:03.143 に答える