Opened 2 months ago

Last modified 6 weeks ago

#17188 new defect

The Dig (Macintosh) crashes after R2D2 scream on Lucasarts animation logo after the ending credits

Reported by: filipetolhuizen Owned by:
Priority: high Component: Engine: SCUMM
Version: Keywords: The Dig Mac Macintosh
Cc: Game: The Dig

Description

After the ending credits roll, the Lucasarts logo animation plays and right after it ends with the R2D2 scream, ScummVM crashes with a faulty msvcrt.dll module message. OS is Windows 7 64 bits and SCUMMVM port is Win64, latest daily build 2026.3.1. I'm also providing a save game.

Attachments (1)

dig-mac.s00​ (41.0 KB ) - added by filipetolhuizen 2 months ago.

Download all attachments as: .zip

Change History (12)

by filipetolhuizen, 2 months ago

Attachment: dig-mac.s00​ added

comment:1 by eriktorbjorn, 2 months ago

I can reproduce it, though it's probably not unique to the Macintosh version. It appears to be a fairly straightforward "use-after-free" bug, where an object is freed but there's still a variable pointing to the memory area where it used to be.

I think this is the proper fix:

diff --git a/engines/scumm/scumm.cpp b/engines/scumm/scumm.cpp
index d816ca77d5f..8771959b194 100644
--- a/engines/scumm/scumm.cpp
+++ b/engines/scumm/scumm.cpp
@@ -477,6 +477,7 @@ ScummEngine::ScummEngine(OSystem *syst, const DetectorResult &dr)

 ScummEngine::~ScummEngine() {
        delete _musicEngine;
+       _musicEngine = _imuseDigital = nullptr;

        // Delete the sound object earlier than the actors
        // for HE games, since in SoundHE::stopAllSounds() we

Though I should ask someone else to look before committing it. (Which I can't easily do from this computer anyway.)

comment:2 by fusefib, 2 months ago

Might be a problem with poverty builds if you don't guard that with ENABLE_SCUMM_7_8 or, better, split it as two assignments?

comment:3 by eriktorbjorn, 2 months ago

_imuseDigital is available even if ENABLE_SCUMM_7_8 isn't defined, as far as I can see?

I've tried investigating a bit further why it crashes. It only seems to happen if "Ask for confirmation on exit" is enabled. The SCUMM engine would usually show a SCUMM style or Mac style dialog, but apparently it's gotten far enough tearing down the engine that neither of these are available. So it goes to a ScummVM style dialog instead.

Which is pretty silly. We shouldn't present a dialog where the answer makes no difference.

And all this happens in the Engine destructor. Is it even safe for it to call things in ScummEngine after its destructor has run? Are there any language lawyers in the building?

Valgrind log:

==74309== Invalid read of size 1
==74309==    at 0x717A286: Scumm::IMuseDigital::isEngineDisabled() (dimuse_engine.cpp:569)
==74309==    by 0x6FAD284: Scumm::ScummDebugger::onFrame() (debugger.cpp:131)
==74309==    by 0xC32D3D4: SurfaceSdlGraphicsManager::internUpdateScreen() (surfacesdl-graphics.cpp:1214)
==74309==    by 0xC32D31E: SurfaceSdlGraphicsManager::updateScreen() (surfacesdl-graphics.cpp:1199)
==74309==    by 0xC2A1A81: ModularGraphicsBackend::updateScreen() (modular-backend.cpp:198)
==74309==    by 0xC1E6E2C: GUI::GuiManager::redrawFull() (gui-manager.cpp:371)
==74309==    by 0xC1E8758: GUI::GuiManager::screenChange() (gui-manager.cpp:905)
==74309==    by 0xC1E869E: GUI::GuiManager::checkScreenChange() (gui-manager.cpp:877)
==74309==    by 0xC1D9EE0: GUI::Dialog::Dialog(int, int, int, int, bool) (dialog.cpp:40)
==74309==    by 0xC2016DA: GUI::MessageDialog::MessageDialog(Common::U32String const&, Common::U32String const&, Common::U32String const&, Graphics::TextAlign, char const*, Common::U32String const&) (message.cpp:169)
==74309==    by 0xC2A500B: DefaultEventManager::pollEvent(Common::Event&) (default-events.cpp:231)
==74309==    by 0xC1BC37C: Engine::~Engine() (engine.cpp:223)
==74309==  Address 0x3395ceb5 is 85 bytes inside a block of size 100,680 free'd
==74309==    at 0xF4415D4: operator delete(void*) (vg_replace_malloc.c:1096)
==74309==    by 0x7178D19: Scumm::IMuseDigital::~IMuseDigital() (dimuse_engine.cpp:171)
==74309==    by 0x7122C95: Scumm::ScummEngine::~ScummEngine() (scumm.cpp:479)
==74309==    by 0x7137C03: Scumm::ScummEngine_v6::~ScummEngine_v6() (scumm_v6.h:29)
==74309==    by 0x712546A: Scumm::ScummEngine_v7::~ScummEngine_v7() (scumm.cpp:984)
==74309==    by 0x7125495: Scumm::ScummEngine_v7::~ScummEngine_v7() (scumm.cpp:984)
==74309==    by 0xC1C68D1: MetaEngine::deleteInstance(Engine*, DetectedGame const&, void const*) (metaengine.cpp:368)
==74309==    by 0x6F5369C: runGame(Plugin const*, OSystem&, DetectedGame const&, void const*) (main.cpp:341)
==74309==    by 0x6F55AA7: scummvm_main (main.cpp:840)
==74309==    by 0x6F5041C: main (posix-main.cpp:56)
==74309==  Block was alloc'd at
==74309==    at 0xF43DF53: operator new(unsigned long) (vg_replace_malloc.c:501)
==74309==    by 0x712A87E: Scumm::ScummEngine_v7::setupScumm(Common::Path const&) (scumm.cpp:1943)
==74309==    by 0x71280AE: Scumm::ScummEngine::init() (scumm.cpp:1534)
==74309==    by 0x6F8987F: Scumm::ScummEngine::run() (scumm.h:590)
==74309==    by 0x6F535A4: runGame(Plugin const*, OSystem&, DetectedGame const&, void const*) (main.cpp:328)
==74309==    by 0x6F55AA7: scummvm_main (main.cpp:840)
==74309==    by 0x6F5041C: main (posix-main.cpp:56)
==74309== 
==74309== Invalid read of size 8
==74309==    at 0xC1C6A08: MetaEngine::listSaves(char const*) const (metaengine.cpp:386)
==74309==    by 0x6FAD2B3: Scumm::ScummDebugger::onFrame() (debugger.cpp:131)
==74309==    by 0xC32D3D4: SurfaceSdlGraphicsManager::internUpdateScreen() (surfacesdl-graphics.cpp:1214)
==74309==    by 0xC32D31E: SurfaceSdlGraphicsManager::updateScreen() (surfacesdl-graphics.cpp:1199)
==74309==    by 0xC2A1A81: ModularGraphicsBackend::updateScreen() (modular-backend.cpp:198)
==74309==    by 0xC1E6E2C: GUI::GuiManager::redrawFull() (gui-manager.cpp:371)
==74309==    by 0xC1E8758: GUI::GuiManager::screenChange() (gui-manager.cpp:905)
==74309==    by 0xC1E869E: GUI::GuiManager::checkScreenChange() (gui-manager.cpp:877)
==74309==    by 0xC1D9EE0: GUI::Dialog::Dialog(int, int, int, int, bool) (dialog.cpp:40)
==74309==    by 0xC2016DA: GUI::MessageDialog::MessageDialog(Common::U32String const&, Common::U32String const&, Common::U32String const&, Graphics::TextAlign, char const*, Common::U32String const&) (message.cpp:169)
==74309==    by 0xC2A500B: DefaultEventManager::pollEvent(Common::Event&) (default-events.cpp:231)
==74309==    by 0xC1BC37C: Engine::~Engine() (engine.cpp:223)
==74309==  Address 0x0 is not stack'd, malloc'd or (recently) free'd
==74309== 
==74309== 
==74309== Process terminating with default action of signal 11 (SIGSEGV): dumping core
==74309==  Access not within mapped region at address 0x0
==74309==    at 0xC1C6A08: MetaEngine::listSaves(char const*) const (metaengine.cpp:386)
==74309==    by 0x6FAD2B3: Scumm::ScummDebugger::onFrame() (debugger.cpp:131)
==74309==    by 0xC32D3D4: SurfaceSdlGraphicsManager::internUpdateScreen() (surfacesdl-graphics.cpp:1214)
==74309==    by 0xC32D31E: SurfaceSdlGraphicsManager::updateScreen() (surfacesdl-graphics.cpp:1199)
==74309==    by 0xC2A1A81: ModularGraphicsBackend::updateScreen() (modular-backend.cpp:198)
==74309==    by 0xC1E6E2C: GUI::GuiManager::redrawFull() (gui-manager.cpp:371)
==74309==    by 0xC1E8758: GUI::GuiManager::screenChange() (gui-manager.cpp:905)
==74309==    by 0xC1E869E: GUI::GuiManager::checkScreenChange() (gui-manager.cpp:877)
==74309==    by 0xC1D9EE0: GUI::Dialog::Dialog(int, int, int, int, bool) (dialog.cpp:40)
==74309==    by 0xC2016DA: GUI::MessageDialog::MessageDialog(Common::U32String const&, Common::U32String const&, Common::U32String const&, Graphics::TextAlign, char const*, Common::U32String const&) (message.cpp:169)
==74309==    by 0xC2A500B: DefaultEventManager::pollEvent(Common::Event&) (default-events.cpp:231)
==74309==    by 0xC1BC37C: Engine::~Engine() (engine.cpp:223)

comment:4 by eriktorbjorn, 8 weeks ago

From what I understand, this is the situation:

  • ScummEngine is derived from Engine. That means the Engine destructor runs after the ScummEngine destructor.
  • Once the ScummEngine destructor is finished, that object is dead.
  • When the Engine destructor triggers a callback to the ScummEngine instance, that's undefined behavior. It happens to work, and Valgrind doesn't notice. But it should not do that.
  • The callback happens through the debugger object.

So maybe deleting the debugger early is the proper fix? Hopefully that will fix similar issues in other engines as well.

diff --git a/engines/engine.cpp b/engines/engine.cpp
index cc7dbf5f0f0..e1a60ff1362 100644
--- a/engines/engine.cpp
+++ b/engines/engine.cpp
@@ -216,13 +216,17 @@ Engine::Engine(OSystem *syst)
 }

 Engine::~Engine() {
+       // Delete the debugger early. We don't want it to make any callbacks
+       // into the game engine after its destructor has run.
+       delete _debugger;
+       _debugger = nullptr;
+
        _mixer->stopAll();

        // Flush any pending remaining events
        Common::Event evt;
        while (g_system->getEventManager()->pollEvent(evt)) {}

-       delete _debugger;
        delete _mainMenuDialog;
        g_engine = NULL;

I still wish it wouldn't show an exit confirmation dialog in these cases, but at the moment the same option controls both the engine's own confirmation dialogs (which I do want), and ScummVM's (which I could do without).

comment:5 by fusefib, 8 weeks ago

What I meant:

  • It resolves to _musicEngine = (_imuseDigital = nullptr) and may require an IMuseDigital * -> MusicEngine * conversion, even though the value is null.
  • Definition establishing that IMuseDigital inherits from MusicEngine seems version guarded, see scumm/imuse_digi/dimuse_engine.h:22 and :68.
  • Two separate assignments avoid making compilers sad in case of skinny SCUMM builds. Just something to test before committing directly: if compiler doesn't complain, then non-issue so nevermind.

On the new fix: I wonder what happens if you press ctrl+alt+d during the useless exit confirmation. Or maybe the punishment is deserved? :)

comment:6 by eriktorbjorn, 8 weeks ago

Well, if the second fix is correct the first one isn't needed any more. :-)

I wonder what happens if you press ctrl+alt+d during the useless exit confirmation.

Surprisingly (to me), it opened a debug console. A more bare-bones version, with only the clear, cls, debugflag_disable, debugflag_enable, debugflag_list, debuglevel, exec, exit, help, md5, md5mac, openlog, and quit commands.

comment:7 by fusefib, 8 weeks ago

1) Why not both fixes?

