SndPlaySound causing crashes

  • Thread starter Thread starter Nak
  • Start date Start date
N

Nak

Hi there,

*fingers crossed that the newsserver works long enough for me to see any
replies to this!!*

I'm having crashes when using SndPlaySound with SND_ASYNC and SND_MEMORY
in VB.NET. The sounds are being loaded from embedded resources and stored
into byte arrays, which are then being stored in a hashtable for easy
retrieval second time around. Occasionally a get a funny "beep" sound and
the application crashes, the report tells me that it comes from "winmm.dll".
The only reference I could find to this crash was

http://ftp.tuniv.szczecin.pl/pub/windows/Win3/winsock-l/Misc_Utils/Win32+TCPIP/w32s125a.txt

But that is in reference to "MICROSOFT WIN32S UPGRADE". Any ideas on
this one? I've just made my first ever game and the sounds keep making it
crash :-(

Nick.

--
/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\
"No matter. Whatever the outcome, you are changed."

Fergus - September 5th 2003
/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\
 
Nak said:
I'm having crashes when using SndPlaySound with SND_ASYNC and SND_MEMORY
in VB.NET. The sounds are being loaded from embedded resources and stored
into byte arrays, which are then being stored in a hashtable for easy
retrieval second time around. Occasionally a get a funny "beep" sound and
the application crashes, the report tells me that it comes from "winmm.dll".
The only reference I could find to this crash was

Does this sample work for you?

<http://www.google.de/[email protected]>

Notice that 'sndPlaySound' should not be used any more, it's only
available for compatibility with old applications.
 
It sounds like this could be caused by the GC moving the byte array in
memory, and thereby invalidating the pointer you've passed to
sndPlaySound. But I can't say for sure without seeing your code.



Mattias
 
Hi Herfried,
Does this sample work for you?

Excellent, thank you, it works a treat, even with my caching method. The
one I was using previously wasn't passing a pointer, but a whole byte array,
maybe that is why it was failing so easily?
Notice that 'sndPlaySound' should not be used any more, it's only
available for compatibility with old applications.

What would you recommend using? I didn't want to use DirectX for a reason,
even though I have already developed similar classes with increased
functionality, my game is "bog standard", so I have done it the "bog
standard" way :-)

Nick.

--
/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\
"No matter. Whatever the outcome, you are changed."

Fergus - September 5th 2003
/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\
 
Hi Nick,

I believe that Herfried was refering to PlaySound. Please read the
following in MSDN documentation:

"This function (sndPlaySound) offers a subset of the functionality of the
PlaySound function; sndPlaySound is being maintained for backward
compatibility."

Have a nice day!

Regards,

HuangTM
Microsoft Online Partner Support
MCSE/MCSD

Get Secure! -- www.microsoft.com/security
This posting is provided "as is" with no warranties and confers no rights.
 
"This function (sndPlaySound) offers a subset of the functionality of the
PlaySound function; sndPlaySound is being maintained for backward
compatibility."

Aaah, thanks :-)

Nick.

--
/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\
"No matter. Whatever the outcome, you are changed."

Fergus - September 5th 2003
/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\
 
Hi there,
Just a guess, but SndPlaySound is a WinApi call and the source of the
sound is a .NET HashTable. If you're giving a pointer to .NET memory to an
asynchronous Win32 function, it's highly possible that your HashTable contents
have been shuffled around, perhaps by a garbage collection, and all of a
sudden SndPlaySound is grabbing at thin air. If so, I guess you could do a
GlobalAlloc and copy the sound wave into it for the duration of the
playing.

The data for the sound *was/is* being stored in a Hashtable, retrieved into
a byte array and they byte array was being passed to the function. Surely
the garbage collector shouldn't change the address of something that is in
use? It's quite strange, the CLR seems to randomly choose what objects it
reclaims when they fall out of scope, I had a http downloading class
(Microsoft have a very similar example) which has objects that fall out of
scope but never get collected, that was the downside to it because you never
had a solid reference to the object.
ps. Here's a picture of me in my Harry Enfield jacket. What do you think - do
I look like some Dork?

