leaf_iterator error

95 views
Skip to first unread message

Mayar Attawiya

unread,
Jun 20, 2020, 5:43:54 PM6/20/20
to OctoMap developers and users discussion
I am trying to iterate through the leaf nodes of a given octree and count the number of occupied cells. I know that the tree is not empty because it is not returning null. The problem is it doesn't seem to enter the loop of the iterator and returns the error : Segmentation fault (core dumped)
This is my code:

double c=0;
int main( int argc, char** argv )

{
  //initialize the node

   ros::init(argc, argv, "volume_calc");
   ros::NodeHandle node;

   AbstractOcTree* tree = AbstractOcTree::read("/home/mayarattawiya/gp2/src/rtabmap_ros/generated_octomaps/octomap_rtabmap5_stairs.ot");
   std::cout<<"I am here"<<endl;
   if (tree){

     OcTree* octree = dynamic_cast<OcTree*>(tree);
     std::cout<<"I am before the iterator"<<endl;

     for(OcTree::iterator it = octree->begin(); it != octree->end(); ++it)
      {
       std::cout<<"I am inside the iterator"<<endl;
       c=c+1;
      }
    }
}
 
  std::cout<<c<<endl;
   exit(0);
   return 0;


}//main

Armin Hornung

unread,
Jun 28, 2020, 6:38:21 AM6/28/20
to oct...@googlegroups.com
That is weird, especially because you don't even modify the tree at all.

This could be related to the bug here (and indeed be a minimal example
to reproduce):

https://github.com/OctoMap/octomap/issues/287


Could you check if the fix from
https://github.com/OctoMap/octomap/pull/288 works for you? Unfortunately
I didn't have a chance to have a look into it yet.


Best regards,

Armin


Mayar Attawiya

unread,
Jul 1, 2020, 11:30:48 AM7/1/20
to OctoMap developers and users discussion
Thank you for your reply.
I tried the fix but unfortunately still the same error. I also tried to put an empty loop- incrementing a variable- as a delay before the iterator but still the same error occurs.
I would appreciate it much if you could look at it and I can also provide the map if that's gonna help.

Christoph Sprunk

unread,
Jul 1, 2020, 11:11:41 PM7/1/20
to oct...@googlegroups.com, Mayar Attawiya
I tried but could not reproduce the issue. Note that I had commented out
the ros node initialization. If possible, can you share your file?

Cheers,
Christoph
> --
> You received this message because you are subscribed to the Google
> Groups "OctoMap developers and users discussion" group.
> To unsubscribe from this group and stop receiving emails from it, send
> an email to octomap+u...@googlegroups.com
> <mailto:octomap+u...@googlegroups.com>.
> To view this discussion on the web visit
> https://groups.google.com/d/msgid/octomap/2e677be3-be43-46be-a4f6-67a4694649b1o%40googlegroups.com
> <https://groups.google.com/d/msgid/octomap/2e677be3-be43-46be-a4f6-67a4694649b1o%40googlegroups.com?utm_medium=email&utm_source=footer>.

Mayar Attawiya