2) Well, yup, with the early debugger deletion alone, there might be another silly edge cleanup problem?

  • Delete the original debugger.
  • Set _debugger to nullptr.
  • Process events -> ctrl+alt+d -> allocate a new generic GUI::Debugger during the Quit confirmation.
  • Finish destroying Engine, without another delete _debugger.

It might be better to move the existing g_engine reset before the event flush instead:

Engine::~Engine() {
	_mixer->stopAll();

	// The game-specific engine has already been torn down.
	// Stop exposing it before processing any remaining events.
	g_engine = nullptr;

	// Flush any pending remaining events
	Common::Event evt;
	while (g_system->getEventManager()->pollEvent(evt)) {}

	delete _debugger;
	delete _mainMenuDialog;

	// Remove our cursors again to prevent memory leaks
	CursorMan.popCursor();
	CursorMan.popCursorPalette();
}

...but I'm generally not sure what it might upset in various engines or the quit/return-to-launcher paths. I'll get back to you in a couple of years.

Okay, I just wanted to quickly warn about the first thing, so I'll shut up now. ;)

comment:8 by eriktorbjorn, 8 weeks ago

That does fix the crash, and it also doesn't pop up a quit confirmation dialog at the end, so yours seems like the best fix so far.

comment:9 by criezy, 6 weeks ago

