Skip to content
This repository has been archived by the owner on Oct 30, 2019. It is now read-only.

Leaking MediaPlayer causes Win64 crashes #97

Closed
xanather opened this issue Jun 8, 2013 · 21 comments
Closed

Leaking MediaPlayer causes Win64 crashes #97

xanather opened this issue Jun 8, 2013 · 21 comments

Comments

@xanather
Copy link

xanather commented Jun 8, 2013

Ok so I recently just redowloaded the repo and the memory leaking error is back:

A callback was made on a garbage collected delegate of type 'SDL2#!SDL2.SDL_mixer+MusicFinishedDelegate::Invoke'. This may cause application crashes, corruption and data loss. When passing delegates to unmanaged code, they must be kept alive by the managed application until it is guaranteed that they will never be called.

When I was testing the MediaPlayer changes before I had my SDL_GamePlatform Exit method as follows.

    public override void Exit()
    {
        // Stop the game loop
        INTERNAL_window.INTERNAL_StopLoop();

        // End the network subsystem
        Net.NetworkSession.Exit();

        // Stop the Media Player
        Media.MediaPlayer.Stop();

        // Close SDL2_mixer
        SDL_mixer.Mix_CloseAudio();
    }

If the Media.MediaPlayer.Stop(); is not there in the Exit (like it is now) method I receive the "passing delegate" error when calling Game.Exit() just like before. Ill think submit a PR for that in a moment.

I also submitted this issue for two other reasons as well.

When terminating the process through visual studio I get the same "passing delegate" message, how can a process termination result in a error/warning?

Finally (another MediaPlayer issue hehe), when a playing song sucessfully ends I get a untraceable NullReferenceException.

@flibitijibibo
Copy link
Owner

My guess is VS only attempts to kill the main thread, and the other ones just die on their own (well, unmanaged threads probably). You're essentially killing the process without closing the mixer, so this is expected.

That NullReferenceException report is stupidly useless without a trace.

Are you absolutely sure that XNA4 is okay with your code? The more issues I hear on this, the more I suspect that WMP would leak with your engine.

@flibitijibibo
Copy link
Owner

How well does this project work on OSX/Linux? At least if it happens on other platforms it would verify that it's a generic lib problem rather than something Win32 needs to deal with.

@xanather
Copy link
Author

xanather commented Jun 9, 2013

Ok so last night I thought as to why I am getting all these odd disposing issues, I am using 64-bit binaries if that changes anything? I am also using the preview build of sdl2_mixer.dll (I thought we were supposed to use that right?). Ill switch to 32-bit right now. I'd also like to point out that the only other MediaPlayer things I am touching other than MediaPlayer.Play() is MediaPlayer.Volume.

@flibitijibibo
Copy link
Owner

Strange, I've actually not looked at anything 64-bit on Windows. I know 64-bit Linux is okay, and OSX is still limited to 32-bit... no idea how well .NET 64-bit works. The only MG-SDL2 title I've got where Windows is involved is Fez, and that one's currently 32-bit only.

As long as you're using consistent architectures it should be okay, but for .NET (or just Win32 in general) you may just want to stick with 32-bit bins.

@xanather
Copy link
Author

xanather commented Jun 9, 2013

Ok, I was right. No problems with 32-bit binaries. Thats a bumber though, one of the reasons I'm not using just original XNA4 on PC is so I can have access to 64-bit (and .net 4.5.). What differences could there be between 32-bit and 64-bit, I mean there are x64 sdl2_mixer libraries and associated files?

@flibitijibibo
Copy link
Owner

To tell you the truth, I'm not sure. Particularly for Windows. Considering it works with 64-bit Linux, I'm wondering if something else is going on in there. This is where a background in low-level programming/debugging comes in handy, and Windows is my weakest platform.

Maybe it's how the .NET assemblies are being built? I know Windows cares a lot more about the architecture target than Mac/Linux do (I just build my games as x86 and it's fine on both). Maybe SDL2# needs an explicit 64-bit target to deal with Windows? 🤷

Also keep in mind that a lot of those features are not available on other platforms (Mac especially), so you'll probably want to avoid the bleeding edge when it comes to the .NET features (and architecture targets) you use.

