I made a very silly beginner's mistake, I called Dispose on a SoundEffect that I loaded using the ContentManager.
I should have known that assets like these are owned by the ContentManager, and are freed when calling ContentManager.Unload
However, the strangest happened in my case: the sound got corrupted, sometimes my game crashed, XAudio started printing all kinds of warnings...
It would be very helpful to add some debugging code that prints a warning when calling Dispose on an asset that is owned by the ContentManager, to help other beginners :-)
It's perfectly fine to call Dispose on IDisposable assets. Perhaps it should throw an error or print a warning when you try to play it.
How XNA behaves in this situation? A Test for this case would be nice.
Did you test it with the latest 3.6?
Yes 3.6 (latest) and 3.5.1 behave the same in this regard.
The XNA behavior throws an ObjectDisposedException
XNA also throws this exception when calling SoundEffect.CreateInstance
Now if you call dispose on an asset that got loaded by a ContentManager, and afterwards you load the asset again, you get back the unusable disposed cached asset, unless you called ContentManager.Unload. That's also rather confusing. But that's the way XNA works I guess.
That's very specific.
Did you used a disassembler of some sort?
Yes, DotPeek (could also have looked at the IL code), and I also tried it in XNA 4.0.
@Ziriax
https://github.com/MonoGame/MonoGame/blob/develop/CONTRIBUTING.md#decompiler-tools
That's a legal matter, so I will not say anything more in this thread.
@tomspilman , @KonajuGames ?
@Ziriax Can you remove the disassembled lines from your comment please?
I ran into XNA - MonoGame differences in this area before. MG should explicitly check if IDisposable objects are disposed in the functions they expose and throw if they are.
@Jjagg Yes, I know, but for something trivial as this I hoped it would not be a problem. But I edited my posts anyway, mea culpa
Yes, DotPeek
@Ziriax - We never use reflection tools to implement any code in MonoGame because it technically is stealing Microsoft's code. In the future please avoid posting any reflected code to us just in case.
That's a legal matter, so I will not say anything more in this thread.
@nkast
I just checked and yes... XNA throws when a SoundEffect is disposed and you call CreateInstance on it.
We could certainly use some unit tests on this to get the behavior right. The implementation of it is pretty obvious I think, so i'm not worried about the previously posted code.
@tomspilman I understand this perfectly, it was never my intention to publish stolen code, again, my apologies. As a side-note, Monogame implements the Microsoft XNA API, and is this API copyrighted or not? Personally after 30 years I can write code almost with my eyes closed, but crafting a good API is much harder (see the ongoing fight between Oracle and Google about the Java API).
it was never my intention to publish stolen code, again, my apologies.
We know... people in general don't realize this, but we are very careful here to protect the project.
is this API copyrighted
I am not a lawyer, but the MonoGame project started back before this API copyright mess. We continue to operate as if MS doesn't consider the XNA API to be copyright protected and something we cannot use. We feel pretty confident in this as Microsoft is a big supporter of MonoGame and has even help us get MonoGame on Xbox One.
Still out of respect for Microsoft and the XNA team (and of course copyright) we don't copy their documentation or their specific implementation.