qt處理pcl點雲時可能的bug(一)

2020/4/13

原因

在使用qt開發畢設項目界面時,使用QVtkWidget控件,並在項目中調用下列代碼

viewer->updatePointCloud (colored_cloud, "colored cloud");
ui->qvtkWidget->update ();

可能會產生bug,錯誤是vtk庫中拋出的vector::reserve容量溢出異常,應該是vtk的一個bug,剛開始我還不確定是什麼問題,調試到vtk庫中的代碼時又都是彙編代碼,於是我思考如何能調試vtk庫,考慮到之前安裝vtk時,使用的cmake參數是-DCMAKE_BUILD_TYPE="Release",因此我想應該跟這裏有關,將選項修改爲-DCMAKE_BUILD_TYPE="Debug",編譯出的庫就能進行調試了,爲了防止我原先的庫被覆蓋,我也加了這個選項-DCMAKE_DEBUG_POSTFIX="d",接着是編譯make -j4和安裝sudo make install。在.pro文件中加入的vtk庫爲LIBS += /usr/local/lib/libvtk*d.so,調試發現,果然是vtk的bug,報錯點在reserve()處

//VTK-8.2.0/Rendering/OpenGL2/vtkOpenGLIndexBufferObject.cxx
// used to create an IBO for point primitives
void vtkOpenGLIndexBufferObject::AppendPointIndexBuffer(
  std::vector<unsigned int> &indexArray,
  vtkCellArray *cells,
  vtkIdType vOffset)
{
  vtkIdType* indices(nullptr);
  vtkIdType npts(0);
  size_t targetSize = indexArray.size() +
    cells->GetNumberOfConnectivityEntries() -
    cells->GetNumberOfCells();           //0+ 62649-12548 負數轉無符號長整數,造成溢出
  if (targetSize > indexArray.capacity())
  {
    if (targetSize < indexArray.capacity()*1.5)
    {
      targetSize = indexArray.capacity()*1.5;
    }
    indexArray.reserve(targetSize);  //throw length error
  }

  for (cells->InitTraversal(); cells->GetNextCell(npts, indices); )
  {
    for (int i = 0; i < npts; ++i)
    {
      indexArray.push_back(static_cast<unsigned int>(*(indices++)+vOffset));
    }
  }
}

於是爲了避免bug發生,我把上述代碼修改爲了

//調用viewer->updatePointCloud()會發生很奇怪的錯誤,應該是vtk的bug
viewer->removePointCloud("colored cloud");
viewer->addPointCloud (colored_cloud, "colored cloud");
ui->qvtkWidget->update ();

至此,bug就不再出現了。
PS:1)我也在網上搜索了一下調試動態庫的方法,只要加上gcc -g選項,就能編譯得到附加調試信息的庫,因此,我想到-DCMAKE_BUILD_TYPE="Debug"選項在後續處理中應該也有類似的作用吧,生成的makefile文件在make編譯執行時,也能爲庫文件附加調試信息。
         2)學到的一個查看庫文件是否包含調試信息的方法,通過gdb *.so就能查看動態庫有無調試信息了。

#不含調試信息
➜ gdb libvtkCommonMath-8.2.so 
GNU gdb (GDB) 9.1
Copyright (C) 2020 Free Software Foundation, Inc.
License GPLv3+: GNU GPL version 3 or later <http://gnu.org/licenses/gpl.html>
This is free software: you are free to change and redistribute it.
There is NO WARRANTY, to the extent permitted by law.
Type "show copying" and "show warranty" for details.
This GDB was configured as "x86_64-pc-linux-gnu".
Type "show configuration" for configuration details.
For bug reporting instructions, please see:
<http://www.gnu.org/software/gdb/bugs/>.
Find the GDB manual and other documentation resources online at:
    <http://www.gnu.org/software/gdb/documentation/>.

For help, type "help".
Type "apropos word" to search for commands related to "word"...
Reading symbols from libvtkCommonMath-8.2.so...
(No debugging symbols found in libvtkCommonMath-8.2.so)


#包含調試信息
➜ gdb libvtkCommonMath-8.2d.so
GNU gdb (GDB) 9.1
Copyright (C) 2020 Free Software Foundation, Inc.
License GPLv3+: GNU GPL version 3 or later <http://gnu.org/licenses/gpl.html>
This is free software: you are free to change and redistribute it.
There is NO WARRANTY, to the extent permitted by law.
Type "show copying" and "show warranty" for details.
This GDB was configured as "x86_64-pc-linux-gnu".
Type "show configuration" for configuration details.
For bug reporting instructions, please see:
<http://www.gnu.org/software/gdb/bugs/>.
Find the GDB manual and other documentation resources online at:
    <http://www.gnu.org/software/gdb/documentation/>.

For help, type "help".
Type "apropos word" to search for commands related to "word"...
Reading symbols from libvtkCommonMath-8.2d.so...

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章