@xanather
Copy link
Author

xanather commented Jun 9, 2013

SDL 64-bit I think works alright on Windows, the only problem it has is the several odd MediaPlayer issues. Maybe these won't be issues anymore once someone makes the vorbis port?

@flibitijibibo
Copy link
Owner

That would definitely resolve it, but I suppose it's still concerning that Win64 exhibits this problem with SDL2_mixer.

I don't see anything in the SDL2 Bugzilla or the Hg log that suggests any problem is known, so either there's something wrong with my SDL2_mixer interop or SDL2_mixer Win64 has an issue of its own. If you can, see if upstream Hg SDL2_mixer fixes it. For all I know, the binary could just be a dud.

Also, try tracing all of the Song method calls... for all I know there's a call being done out of order that SDL2_mixer can't handle, though I try to limit mixer calls overall (grumble, threads).

@xanather
Copy link
Author

xanather commented Jun 9, 2013

Yep, will do. Also what happens on x64 MacOSX, is it similar problems? Maybe you should create a issue for that as well?

@flibitijibibo
Copy link
Owner

OSX doesn't even have a 64-bit Mono runtime yet. :P
On Jun 9, 2013 1:24 AM, "Jordan S" notifications@github.com wrote:

Yep, will do. Also what happens on x64 MacOSX, is it similar problems?
Maybe you should create a issue for that as well?


Reply to this email directly or view it on GitHubhttps://github.com//issues/97#issuecomment-19161138
.

@xanather
Copy link
Author

xanather commented Jun 9, 2013

Haha, I see.

@xanather
Copy link
Author

xanather commented Jun 9, 2013

Okay well I switched to 32-bit for the time being, more bad news but, even though the end-of-song NullReferenceException is gone the "leak" warning is still there when I call Game.Exit(). Is fez on windows using .ogg files for sounds? Just like before on 64-bit, #98 fixes this, but I don't want you to accept that PR if you feel it is unnecessary. This is the best stack trace I could get:

[External Code] 

MonoGame.Framework.dll!Microsoft.Xna.Framework.Media.Song.closeMixer() Line 71 + 0x5 bytes C#
MonoGame.Framework.dll!Microsoft.Xna.Framework.SDL2_GamePlatform.Exit() Line 154 + 0x5 bytes C#
MonoGame.Framework.dll!Microsoft.Xna.Framework.Game.Exit() Line 376 + 0xe bytes C#
hidden.exe!hidden.Interface.Menu.MenuWindow.Update() Line 42 C#
hidden.exe!hidden.Interface.UIManager.Update() Line 81 + 0x44 bytes C#
hidden.exe!hidden.Menu.Update() Line 69 C#
hidden.exe!hidden.Engine.Update(Microsoft.Xna.Framework.GameTime gameTime) Line 128 C#
MonoGame.Framework.dll!Microsoft.Xna.Framework.Game.DoUpdate(Microsoft.Xna.Framework.GameTime gameTime) Line 697 + 0xe bytes C#
MonoGame.Framework.dll!Microsoft.Xna.Framework.Game.Tick() Line 505 + 0xe bytes C#
MonoGame.Framework.dll!Microsoft.Xna.Framework.SDL2_GameWindow.INTERNAL_RunLoop() Line 404 + 0xd bytes C#
MonoGame.Framework.dll!Microsoft.Xna.Framework.SDL2_GamePlatform.RunLoop() Line 135 + 0xd bytes C#
MonoGame.Framework.dll!Microsoft.Xna.Framework.Game.Run(Microsoft.Xna.Framework.GameRunBehavior runBehavior) Line 437 + 0xe bytes C#
MonoGame.Framework.dll!Microsoft.Xna.Framework.Game.Run() Line 415 + 0x22 bytes C#
hidden.exe!hidden.Program.Main() Line 16 + 0xa bytes C#
[External Code]

My own global exception handler for the game writes this down as a NullReferenceException in the logs as:

