自定义 Symbian 视图系统,未知位置分段错误,未知原因

发布于 2024-11-07 07:08:34 字数 1317 浏览 0 评论 0原文

我正在开发一个 Symbian 应用程序。 我编写了一个可以轻松更改视图的系统,大致如下:

class ViewManager : public QWidget {
   public slots:
      void changeView( const QString &id ) {
         if( currentView_m ) {
            delete currentView_m;
            currentView_m = 0;
         }

         if( id == "main" ) {
            currentView = new MainView( this );
         }
         else if( ... ) {
            //etc..

         layout_m->addWidget( currentView_m );
         connect( currentView_m, SIGNAL( changeView( QString ) ),
                  this, SLOT( changeView( QString ) ) );
      }
   private:
      View *currentView_m;
};

class View : public QWidget {
   signals:
      void ChangeView( const QString &id );
};

class MainView : public View {
   public slots:
      void onButtonClicked() {
         emit changeView( "someview" );
      } 
};

然后作为示例,我在 main 中使用 ViewManager:

int main( int argc, char *argv[] ) {
   QApp app...

   ViewManager man;
   man.changeView( "main" );

   app.exec();
}

当我第一次更改视图时,它工作得很好,然后当我再次更改视图时时间,它出现段错误!您可能认为当我删除 currentView_m 指针时会出现段错误,但事实并非如此!分段错误发生在程序退出changeView-slot之后。

我不知道如何调试它,因为程序崩溃并显示反汇编程序转储,并且堆栈跟踪仅显示乱码。

难道是在槽调用之后,程序进入了 QApplication 事件循环并在那里崩溃了?我在 View 实现中使用自定义小部件来覆盖一些受保护的 QWidget 事件。

I'm developing a Symbian application.
I've written a system for easily changing views, roughly like this:

class ViewManager : public QWidget {
   public slots:
      void changeView( const QString &id ) {
         if( currentView_m ) {
            delete currentView_m;
            currentView_m = 0;
         }

         if( id == "main" ) {
            currentView = new MainView( this );
         }
         else if( ... ) {
            //etc..

         layout_m->addWidget( currentView_m );
         connect( currentView_m, SIGNAL( changeView( QString ) ),
                  this, SLOT( changeView( QString ) ) );
      }
   private:
      View *currentView_m;
};

class View : public QWidget {
   signals:
      void ChangeView( const QString &id );
};

class MainView : public View {
   public slots:
      void onButtonClicked() {
         emit changeView( "someview" );
      } 
};

Then as an example, I'm using the ViewManager in main:

int main( int argc, char *argv[] ) {
   QApp app...

   ViewManager man;
   man.changeView( "main" );

   app.exec();
}

When I change the view the first time, it works just fine, then when I change the view another time, it segfaults! You might think it segfaults when I delete the currentView_m pointer, but no! The segmentation fault happens right after the program exits the changeView-slot.

I have no idea how to debug this, as the program crashes and shows a disassembler dump, and the stack trace shows only gibberish.

Could it be that after the slot call, the program goes to the QApplication event loop and crashes there? I'm using a custom widgets inside View implementations that override some of the protected QWidget events.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

静谧 2024-11-14 07:08:34

您正在删除正在处理其信号的对象。只需对对象调用 deleteLater() 即可,将删除推迟到“安全”点,而不是删除。

You are deleting a object the signal of which you are processing. Instead of delete, just call deleteLater() on the object, deferring the deletion to a "safe" point.

2024-11-14 07:08:34

首先尝试从布局中删除视图。然后删除视图。为此,您可以使用布局的removeWidget、removeItem 方法,

布局可能会尝试访问删除视图。

另请阅读此 Qt - 从布局中删除所有小部件? 问题。它可能会给你洞察力。

Try removing the view from your layout first. Then delete the view. You can use removeWidget,removeItem methods of layout for this purpose

Layout might be trying to access a delete view.

Read this Qt - remove all widgets from layout? question as well. It might give you insight.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文