void QtWidgetsApplication2::pt_clicked(QString data1, QString data2) { pcl::console::TicToc time; // --------------------------------读取点云------------------------------------ pcl::PointCloud<pcl::PointXYZ>::Ptr cloud(new pcl::PointCloud<pcl::PointXYZ>); if (pcl::io::loadPCDFile<pcl::PointXYZ>("opened_cloud.pcd", *cloud) == -1) { PCL_ERROR("Cloudn't read file!"); } //cout << "滤波前点的个数为:" << cloud->size() << endl; // --------------------------------直通滤波------------------------------------ float a = data1.toFloat(); float b = data2.toFloat(); pcl::PointCloud<pcl::PointXYZ>::Ptr filtered(new pcl::PointCloud<pcl::PointXYZ>); std::string fv = "z"; // 滤波字段 filtered = pcl_filter_passthrough(cloud, a, b, fv); //cout << "直通滤波用时:" << time.toc() << " ms" << endl; pcl::io::savePCDFileASCII("opened_cloud.pcd", *filtered); ui.textBrowser->clear(); QString Pointsize = QString("%1").arg(cloud->points.size()); ui.textBrowser->insertPlainText(QStringLiteral("点云数量:") + Pointsize); QString Pointsize1 = QString("%1").arg(filtered->points.size()); ui.textBrowser->insertPlainText(QStringLiteral("\n滤波后点云数量:") + Pointsize1); auto renderer2 = vtkSmartPointer<vtkRenderer>::New(); auto renderWindow2 = vtkSmartPointer<vtkGenericOpenGLRenderWindow>::New(); renderWindow2->AddRenderer(renderer2); viewer.reset(new pcl::visualization::PCLVisualizer(renderer2, renderWindow2, "viewer", false)); ui.openGLWidget->setRenderWindow(viewer->getRenderWindow()); viewer->setupInteractor(ui.openGLWidget->interactor(), ui.openGLWidget->renderWindow()); viewer->setBackgroundColor(0, 0, 0); //设置背景 pcl::visualization::PointCloudColorHandlerGenericField<pcl::PointXYZ> fildColor(filtered, "z"); viewer->addPointCloud<pcl::PointXYZ>(filtered, fildColor, "sample cloud"); viewer->setPointCloudRenderingProperties(pcl::visualization::PCL_VISUALIZER_POINT_SIZE, 1, "sample cloud"); viewer->resetCamera(); update(); };这段代码存在内存泄漏的问题
时间: 2023-09-26 22:09:06 浏览: 55
在这段代码中,存在内存泄漏的问题,主要是在以下两个地方:
1. 在使用`pcl_filter_passthrough`函数时,返回的`filtered`点云指针被赋值给了`filtered`智能指针,但是在函数结束时,`filtered`智能指针没有被释放,导致内存泄漏。
2. 在使用`viewer`对象时,通过`reset`函数将其重新初始化,但是在此之前并没有释放其之前的内存,也就是说,之前分配的内存没有被正确释放,导致内存泄漏。
为了解决这个问题,可以在使用`pcl_filter_passthrough`函数时,将返回的点云指针直接赋值给`filtered`智能指针,这样在函数结束时,智能指针会自动释放其所指向的内存。同时,在使用`viewer`对象时,可以先调用`removeAllPointClouds`函数,将之前的点云对象从视图中移除,然后再调用`reset`函数重新初始化`viewer`对象,这样可以避免内存泄漏。修改后的代码如下:
```
void QtWidgetsApplication2::pt_clicked(QString data1, QString data2) {
pcl::console::TicToc time;
// --------------------------------读取点云------------------------------------
pcl::PointCloud<pcl::PointXYZ>::Ptr cloud(new pcl::PointCloud<pcl::PointXYZ>);
if (pcl::io::loadPCDFile<pcl::PointXYZ>("opened_cloud.pcd", *cloud) == -1)
{
PCL_ERROR("Cloudn't read file!");
}
//cout << "滤波前点的个数为:" << cloud->size() << endl;
// --------------------------------直通滤波------------------------------------
float a = data1.toFloat();
float b = data2.toFloat();
std::string fv = "z"; // 滤波字段
pcl::PointCloud<pcl::PointXYZ>::Ptr filtered = pcl_filter_passthrough(cloud, a, b, fv);
//cout << "直通滤波用时:" << time.toc() << " ms" << endl;
pcl::io::savePCDFileASCII("opened_cloud.pcd", *filtered);
ui.textBrowser->clear();
QString Pointsize = QString("%1").arg(cloud->points.size());
ui.textBrowser->insertPlainText(QStringLiteral("点云数量:") + Pointsize);
QString Pointsize1 = QString("%1").arg(filtered->points.size());
ui.textBrowser->insertPlainText(QStringLiteral("\n滤波后点云数量:") + Pointsize1);
viewer->removeAllPointClouds(); //移除之前的点云对象
viewer->resetCamera();
pcl::visualization::PointCloudColorHandlerGenericField<pcl::PointXYZ> fildColor(filtered, "z");
viewer->addPointCloud<pcl::PointXYZ>(filtered, fildColor, "sample cloud");
viewer->setPointCloudRenderingProperties(pcl::visualization::PCL_VISUALIZER_POINT_SIZE, 1, "sample cloud");
ui.openGLWidget->update();
};
```