tophat icon indicating copy to clipboard operation
tophat copied to clipboard

Memory leaks in `umsoundplay()` if called too frequently

Open vtereshkov opened this issue 3 years ago • 8 comments

How to reproduce:

  • Run the tetris example
  • Hit a rotation (Z, X, C) or drop (space) key several times very quickly
  • Close the program window
  • See memory leak warnings

vtereshkov avatar Aug 27 '22 15:08 vtereshkov

Looks like the problem is unnecessary increment: https://github.com/marekmaskarinec/tophat/blob/main/src/bindings.c#L352

ske2004 avatar Aug 27 '22 15:08 ske2004

I can see why it was put there, it creates issue of memory leaks but it solves the problem of the object getting freed after being discarded. The way to solve this is probably to allocate the handle instead

ske2004 avatar Aug 27 '22 15:08 ske2004

@skejeton Notice that this leak never happens if the function is called not too often (so that the sound plays to the end and stops normally?)

vtereshkov avatar Aug 27 '22 15:08 vtereshkov

Yeah it seems to only happen when the sound didn't finish playing entirely

ske2004 avatar Aug 27 '22 15:08 ske2004

Notice that this leak never happens if the function is called not too often (so that the sound plays to the end and stops normally?)

This is weird, since adding more playbacks shouldn't affect other playbacks.

Looks like the problem is unnecessary increment: https://github.com/marekmaskarinec/tophat/blob/main/src/bindings.c#L352

This is since the thg.playbacks own a reference. It is decremented when the sound stop playing.

marekmaskarinec avatar Aug 27 '22 20:08 marekmaskarinec

This is weird, since adding more playbacks shouldn't affect other playbacks.

But what if all those playbacks refer to the same audio.Sound?

vtereshkov avatar Aug 27 '22 20:08 vtereshkov

Playbacks don't refer to audio.Sounds at all.

marekmaskarinec avatar Aug 27 '22 20:08 marekmaskarinec

@marekmaskarinec prev = pbi; is missing from the for loop in _th_audio_data_callback(), so it cannot process lists longer than one sound.

vtereshkov avatar Aug 31 '22 11:08 vtereshkov

@marekmaskarinec prev = pbi; is missing from the for loop in _th_audio_data_callback(), so it cannot process lists longer than one sound.

This might have been the problem. I fixed it and now I can't reproduce neither the memory leak warnings or the sound cutting of too early issue.

marekmaskarinec avatar Sep 02 '22 18:09 marekmaskarinec

Great!

vtereshkov avatar Sep 02 '22 20:09 vtereshkov