从函数返回时对象被破坏

时间:2014-02-11 17:53:51

标签: c++ qt constructor namespaces

我正在研究qt项目,并注意到我的应用程序崩溃了  在调试时我发现它是因为没有创建/初始化qpushbutton对象。

现在我无法理解为什么它没有被初始化?

我在命名空间中编写函数,调用所有Qpushbutton的构造函数,我通过引用传递对象 一旦功能恢复,它不应该保留它的价值吗?但在我的情况下,一旦函数返回它就被销毁了? 是否与Namespace ???

相关

任何帮助或批评会有所帮助吗?

   MY_Utility.h
  class MY_Utility
  {

private:
    QPushButton* add_cal_button_; 
    QPushButton*  sub_cal_button_; 
    QPushButton*  mul_button_; 

  }
My_Utility.cpp

namespace
{
   void create_push_button_for_cal( QPushButton *button_cal, QString button_name, QGridLayout *grid, int grid_row, int grid_col )
    {
        button_cal = new QPushButton( button_name );

        if(!button_cal )
    {
        msg.setInformativeText("The code does not come here so object is created");
        QMessageBox msg;
        msg.setText("Messsage");
        msg.setInformativeText("OBject not initialised in create_push button  ");
        msg.exec();;
    }
        button_cal->setFixedSize( 200 , 40 );
        button_cal->setVisible( false );
        grid->addWidget(  button_cal, grid_row, grid_col );
    }


    // function where we call all button created
void create_main_view( MY_Utility* main_p, QTreeWidget* tree_p, QTableWidget* table_p, QPushButton* add_cal_button_,  
                                                                                                   QPushButton* sub_cal_cal_button_, 
                                                                                                   QPushButton* mul_cal_button_,

{
    QWidget*        center_p = new QWidget( main_p );
    QHBoxLayout*    layout_p = new QHBoxLayout( center_p );
    QGridLayout*    grid     = new QGridLayout( center_p );
    grid->setSpacing( 1 );

    create_push_button_for_cal( add_cal_button_, "addition_calculate" , grid, 1, 2 );
    if( !add_cal_button_ )
    {
        QMessageBox msg;
        msg.setText("Messsage");
        msg.setInformativeText("OBject not initialised in first part of message  why not ?? ");
        msg.exec();;
    }
    create_push_button_for_cal( sub_cal_cal_button_ ,"sub_Calculate",           grid, 2, 2 );
    create_push_button_for_cal( mul_cal_button_ ,    "multplication_Calculate", grid, 3, 2 );
    // Make the QWidget the central widget so we can resize within it
    main_p->setCentralWidget( center_p );
   bla .. 
   bla ..



}

}

My_Utility::set_all_cal_button_visible(){
 add_cal_button_->setVisible(true) ; // it crashes here 

}

1 个答案:

答案 0 :(得分:2)

您的问题是C ++问题,而不是Qt问题。

void create_push_button_for_cal(QPushButton *button_cal, QString button_name)
{
    button_cal = new QPushButton(button_name);
    Q_ASSERT(button_cal);
}

指针(地址)按值传递。您的代码丢弃此值,并使用指向新实例的指针覆盖它。

检查null button_cal是没用的,因为new包含一个很好的不变量:如果执行后面的代码执行,则意味着它已成功分配内存。它让生活变得非常简单:如果new返回一个值,那就没问题了。它不会返回无效或空值。

如果new失败,那么断言将永远不会执行,事实上,new甚至不会返回值。它会抛出一个异常,当抛出异常时,你可以做的很少,因为几乎所有东西都需要更多的内存,而我们刚用完了:(

您需要更改函数的签名以返回指向新创建的实例的指针。您还应该通过const引用传递Qt类,而不是通过值传递。如果函数中的其他代码可能抛出异常,请使用智能指针来保护自己不会泄漏按钮实例。所以:

QPushButton * create_push_button_for_cal(const QString & button_name) {
    QScopedPointer<QPushButton> btn(new QPushButton(button_name));
    ...
    // If any code here throws an exception, the scoped pointer will delete
    // the button instance, so that it won't leak.
    ...
    return btn.take();
}