在类中构造新的变量使我的程序崩溃
Constructing new variables within a class making my program crash
大家好,开始吧:
用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修复了这个问题。不太确定是什么原因造成的。但是现在已经修复了,如果我找到一个合适的答案,我会在这里再次发布一些东西。
相关文章:
- 为什么我的变量没有更新,我的 LED 没有亮起?
- 为什么我的变量存在于其范围之外
- 为什么我的字符串变量没有打印完整的字符串?
- 为什么我的 c++ 程序检查不是初始化的变量?
- 为什么我的全局变量似乎没有变化?
- C ++程序如何返回我的数组或写入全局变量
- 为什么我的数组值与此处的全局变量不匹配?
- 为什么弹出我的堆栈会返回垃圾而不是初始变量?
- 我的包装函数缺少变量?
- 在我的变量打印C++后无法获得 $ 打印符号
- 我的 c++ 程序似乎没有发现字符串和我拥有但输入使用 getline 的变量之间的比较
- 为什么我的双变量通过添加 c++ 显示 nan?
- 我应该使我的局部变量常量还是可移动的
- constexpr 函数在编译时获取值,即使我的变量不是 constexpr
- 我可以在我的类中使用 omnetpp.ini 变量吗?
- 我的类中应该有一个Allocator作为成员变量吗
- 我可以在初始化之前使用 std::array 成员变量中的 data() 指针吗?发出警告
- 为什么我的 std::atomic<int> 变量不是线程安全的?
- 为什么访问我的引用捕获变量会导致我的 lambda 函数出现段错误?
- 为什么我的程序没有更改结构变量中的值?