In 87571811:

ENGINES: Purge input events after deleting the engine

This reverts commit 27ee364 that purged the events in the engine destructor
by polling events until the event queue is empty, but caused in some cases
the quit confirmation dialog to be shown during engine destruction, and a
crash. See bug #17188.

Instead we purge imputs events after the engine deletion using the dedicated
functions. We should maybe purge all events, as done before this commit, but
it would be better to have a dedicated function for this rather than poll
the events.

comment:10 by criezy, 6 weeks ago

I pushed a first fix to remove the cause of the crash. This change however has the potential to cause the game to quit scummvm (or rather to return to the launcher and then immediately quit scummvm) when we have both options to return to the launcher and to ask for confirmation. I have additional changes that I need to test before they are pushed.

comment:11 by fusefib, 6 weeks ago

If I Did It: Confessions of the Engine Killer
​https://github.com/scummvm/scummvm/compare/master...fusefib:scummvm:events-fix2
I don't want to jinx or delay things, but because this may not be going the Pull Request route (not something I'm requesting), I might as well drop this draft from a while back in case parts are useful. It's the control-maxxing and procedure route, however.

Also - not that anyone cares, but CTRL+Q and cancel isn't handled too great in SWORD1 (or the demo). Fixed in branch above as a drive-by.

Note: See TracTickets for help on using tickets.