Skip to content

Commit

Permalink
Avoid crashes when engine leaks canvas items and friends
Browse files Browse the repository at this point in the history
  • Loading branch information
YuriSizov committed Dec 20, 2023
1 parent 3ce73e5 commit 34ecfff
Show file tree
Hide file tree
Showing 5 changed files with 33 additions and 4 deletions.
24 changes: 24 additions & 0 deletions servers/rendering/renderer_canvas_cull.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -2165,6 +2165,30 @@ bool RendererCanvasCull::free(RID p_rid) {
return true;
}

template <class T>
void RendererCanvasCull::_free_rids(T &p_owner, const char *p_type) {
List<RID> owned;
p_owner.get_owned_list(&owned);
if (owned.size()) {
if (owned.size() == 1) {
WARN_PRINT(vformat("1 RID of type \"%s\" was leaked.", p_type));
} else {
WARN_PRINT(vformat("%d RIDs of type \"%s\" were leaked.", owned.size(), p_type));
}
for (const RID &E : owned) {
free(E);
}
}
}

void RendererCanvasCull::finalize() {
_free_rids(canvas_owner, "Canvas");
_free_rids(canvas_item_owner, "CanvasItem");
_free_rids(canvas_light_owner, "CanvasLight");
_free_rids(canvas_light_occluder_owner, "CanvasLightOccluder");
_free_rids(canvas_light_occluder_polygon_owner, "CanvasLightOccluderPolygon");
}

RendererCanvasCull::RendererCanvasCull() {
z_list = (RendererCanvasRender::Item **)memalloc(z_range * sizeof(RendererCanvasRender::Item *));
z_last_list = (RendererCanvasRender::Item **)memalloc(z_range * sizeof(RendererCanvasRender::Item *));
Expand Down
6 changes: 6 additions & 0 deletions servers/rendering/renderer_canvas_cull.h
Original file line number Diff line number Diff line change
Expand Up @@ -170,6 +170,9 @@ class RendererCanvasCull {
RID_Owner<Item, true> canvas_item_owner;
RID_Owner<RendererCanvasRender::Light, true> canvas_light_owner;

template <class T>
void _free_rids(T &p_owner, const char *p_type);

bool disable_scale;
bool sdf_used = false;
bool snapping_2d_transforms_to_pixel = false;
Expand Down Expand Up @@ -329,6 +332,9 @@ class RendererCanvasCull {
Rect2 _debug_canvas_item_get_rect(RID p_item);

bool free(RID p_rid);

void finalize();

RendererCanvasCull();
~RendererCanvasCull();
};
Expand Down
2 changes: 2 additions & 0 deletions servers/rendering/renderer_canvas_render.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -31,6 +31,8 @@
#include "renderer_canvas_render.h"
#include "servers/rendering/rendering_server_globals.h"

RendererCanvasRender *RendererCanvasRender::singleton = nullptr;

const Rect2 &RendererCanvasRender::Item::get_rect() const {
if (custom_rect || (!rect_dirty && !update_when_visible && skeleton == RID())) {
return rect;
Expand Down
4 changes: 0 additions & 4 deletions servers/rendering/renderer_compositor.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -31,8 +31,6 @@
#include "renderer_compositor.h"

#include "core/config/project_settings.h"
#include "core/os/os.h"
#include "core/string/print_string.h"
#include "servers/xr_server.h"

RendererCompositor *RendererCompositor::singleton = nullptr;
Expand All @@ -57,5 +55,3 @@ RendererCompositor::RendererCompositor() {
xr_enabled = XRServer::get_xr_mode() == XRServer::XRMODE_ON;
}
}

RendererCanvasRender *RendererCanvasRender::singleton = nullptr;
1 change: 1 addition & 0 deletions servers/rendering/rendering_server_default.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -214,6 +214,7 @@ void RenderingServerDefault::_finish() {
free(test_cube);
}

RSG::canvas->finalize();
RSG::rasterizer->finalize();
}

Expand Down

0 comments on commit 34ecfff

Please sign in to comment.