Unhandled exception at: 9/06/2013 3:54:02 PM, on thread: Main Thread. Fatal: True
System.NullReferenceException: Object reference not set to an instance of an object.
at SDL2.SDL_mixer.Mix_CloseAudio()
at Microsoft.Xna.Framework.Media.Song.closeMixer() in c:\Users\Xanather\Documents\Programming\Libs\MonoGame\MonoGame.Framework\SDL2\Media\Song.cs:line 71
at Microsoft.Xna.Framework.SDL2_GamePlatform.Exit() in c:\Users\Xanather\Documents\Programming\Libs\MonoGame\MonoGame.Framework\SDL2\SDL2_GamePlatform.cs:line 154
at Microsoft.Xna.Framework.Game.Exit() in c:\Users\Xanather\Documents\Programming\Libs\MonoGame\MonoGame.Framework\Game.cs:line 376
at hidden.Interface.Menu.MenuWindow.Update() in c:\Users\Xanather\Documents\Programming\Main\hiddenTest\hidden\Interface\Menu\MenuWindow.cs:line 40
at hidden.Interface.UIManager.Update() in c:\Users\Xanather\Documents\Programming\Main\hiddenTest\hidden\Interface\UIManager.cs:line 83
at hidden.Menu.Update() in c:\Users\Xanather\Documents\Programming\Main\hiddenTest\hidden\Menu.cs:line 68
at hidden.Engine.Update(GameTime gameTime) in c:\Users\Xanather\Documents\Programming\Main\hiddenTest\hidden\Engine.cs:line 127
at Microsoft.Xna.Framework.Game.DoUpdate(GameTime gameTime) in c:\Users\Xanather\Documents\Programming\Libs\MonoGame\MonoGame.Framework\Game.cs:line 697
at Microsoft.Xna.Framework.Game.Tick() in c:\Users\Xanather\Documents\Programming\Libs\MonoGame\MonoGame.Framework\Game.cs:line 505
at Microsoft.Xna.Framework.SDL2_GameWindow.INTERNAL_RunLoop() in c:\Users\Xanather\Documents\Programming\Libs\MonoGame\MonoGame.Framework\SDL2\SDL2_GameWindow.cs:line 404
at Microsoft.Xna.Framework.SDL2_GamePlatform.RunLoop() in c:\Users\Xanather\Documents\Programming\Libs\MonoGame\MonoGame.Framework\SDL2\SDL2_GamePlatform.cs:line 135
at Microsoft.Xna.Framework.Game.Run(GameRunBehavior runBehavior) in c:\Users\Xanather\Documents\Programming\Libs\MonoGame\MonoGame.Framework\Game.cs:line 437
at Microsoft.Xna.Framework.Game.Run() in c:\Users\Xanather\Documents\Programming\Libs\MonoGame\MonoGame.Framework\Game.cs:line 415
at hidden.Program.Main() in c:\Users\Xanather\Documents\Programming\Main\hiddenTest\hidden\Program.cs:line 16

I am so lost... I'm doing nothing significant with MediaPlayer and if Fez reports no problems with song on Windows then what?

This error does not happen if I were to close the game quickly after it started (within the first 10 seconds), after woods its doomed and always results in this.

@flibitijibibo
Copy link
Owner

Wow, that's actually an interesting trace. It suggests that Song.closeMixer
isn't entirely static, which it obviously is... I dunno if I could explain
that. I think if we could find the explanation and the MediaPlayer.Stop
call really fixes it I'd be down, but I can't really tell if there's a
deeper problem we should be looking at.
On Jun 9, 2013 4:00 AM, "Jordan S" notifications@github.com wrote:

Okay well I switched to 32-bit for the time being, more bad news but, even
though the end-of-song NullReferenceException is gone the "leak" warning is
still there when I call Game.Exit(). Is fez on windows using .ogg files for
sounds? Just like before on 64-bit, #98https://github.com/flibitijibibo/MonoGame/issues/98fixes this, but I don't want you to accept that PR if you feel it is
unnecessary. This is the best stack trace I could get:

[External Code]

