Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix core dump due to memory corruption #24

Merged
merged 2 commits into from
Jul 16, 2024
Merged

Conversation

a8jan
Copy link
Contributor

@a8jan a8jan commented Jul 12, 2024

I was getting quite often core dumps when starting emulator, e.g:

DBG: [Screen] Windowed mode: 1440x1300 -> viewport: 1440x1280
DBG: [Sound] Initialized device to 44100Hz/8bit with 1024B (2048B) buffer
malloc(): unaligned tcache chunk detected
Aborted (core dumped)

finding the cause with help of valgrind:

...
DBG: [Sound] Initialized device to 44100Hz/8bit with 1024B (2048B) buffer
==47796== Invalid write of size 8
==47796==    at 0x48527F7: memset (in /usr/libexec/valgrind/vgpreload_memcheck-amd64-linux.so)
==47796==    by 0x14D5AB: Joy4004482::Joy4004482(IifGPIO*, TSettings::SetJoystick*) (Joy4004482.cpp:36)
==47796==    by 0x12C414: TEmulator::SetComputerModel(bool, int, unsigned char*) (Emulator.cpp:1396)
==47796==    by 0x1290E4: TEmulator::ProcessSettings(unsigned char) (Emulator.cpp:372)
==47796==    by 0x11CA2F: main (GPMD85emu.cpp:106)
==47796==  Address 0x125d4310 is 0 bytes after a block of size 144 alloc'd
==47796==    at 0x4849013: operator new(unsigned long) (in /usr/libexec/valgrind/vgpreload_memcheck-amd64-linux.so)
==47796==    by 0x12C3F0: TEmulator::SetComputerModel(bool, int, unsigned char*) (Emulator.cpp:1396)
==47796==    by 0x1290E4: TEmulator::ProcessSettings(unsigned char) (Emulator.cpp:372)
==47796==    by 0x11CA2F: main (GPMD85emu.cpp:106)
==47796== 
==47796== Invalid write of size 8
...

To fix it, I changed the way the joy[2] was initialized in Joy4004482 constructor.

-jan

@mborik
Copy link
Owner

mborik commented Jul 13, 2024

Amazing catch @a8jan
I'm getting faults too, but I won't be able to trace it properly on my M1 Mac.
Now I'm on vacation, so I can't test it.

@@ -19,18 +19,16 @@
#include <stdexcept>
#include "Joy4004482.h"
//---------------------------------------------------------------------------
Joy4004482::Joy4004482(IifGPIO *pio, TSettings::SetJoystick* settings)
Joy4004482::Joy4004482(IifGPIO *pio, TSettings::SetJoystick* settings) :
joy{{ nullptr, nullptr, false, 0 }, { nullptr, nullptr, false, 0 }},
Copy link
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Q: What's difference between nullptr and NULL?

Copy link
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

oh, I see, it's new C++11 keyword. But I think that we could put NULL macro here a update globals.h to work with nullptr (I'll change it when I come back home).

Comment on lines 30 to 31
this->pio = pio;
this->settings = settings;
Copy link
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Do you think, that we could set pio and settings same way?

So that throw (plus include) we could omit...? 🤔

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

yes, I think we can reduce the constructor code to just initializer

Joy4004482::Joy4004482(IifGPIO *pio, TSettings::SetJoystick* settings) :
	pio(pio),
	settings(settings),
	joy{{ NULL, NULL, false, 0 }, { NULL, NULL, false, 0 }},
	joyCnt(0),
	sameDev(false)
{
}

@mborik mborik linked an issue Jul 13, 2024 that may be closed by this pull request
@mborik
Copy link
Owner

mborik commented Jul 13, 2024

Confirmed that it works: #25 (comment)

@mborik mborik merged commit 1289c09 into mborik:master Jul 16, 2024
1 check passed
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

Frequent crashes on start (or shortly after)
2 participants