unread,
Jul 2, 2020, 6:40:20 AM7/2/20
to OctoMap developers and users discussion
Hello, thanks for replying.
I now know that the error emerges from the ( end=octree->end_leafs() ) part. Looks like the end_leafs() isn't returning anything.
My octomap was generated by rtabmap and I will add a link of it (i will also add its .bt format)
I also tried the code on one of the octomaps from the dataset here ( http://ais.informatik.uni-freiburg.de/projects/datasets/octomap/ ), the one i tried is freiburg1_360.ot.gz
but still I got the same error.
I also tried to reinstall the octomap library and packages.
I am using ROS kinetic.

I realized that the same error emerges when I use any kind of iterator. Below is the code I am now working on using the leaf iterator:

#include <ros/ros.h>
#include <sensor_msgs/PointCloud2.h>
#include <stdio.h>
#include <octomap/octomap.h>
#include <octomap/OcTree.h>
#include <octomap/OcTreeBase.h>
#include <octomap_server/TrackingOctomapServer.h>
#include <octomap_server/OctomapServer.h>
#include <octomap_server/OctomapServerMultilayer.h>
#include <octomap/AbstractOccupancyOcTree.h>
#include <octomap/octomap_types.h>
#include <octomap/OcTreeNode.h>
#include <string>


using namespace std;
using namespace octomap;
int j=0;


int main( int argc, char** argv )

{
  //initialize the node

   ros::init(argc, argv, "volume_calc");
   ros::NodeHandle node;

   AbstractOcTree* tree = AbstractOcTree::read("/home/mayarattawiya/Downloads/freiburg1_360.ot");

   std::cout<<"I am here"<<endl;
   if (tree)
   {

     OcTree* octree = dynamic_cast<OcTree*>(tree);

//a dumb loop as a delay
     for(int i=0; i<10000000; ++i)
    {
       j=j+1;
      // std::cout<<j<<endl;

     }
     std::cout<<"I am before the iterator"<<endl;


     for(OcTree::leaf_iterator it = octree->begin_leafs(), end=octree->end_leafs(); it!= end; ++it)

        {
          std::cout<<"I am inside the iterator"<<endl;
 

      }
 



}
   exit(0);
   return 0;


}//main

Mayar Attawiya

unread,
Jul 2, 2020, 6:43:34 AM7/2/20
to OctoMap developers and users discussion
The octomap generated from rtabmap:


Thank you so much in advance.

Christoph Sprunk

unread,
Jul 2, 2020, 10:17:16 AM7/2/20
to oct...@googlegroups.com, Mayar Attawiya
Thanks, I can reproduce now.

The issue seems to be that the dynamic_cast is failing, "octree" is
holding a null pointer. It looks like you are casting to the wrong type,
for the freiburg1_360 for example you'd want "ColorOcTree" instead of
"OcTree". You can get the type of the tree as string with the
getTreeType() method, see the example below.

#include <iostream>
#include <stdlib.h>

#include <octomap/OcTree.h>

int main( int argc, char** argv ){
octomap::AbstractOcTree* tree =
octomap::AbstractOcTree::read("/home/sprunkc/Downloads/freiburg1_360.ot");
if (tree){
std::cout << "Loaded tree of type " << tree->getTreeType() <<
std::endl;
const octomap::OcTree* octree = dynamic_cast<octomap::OcTree*>(tree);
if(octree == nullptr){
std::cout << "Dynamic cast failed, octree is nullptr, expect
crash" << std::endl;
}
std::cout << "If octree is nullptr next line will yield an invalid
read of size 8 and segfault." << std::endl;
octomap::OcTree::iterator it_end = octree->end();
std::cout << "Done" << std::endl;
}

return 0;
}

On 02.07.20 03:40, Mayar Attawiya wrote:
> Hello, thanks for replying.
> I now know that the error emerges from the ( end=octree->end_leafs() )
> part. Looks like the end_leafs() isn't returning anything.
> My octomap was generated by rtabmap and I will add a link of it (i will
> also add its .bt format)
> I also tried the code on one of the octomaps from the dataset here (
> http://ais.informatik.uni-freiburg.de/projects/datasets/octomap/ ), the
> one i tried is freiburg1_360.ot.gz
> <http://ais.informatik.uni-freiburg.de/projects/datasets/octomap/freiburg1_360.ot.gz>
> > an email to oct...@googlegroups.com <javascript:>
> > <mailto:oct...@googlegroups.com <javascript:>>.
> <https://groups.google.com/d/msgid/octomap/2e677be3-be43-46be-a4f6-67a4694649b1o%40googlegroups.com?utm_medium=email&utm_source=footer
> <https://groups.google.com/d/msgid/octomap/2e677be3-be43-46be-a4f6-67a4694649b1o%40googlegroups.com?utm_medium=email&utm_source=footer>>.
>
>
> --
> You received this message because you are subscribed to the Google
> Groups "OctoMap developers and users discussion" group.
> To unsubscribe from this group and stop receiving emails from it, send
> an email to octomap+u...@googlegroups.com
> <mailto:octomap+u...@googlegroups.com>.
> To view this discussion on the web visit
> https://groups.google.com/d/msgid/octomap/7fb62d3f-2edb-49e5-a6f6-71bd4aba8332o%40googlegroups.com
> <https://groups.google.com/d/msgid/octomap/7fb62d3f-2edb-49e5-a6f6-71bd4aba8332o%40googlegroups.com?utm_medium=email&utm_source=footer>.

Mayar Attawiya

unread,
Jul 4, 2020, 11:39:30 AM7/4/20
to OctoMap developers and users discussion
This has perfectly resolved the issue.
Thank you so much. I'm very grateful for your help.
Reply all
Reply to author
Forward
0 new messages