Reputation: 519
This is how my class looks. My code compiles successfully but when I run it, it crashes and stops.
class Explore{
public:
Explore(ros::NodeHandle &nh, tf2_ros::Buffer &buffer);
private:
bool is_frontier(size_t mx, size_t my);
void explore_level_three();
void go_to_cell(size_t mx, size_t my);
void publish_markers_array();
void publish_markers();
costmap_2d::Costmap2DROS* global_costmap, *local_costmap;
costmap_2d::Costmap2D* global_costmap_, *local_costmap_;
ros::NodeHandle nh_;
ros::Publisher frontier_array_pub, frontier_pub;
vector<pair<size_t, size_t> >frontiers;
string global_frame, robot_base_frame;
unsigned char *global_og;
size_t size_x, size_y;
double init_wx, init_wy;
int vis[4001][4001], frontier_vis[4001][4001] ;
};
When I change vis[4001][4001], frontier_vis[4001][4001];
to vis[500][500], frontier_vis[500][500];
the code runs successfully.
This is my main
function -
int main(int argc, char** argv) {
ros::init(argc, argv, "explore_node");
ros::NodeHandle nh("explore_node");
tf2_ros::Buffer buffer(ros::Duration(10));
tf2_ros::TransformListener tf(buffer);
Explore explore_one(nh, buffer);
return 0;
}
How can I solve this issue?
Upvotes: 1
Views: 228
Reputation: 7542
Most probably you are creating an object of Explore
on stack which would be giving a Segmentation Fault
. You should declare the object dynamically on heap
int main() {
Explore * obj = new Explore(...);
delete obj; //since obj is on the heap,
//you have to take care of delete. using smart pointers
//like std::unique_ptr can efficiently handle the lifetime
return 0;
}
instead of
int main() {
Explore obj(...);
return 0;
}
You are better off with std::vector
though.
Upvotes: 1
Reputation: 238311
How can I solve this issue?
The memory available for automatic objects is typically very limited. As such, you must allocate all huge objects dynamically. Otherwise your variables will likely consume all of the memory reserved for automatic objects, which results in ... stack overflow.
Having huge member variables would not be a problem if all instances of the class were allocated dynamically, but since that restriction is hard to enforce, it is usually best to simply not have huge member variables.
Your arrays vis
and frontier_vis
are huge. Simple solution: Use std::vector
.
Upvotes: 2