Not really, that happens to be one of my best friends!

Nick.

--
/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\
"No matter. Whatever the outcome, you are changed."

Fergus - September 5th 2003
/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\
 
Hi Fergus,
Just a guess, but SndPlaySound is a WinApi call and the source of the
sound is a .NET HashTable. If you're giving a pointer to .NET memory to an
asynchronous Win32 function, it's highly possible that your HashTable contents
have been shuffled around, perhaps by a garbage collection, and all of a
sudden SndPlaySound is grabbing at thin air. If so, I guess you could do a
GlobalAlloc and copy the sound wave into it for the duration of the
playing.

A man of many many guises!

Nick.

--
/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\
"No matter. Whatever the outcome, you are changed."

Fergus - September 5th 2003
/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\
 
Hi Nick,

LOL - you saw through it. ;-)) What gave the game away, I wonder.

|| that happens to be one of my best friends!

Lol, again. And he's achieved fame and notoriety as the man with the funny
jaw, the wobbly head and the belch!! No, he's not a dork, but he does remind
me of Harry Enfield (only in the less close-up pictures).

A good bit of fun is FFC. I had a liitle play with it. (And it's why I
knew you'd be able to answer that sprite question - shame that guy never came
back),

ps. I've missed you, you've only been dropping in for seconds at a time.
But I'm glad that it's because you're getting into your Game Prog. More power
to your elbow. :-)

Regards,
Fergus
 
Surely
the garbage collector shouldn't change the address of something that is in
use?

It sure can do that, when "compacting" the memory after having cleaned
up other objects.

And there's no way for the runtime to know that the byte array is "in
use" by the unmanaged API function. The runtime can only track uses in
managed code.

It's quite strange, the CLR seems to randomly choose what objects it
reclaims when they fall out of scope, I had a http downloading class
(Microsoft have a very similar example) which has objects that fall out of
scope but never get collected, that was the downside to it because you never
had a solid reference to the object.

How do you know they weren't collected?



Mattias
 
Hi there,
It sure can do that, when "compacting" the memory after having cleaned
up other objects.
And there's no way for the runtime to know that the byte array is "in
use" by the unmanaged API function. The runtime can only track uses in
managed code.

Aah, that makes sence, but if I still have a solid reference to the data
that is being garbage collected, that should be detected as being in use.
Data being stored in a hash table is surely a solid reference, why would
their addresses change?
How do you know they weren't collected?

Because the application still functions and a callback occurs. Also when
terminating the class it becomes a nightmare because you have no references
to dispose of, they just seem to float around. I replaced it in the end for
a class that simply runs synchronously in another thread to prevent the main
thread from becoming blocked, far more reliable than the previous method.

Nick.

--
/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\
"No matter. Whatever the outcome, you are changed."

Fergus - September 5th 2003
/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\
 
Hi Fergus,
LOL - you saw through it. ;-)) What gave the game away, I wonder.

;-) Where there is a will, there is a way!
Lol, again. And he's achieved fame and notoriety as the man with the funny
jaw, the wobbly head and the belch!! No, he's not a dork, but he does remind
me of Harry Enfield (only in the less close-up pictures).

Yeah I know you were only joking, but I thought you *may* have been someone
else, so I was proceeding with caution, if you know what I mean :-)
A good bit of fun is FFC. I had a liitle play with it. (And it's why I
knew you'd be able to answer that sprite question - shame that guy never came
back),

Yeah, my next one's going to be a damn site better, hopefully anyway. Glad
you like it :-)
ps. I've missed you, you've only been dropping in for seconds at a time.
But I'm glad that it's because you're getting into your Game Prog. More power
to your elbow. :-)

Yeah, I don't seem to be able to use the newsgroups very well lately, OE it
seems to refuse to pick up headers and the most inconvenient parts of the
day. I've just started doing a bit of game programming just for the
experience, I read an article somewhere that said start with a Tetris clone,
damn, I didn't think it would be that hard! but it works now anyway, I'm
gonna stick it on my web sit soon providing I don't get sued!

