在类中构造新的变量使我的程序崩溃

Constructing new variables within a class making my program crash

本文关键字:变量 我的 程序 崩溃      更新时间:2023-10-16

大家好,开始吧:

用c++编写

使用QT Creator版本5.4.1

所以我的程序有一个问题,当我开始用一些成员变量构造新的类时,我的程序崩溃了。我试过给它们赋值,在头中,在类构造函数中,在类初始化中都没有帮助。以下是我认为导致问题的原因:

类被称为WfsFeatureViewer使用头文件和Qt Ui文件。Ui在构造时初始化。

std::vector<CustomLineItem*> m_line_items;
std::vector<CustomPointItem*> m_point_items;
std::vector<CustomPolygonItem*> m_polygon_items;

它们是简单的多边形/线/点项目,从QGraphicsPolygonItem,PointItem和Line item子类。没什么太过分的。如果我从我的类中排除这些变量,它不会崩溃(但重要的是它们是这个类的成员变量)

我想知道如果它的速度我正在创建类?通常我收到5-60个这样的信号,然后就崩溃了。WfsFeatureViewer类被添加到QListWidgetView中。所以使用循环创建了一堆它们供用户看到。如下图所示:

// I used for index to pass the the WfsFeatureViewer
int i = 0;
// Loop for all the WFS Feature names and elements
for(auto feature_itr : m_layer_elements)
{
    // Increment I per loop
    i++;
    // Create a new WfsFeatureViewer
    auto wfs_viewer(new WfsFeatureViewer());
    QObject::connect(wfs_viewer,&WfsFeatureViewer::sendNewShps,this,&DialogWfsReader::catchNewShps);
    //Set some UI data and base WFS for later reading.
    wfs_viewer->setElementAndName(feature_itr.first,feature_itr.second);
    wfs_viewer->setIndex(i);
    wfs_viewer->setWfsBaseURL(m_ui->m_line_wfs_name->text().toStdString());
    // Loop to get the layer count for this wfsViewer
    for(auto layer_itr : m_layer_count)
    {
        // check if its the correct name
        if(layer_itr.first == feature_itr.first)
        {
            // Set the layer count
            wfs_viewer->setLayerCount(layer_itr.second);
        }
    }
    // loop to get the shape type
    for(auto shp_itr : m_layer_shp_type)
    {
        // check if its the correct name
        if(shp_itr.first == feature_itr.first)
        {
            // set the shape type
            wfs_viewer->setShapeType(shp_itr.second);
        }
    }
    // Create a new QListWidgetItem
    QListWidgetItem* item(new QListWidgetItem);
    // Add it to the QListWidget
    m_ui->m_list_layers->addItem(item);
    // Set the wfs_viewer onto the item
    m_ui->m_list_layers->setItemWidget(item,wfs_viewer);
    // Give it a size
    item->setSizeHint(QSize(100,210));
    // And adjust the UI
    wfs_viewer->adjustSize();
}

所以这可以循环很多次,这意味着创建了很多新对象,如果我在。

中加载大约70-100个类,大约需要10-30MB的内存。

我不太确定是什么原因造成的,我也没有得到堆错误,所以我知道我没有杀死堆。

好的,我现在似乎已经修复了它,当创建新类时停止崩溃一次。一个完整的Clean, QMake和rebuild修复了这个问题。不太确定是什么原因造成的。但是现在已经修复了,如果我找到一个合适的答案,我会在这里再次发布一些东西。