Skip to content

Commit 07f9d78

Browse files
committed
Fixed a rare crash
1 parent a0d0538 commit 07f9d78

File tree

4 files changed

+27
-7
lines changed

4 files changed

+27
-7
lines changed

CHANGELOG.md

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -38,7 +38,7 @@ The format is based on [Keep a Changelog](https://keepachangelog.com/en/1.0.0/),
3838

3939
- Fixed an issue that could cause post-effects to appear very blurry.
4040

41-
- Fixed a rare crash that could occur depending on Lua garbage collection and async processing.
41+
- Fixed a couple of rare crashes that could occur depending on Lua garbage collection and async processing.
4242

4343
- Fixed a crash that could occur when using the Nucleo Swarm weapon.
4444

Source/Main.cpp

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -323,7 +323,6 @@ void RunGameLoop() {
323323
g_LuaMan.Update();
324324

325325
g_UInputMan.Update();
326-
g_ConsoleMan.Update();
327326

328327
// It is vital that server is updated after input manager but before activity because input manager will clear received pressed and released events on next update.
329328
if (g_NetworkServer.IsServerModeEnabled()) {
@@ -333,6 +332,9 @@ void RunGameLoop() {
333332

334333
g_FrameMan.Update();
335334

335+
g_MovableMan.CompleteQueuedMOIDDrawings();
336+
337+
g_ConsoleMan.Update();
336338
g_ActivityMan.Update();
337339

338340
if (g_SceneMan.GetScene()) {

Source/Managers/MovableMan.cpp

Lines changed: 20 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -123,7 +123,20 @@ void MovableMan::Destroy() {
123123

124124
MovableObject* MovableMan::GetMOFromID(MOID whichID) {
125125
if (whichID != g_NoMOID && whichID != 0 && whichID < m_MOIDIndex.size()) {
126-
return m_MOIDIndex[whichID];
126+
// This is really, really awful
127+
// But, Lua scripts can take ownership of an MO which exists in this list
128+
// And then the MO can be deallocated by Lua GC
129+
// Meaning that this ptr points to stale memory.
130+
// Due to our pooled memory system, we can avoid a crash by just... checking that the MO isn't NoMOID
131+
// But this is still atrociously awful and terrible and this can point to a newly-allocated object that just so happens to be allocated the same place.
132+
// Anyways, until we can fix the god-awful abomination that is this game's memory ownership semantics, we're stuck with this
133+
// Which is also technically undefined behaviour
134+
MovableObject* candidate = m_MOIDIndex[whichID];
135+
if (candidate->GetID() != whichID) {
136+
return nullptr;
137+
}
138+
139+
return candidate;
127140
}
128141
return nullptr;
129142
}
@@ -1654,10 +1667,6 @@ void MovableMan::Update() {
16541667
void MovableMan::Travel() {
16551668
ZoneScoped;
16561669

1657-
if (m_DrawMOIDsTask.valid()) {
1658-
m_DrawMOIDsTask.wait();
1659-
}
1660-
16611670
// Travel Actors
16621671
{
16631672
ZoneScopedN("Actors Travel");
@@ -1857,6 +1866,12 @@ void MovableMan::UpdateDrawMOIDs() {
18571866
}
18581867
}
18591868

1869+
void MovableMan::CompleteQueuedMOIDDrawings() {
1870+
if (m_DrawMOIDsTask.valid()) {
1871+
m_DrawMOIDsTask.wait();
1872+
}
1873+
}
1874+
18601875
void MovableMan::Draw(BITMAP* pTargetBitmap, const Vector& targetPos) {
18611876
ZoneScoped;
18621877

Source/Managers/MovableMan.h

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -456,6 +456,9 @@ namespace RTE {
456456
/// Updates the MOIDs of all current MOs.
457457
void UpdateDrawMOIDs();
458458

459+
// Forces MOID drawing to complete (should be done before any physics sim or collision detection etc)
460+
void CompleteQueuedMOIDDrawings();
461+
459462
/// Draws this MovableMan's current graphical representation to a BITMAP of choice.
460463
/// @param pTargetBitmap A pointer to a BITMAP to draw on.
461464
/// @param targetPos The absolute position of the target bitmap's upper left corner in the scene. (default: Vector())

0 commit comments

Comments
 (0)