Nick.

--
/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\
"No matter. Whatever the outcome, you are changed."

Fergus - September 5th 2003
/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\
 
Hi Nick,

The whole point about managed memory is that the GC knows where everything
is, who talks to who and how's your father. Thus, when the GC stops the music,
every object freezes and waits for the music to start again. During that
period, the GC can shift objects about devil-may-care. And because the GC <is>
that devil, and takes that care, it always fixes up the objects' pointers
accordingly. So when the GC says "off you go then children", some will have
moved but they'll never know it.

Not the case for uny one in unmanged territory - their pointers into te
managed memory space can go invalid at a moment's notice.

There is a way round all this. In C# you can have memory that is 'pinned'
in place - such that the GC is not allowed to touch it. In C# the keyword is
'fixed' but I don't think it's possible to do pinning in VB. You could have a
C# Project in your solution which can deal with the sounds. (Good excuse to
get your toes wet in C#, too).

Regards,
Fergus
 
Hi Nick,

|| I thought you *may* have been someone else, so I was
|| proceeding with caution, if you know what I mean :-)

In a way it would have been good if it had been him - so that he could
read what you said about him - if he's the one I think you're talking about!!

|| Tetris

I like Tetris - I'll alpha/beta test if you like.

Regards,
Fergus
 
Hi Fergus,

WOW, OE working 2 attempts in a row!!! Is this too good to be true?
I like Tetris - I'll alpha/beta test if you like.

I'm gonna start referring it to as a "falling blocks game" just so there
is no affiliation with Tetris ;-)

Nick.

--
/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\
"No matter. Whatever the outcome, you are changed."

Fergus - September 5th 2003
/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\
 
Hi Nick,

|| WOW, OE working 2 attempts in a row!!!

Yo!!

|| Is this too good to be true?

I only got one copy of the virus-spam email in the last 24 hours, and
OE/msnews has regained it's two-minute response time!! I was going to mention
it myself but I bottled out in case the Gods were watching. Well done - you've
averted the curse for me.

|| I'm gonna start referring it to as a "falling blocks game"
|| just so there is no affiliation with Tetris ;-)

The first version I ever played was a gorgeous 3-D wire-frame version.
Excellent fun. It was called 'Block'!!

Whatever you call it, I look forward to it. ;-)

Regards,
Fergus

Tetris - I never liked that name. Apparently it's derived from 'tetra' because
of the four squares that make up each piece. You could call your game
FourPlay!!

But then again, there are seven possible shapes so you could call it Septis.

But then again - Nah!!
 
Fergus Cooney said:
In C# you can have memory that is 'pinned'
in place - such that the GC is not allowed to touch it. In C# the keyword is
'fixed' but I don't think it's possible to do pinning in VB. You could have a
C# Project in your solution which can deal with the sounds. (Good excuse to
get your toes wet in C#, too).

Have a look at the 'GCHandle' class.
 
Hi Fergus,
Tetris - I never liked that name. Apparently it's derived from 'tetra' because
of the four squares that make up each piece. You could call your game
FourPlay!!

LOL! I was thinking if "Bastard blocks", but I decided to call it
"nikblox". I have incorporated a "Bastard blocks" feature though, which is
pretty ammusing, if your lines equal a multiple of 10 you get dished out 1
bastard block, which is a really awkwardly shaped block made out of 5 rather
than 4 blocks, I love the cat sound that it plays, MMMMMMEEEEAAWWW, someone
kick that cat!!!

Nick.

--
/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\
"No matter. Whatever the outcome, you are changed."

Fergus - September 5th 2003
/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\
 
Hi Nick,

Bastard blocks sounds like a good innovation.

If it were a dog you could have Nikblox as its day-to-day doggie
name and Nick-a-Block-a-Glory as its fancy show-time pedigree name.

Regards,
Fergus

ps. I hope the keyboard setup is editable - I like my numeric keypad, I do,
with Spacebar to drop.
 
Back
Top