Do I use this structures correctly?

:information_source: Attention Topic was automatically imported from the old Question2Answer platform.
:bust_in_silhouette: Asked By Robotex
    void godot::NavigationArea::fill_points()
{
	u32 k = 0;
	godot::CircleShape2D *shape = godot::CircleShape2D::_new();
	fp32 radius = _max(horizontal_spacing, vertical_spacing);
	godot::Physics2DShapeQueryParameters* query = godot::Physics2DShapeQueryParameters::_new();
	shape->set_radius(radius);
	//query.set_exclude([])
	query->set_shape(shape);

	godot::Physics2DDirectSpaceState* space_state = get_world_2d()->get_direct_space_state();

	for (u32 i = 0; i < width; i+= horizontal_spacing) {
		for (u32 j = 0; j < height; j += vertical_spacing) {
			godot::Vector2 pos = godot::Vector2(i, j);
			query->set_transform(godot::Transform2D(0.f, pos));
			godot::Array res = space_state->intersect_shape(query);

			boolean obstaclesIntersected = false;
			if (!res.empty()) {
				for (u32 i = 0; i < res.size(); ++i) {
					godot::Dictionary intersection = res[i];
					godot::Node* collider = Object::cast_to<godot::Node>(intersection["collider"].operator Object * ());
					if (collider && collider->is_in_group("Obstacles")) {
						obstaclesIntersected = true;
						break;
					}
				}
			}

			if (!obstaclesIntersected) {
				godot::Dictionary point;
				point["position"] = pos;
				points[k] = point;
				k += 1;
			}
		}
	}

	query->free();
	shape->free();
}

I’m getting a crash and this errors:

ERROR: _intersect_shape: Condition "!p_shape_query.is_valid()" is true. Returned: Array()
   At: servers/physics_2d_server.cpp:295
:bust_in_silhouette: Reply From: Robotex

Ok, I used it wrong.

I should use Ref instead of raw pointers:

void godot::NavigationArea::fill_points()
{
  u32 k = 0;
  godot::Ref<godot::CircleShape2D> shape;
  shape.instance();
  fp32 radius = _max(horizontal_spacing, vertical_spacing);
  godot::Ref <godot::Physics2DShapeQueryParameters> query;
  query.instance();
  shape->set_radius(radius);
  //query.set_exclude([])
  query->set_shape(shape);

  godot::Physics2DDirectSpaceState* space_state = get_world_2d()->get_direct_space_state();

  for (u32 i = 0; i < width; i+= horizontal_spacing) {
    for (u32 j = 0; j < height; j += vertical_spacing) {
      godot::Vector2 pos = godot::Vector2(i, j);
      query->set_transform(godot::Transform2D(0.f, pos));
      godot::Array res = space_state->intersect_shape(query);

      boolean obstaclesIntersected = false;
      if (!res.empty()) {
        for (u32 i = 0; i < res.size(); ++i) {
          godot::Dictionary intersection = res[i];
          godot::Node* collider = Object::cast_to<godot::Node>(intersection["collider"].operator Object * ());
          if (collider && collider->is_in_group("Obstacles")) {
            obstaclesIntersected = true;
            break;
          }
        }
      }

      if (!obstaclesIntersected) {
        godot::Dictionary point;
        point["position"] = pos;
        points[k] = point;
        k += 1;
      }
    }
  }
}

Thanks to Reddit - Dive into anything