From 4f881b9e26131b2cd259337bf81c89104b349c46 Mon Sep 17 00:00:00 2001 From: Misa Date: Thu, 19 Aug 2021 16:36:31 -0700 Subject: [PATCH] Fix enemy movement types 10/12 causing memory leaks If spawned as a custom enemy (createentity entry 56), or spawned outside of the rooms they spawn in in the main game, they will repeatedly clone themselves every frame, which profusely leaks memory. In fact it quickly causes a crash in 2.2 and previous, but 2.3 fixes that crash, so it just keeps spawning enemies endlessly, which eventually lags the game, and eventually can out-of-memory your system (bad!). The problem is those movement types rely on entclass::setenemyroom() to change their `behave` to be 11 or 13. Else, the new entity created will still have `behave` 10 or 12, which will create ANOTHER entity in the same way, and so on, and so forth. So to fix this, just make it so if an enemy is still `behave` 10 or 12 by the end, then, just set it to -1. That way it'll stay still and won't cause any harm. I considered setting the `behave` to 11 or 13 respectively, but, that's probably going farther than just fixing a memory leak, and anyways, it's not that much useful for me as a custom level maker, and the entities spawned aren't really controllable. --- desktop_version/src/Entity.cpp | 11 +++++++++++ 1 file changed, 11 insertions(+) diff --git a/desktop_version/src/Entity.cpp b/desktop_version/src/Entity.cpp index 5ca6c9f9..a9cd394d 100644 --- a/desktop_version/src/Entity.cpp +++ b/desktop_version/src/Entity.cpp @@ -1204,6 +1204,15 @@ int entityclass::crewcolour( int t ) return 0; } +static void entityclonefix(entclass* entity) +{ + if (entity->behave == 10 || entity->behave == 12) + { + /* Fix memory leak */ + entity->behave = -1; + } +} + void entityclass::createentity(int xp, int yp, int t, int meta1, int meta2, int p1, int p2, int p3, int p4) { k = entities.size(); @@ -1324,6 +1333,7 @@ void entityclass::createentity(int xp, int yp, int t, int meta1, int meta2, int else { entity.setenemyroom(game.roomx, game.roomy); + entityclonefix(&entity); } break; case 2: //A moving platform @@ -2094,6 +2104,7 @@ void entityclass::createentity(int xp, int yp, int t, int meta1, int meta2, int entity.colour = 18; } + entityclonefix(&entity); break; }