MonoGame.Framework.dll!Microsoft.Xna.Framework.Media.Song.closeMixer()
Line 71 + 0x5 bytes C#
MonoGame.Framework.dll!Microsoft.Xna.Framework.SDL2_GamePlatform.Exit()
Line 154 + 0x5 bytes C#
MonoGame.Framework.dll!Microsoft.Xna.Framework.Game.Exit() Line 376 + 0xe
bytes C#
hidden.exe!hidden.Interface.Menu.MenuWindow.Update() Line 42 C#
hidden.exe!hidden.Interface.UIManager.Update() Line 81 + 0x44 bytes C#
hidden.exe!hidden.Menu.Update() Line 69 C#
hidden.exe!hidden.Engine.Update(Microsoft.Xna.Framework.GameTime gameTime)
Line 128 C#
MonoGame.Framework.dll!Microsoft.Xna.Framework.Game.DoUpdate(Microsoft.Xna.Framework.GameTime
gameTime) Line 697 + 0xe bytes C#
MonoGame.Framework.dll!Microsoft.Xna.Framework.Game.Tick() Line 505 + 0xe
bytes C#
MonoGame.Framework.dll!Microsoft.Xna.Framework.SDL2_GameWindow.INTERNAL_RunLoop()
Line 404 + 0xd bytes C#
MonoGame.Framework.dll!Microsoft.Xna.Framework.SDL2_GamePlatform.RunLoop()
Line 135 + 0xd bytes C#
MonoGame.Framework.dll!Microsoft.Xna.Framework.Game.Run(Microsoft.Xna.Framework.GameRunBehavior
runBehavior) Line 437 + 0xe bytes C#
MonoGame.Framework.dll!Microsoft.Xna.Framework.Game.Run() Line 415 + 0x22
bytes C#
hidden.exe!hidden.Program.Main() Line 16 + 0xa bytes C#
[External Code]

My own global exception handler for the game writes this down as a
NullReferenceException in the logs as:
Unhandled exception at: 9/06/2013 3:54:02 PM, on thread: Main Thread.
Fatal: True

System.NullReferenceException: Object reference not set to an instance of
an object.
at SDL2.SDL_mixer.Mix_CloseAudio()
at Microsoft.Xna.Framework.Media.Song.closeMixer() in
c:\Users\Xanather\Documents\Programming\Libs\MonoGame\MonoGame.Framework\SDL2\Media\Song.cs:line
71
at Microsoft.Xna.Framework.SDL2_GamePlatform.Exit() in
c:\Users\Xanather\Documents\Programming\Libs\MonoGame\MonoGame.Framework\SDL2\SDL2_GamePlatform.cs:line
154
at Microsoft.Xna.Framework.Game.Exit() in
c:\Users\Xanather\Documents\Programming\Libs\MonoGame\MonoGame.Framework\Game.cs:line
376
at Xancraft.Interface.Menu.MenuWindow.Update() in
c:\Users\Xanather\Documents\Programming\Main\XancraftTest\Xancraft\Interface\Menu\MenuWindow.cs:line
40
at Xancraft.Interface.UIManager.Update() in
c:\Users\Xanather\Documents\Programming\Main\XancraftTest\Xancraft\Interface\UIManager.cs:line
83
at Xancraft.Menu.Update() in
c:\Users\Xanather\Documents\Programming\Main\XancraftTest\Xancraft\Menu.cs:line
68
at Xancraft.Engine.Update(GameTime gameTime) in
c:\Users\Xanather\Documents\Programming\Main\XancraftTest\Xancraft\Engine.cs:line
127
at Microsoft.Xna.Framework.Game.DoUpdate(GameTime gameTime) in
c:\Users\Xanather\Documents\Programming\Libs\MonoGame\MonoGame.Framework\Game.cs:line
697
at Microsoft.Xna.Framework.Game.Tick() in
c:\Users\Xanather\Documents\Programming\Libs\MonoGame\MonoGame.Framework\Game.cs:line
505
at Microsoft.Xna.Framework.SDL2_GameWindow.INTERNAL_RunLoop() in
c:\Users\Xanather\Documents\Programming\Libs\MonoGame\MonoGame.Framework\SDL2\SDL2_GameWindow.cs:line
404
at Microsoft.Xna.Framework.SDL2_GamePlatform.RunLoop() in
c:\Users\Xanather\Documents\Programming\Libs\MonoGame\MonoGame.Framework\SDL2\SDL2_GamePlatform.cs:line
135
at Microsoft.Xna.Framework.Game.Run(GameRunBehavior runBehavior) in
c:\Users\Xanather\Documents\Programming\Libs\MonoGame\MonoGame.Framework\Game.cs:line
437
at Microsoft.Xna.Framework.Game.Run() in
c:\Users\Xanather\Documents\Programming\Libs\MonoGame\MonoGame.Framework\Game.cs:line
415
at Xancraft.Program.Main() in
c:\Users\Xanather\Documents\Programming\Main\XancraftTest\Xancraft\Program.cs:line
16

I am so lost... I'm doing nothing significant with MediaPlayer and if Fez
reports no problems with song on Windows then what?

This error does not happen if I were to close the game quickly after it
started (within the first 10 seconds), after woods its doomed and always
results in this.


Reply to this email directly or view it on GitHubhttps://github.com//issues/97#issuecomment-19162382
.

@xanather
Copy link
Author

xanather commented Jun 9, 2013

http://www.libsdl.org/projects/SDL_mixer/docs/SDL_mixer_12.html

Was just reading it. It mentions that "After calling this all audio is stopped, the device is closed, and the SDL_mixer functions should not be used."

Makes me wonder why MediaPlayer.Stop() needs to be called in the first place then.

@xanather
Copy link
Author

xanather commented Jun 9, 2013

After reading through some of the SDL_Mixer docs and looking at Song.cs I am so surprised this is even happening (I never realized how high level SDL/SDL_Mixer was haha, makes me want to learn C++). I'm probably going to do a variety of tests tomorrow on computers other than just my desktop as well. I'll also try create a test case that can create the problem.

@flibitijibibo
Copy link
Owner

Does adding SDL_mixer.Mix_HookMusicFinished(null); after this line help at all? Wondering if CloseAudio's halting the music and possibly calling the hook:

https://github.com/flibitijibibo/MonoGame/blob/monogame-sdl2/MonoGame.Framework/SDL2/Media/Song.cs#L70

@flibitijibibo
Copy link
Owner

Ha, turns out I was a big liar: there is a 64-bit OSX runtime, as long as you're smart and stay away from System.Windows.Forms and MonoMac.

Still, though.

@xanather
Copy link
Author

Ok, well, I downloaded the updated SDL_Mixer libraries and now I cannot get sound to play at all. I tried looking into why this is happening and I cant figure it out, nothing looks too significant on the SDL_Mixer change log. If I go back to the older SDL_Mixer libraries the sound works but the several crashing issues are back. My laptop reports exactly the same things. For now I am just going to disable SDL_Mixer entirely (thanks for that by the way - its not a required dependency anymore). I want to get back actually developing my game because for now I am over these SDL_Mixer issues and have not made much progress otherwise.

By the time I actually "need" to play songs when my game is closer to release I will probably come back here, maybe others will report the same thing by then. I'm sure as heck still going to be using MonoGameSDL though, I love it and its in a much better state than WindowsGL/WindowsDX (MonoGame's WinDX is still unfinished and has been for 6 months...)

If I come along any other issues I will be sure to post them here, Ill post if I end up getting SDL_Mixer working as well. Thanks for the help flibitijibibo :)

@flibitijibibo
Copy link
Owner

Hm, wondering if your SDL2_mixer build missed Vorbis support, but the VCproj files should include it...

Ah well. Hoping we find a real Vorbisfile# at some point, rather than that thing I found in the dumpster. Glad the rest of MG-SDL2 works though!

@xanather
Copy link
Author

xanather commented Aug 2, 2013

@flibitijibibo
Good news! Playing .ogg files under x86 works now! It must have been a issue with SDL2 (I just downloaded latest binaries). No more leaking warnings, nothing, just works :)

@xanather xanather closed this as completed Aug 2, 2013
@flibitijibibo
Copy link
Owner

Ah, good. Will be nice that this works with the 2.0 release, then. :D

Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants