Author Topic: Memory leaks  (Read 367 times)

Bereror

  • Hydlaa Notable
  • *
  • Posts: 773
    • View Profile
    • Planeshift API
Memory leaks
« on: March 05, 2006, 04:31:02 am »
Here are some findings where memory is lost in the new Crystal Space sound system.

1. CS/plugins/sndsys/element/ogg/oggdata2.cpp

The function SndSysOggSoundData::IsOgg() allocates memory for OggStreamData, but never releases it.

This is no big deal because sound files are loaded only once and it is a wasted memory, but nothing that would take the client down.

2. CS/plugins/sndsys/renderer/software/renderer.cpp

The sound engine is supposed to remove sounds and release memory automatically if said so with the iSndSysStream::SetAutoUnregister() function. Planeshift uses that method in several places like for the background music and for interface sounds (the \"click\" sound when a button is clicked with the mouse). Unfortunately the sound engine only removes these sounds from the list, but forgets to decrement the reference counter, so these sound streams and sources will be never released.

It is a small but steady memory leak depending how often one clicks on different buttons and crosses sectors to change the background music.

3. CS/plugins/sndsys/renderer/software/source.cpp

This is the big one. Effects like spells, combat sounds etc. use the SndSysSourceSoftware3D sound source object. SndSysSourceSoftware creates 6 sound filters for 18 sound channels in the SndSysSourceSoftware3D::SetupFilters() function, which are never deleted. In addition, some of these filters allocate memory for processing sound samples which is also never released. Memory allocated and never released for one single sound effect in one of the tests I did was 724 KB.

4. src/common/effects/pseffectobjsound.cpp

The only sound related memory leak in Planesift code that I found ;)

If sound is enabled in the PS setup, but muted in options (or muted because the game window lost focus), the game still creates psEffectObjSound objects, but never deletes them. These objects are deleted when the sound effect stops playing, but since sound effects are muted, they are never started.


After fixing the memory leak no. 3 the client was still leaking memory, but now we are talking about 60 MB in 30 minutes when standing at the Arena and not 500 MB in 8 minutes like it was before (Arena is usually filled with spells, combat sounds etc).
« Last Edit: March 05, 2006, 04:32:24 am by Bereror »
PlaneShift Sources
PlaneShift API
"Words never spoken
Are the strongest resounding"

AryHann

  • Veteran
  • *
  • Posts: 1244
  • WonderWoman
    • View Profile
(No subject)
« Reply #1 on: March 05, 2006, 05:24:36 am »
Really great job!

I was coming to the conclusion about pseffectobjsound myself - with the cooperation of Magodra, of course - and we were discussing with Rhad - who did the new sound renderer - what was wrong.

I hope this thread will be considered by him for the fixes in CS code.
AryHann

http://www.reflex.lth.se/culture/annelov - Virtual Annelöv -
Engine Dep. - One of Talad's Angels - Aka ww & Ahrijani's Goddess

DaveG

  • Forum Addict
  • *
  • Posts: 2058
    • View Profile
(No subject)
« Reply #2 on: March 05, 2006, 08:22:50 am »
WOW!  Thanks Bereror!  Fantastic job!

Seriously, this is a big help.  Please keep this up, if not applying to the team altogether.  This new sound system, while better and more stable, is still new.  I\'m quite glad these things have been found, and I hope we can get them all fixed and on the update server as quickly as possible.  Again, thank you very much.  :)

::  PlaneShift Team Programmer  ::

Rhad

  • Wayfarer
  • *
  • Posts: 3
    • View Profile
(No subject)
« Reply #3 on: March 05, 2006, 03:13:13 pm »
Indeed, thank you!  I\'ve written much of the code for the new sound system in CS.  Since so few projects in CS use sound at all, feedback has been sparse.

  Any feedback, criticism, bug reports you have are definately appreciated - the point of the system wasn\'t to push any personal agenda, but was to replace the older sound system which had so many hacks and workarounds that it was very difficult to understand, and nearly impossible to fix any bug without breaking something else.

  In case you\'re interested, here are the fixes I\'ve made for #1-3 above, which will be commited when sourceforge CVS comes back up:

#1) SndSysOggSoundData - General cleanup.  Most variables have been changed to stack based variables instead of heap allocation.  Member variables renamed and improved documentation.  Memory leak in IsOgg() fixed.

#2) SndSysRendererSoftware - Garbage collection added (fixes memory leak with AutoUnregistered streams).  Active sound objects referenced by the sound renderer are now cleaned up on Close().  The RemoveStream() and RemoveSource() functions no longer wait for the background thread to acknowledge removal (this was buggy anyway). Garbage collection runs at most every 500ms.

#3) Software source filters - Egads, I can\'t believe I let this one slip through.  The filters are a preliminary idea, and are kind of hacked in at the moment to provide some very basic locational audio functionality.  The idea is to make an interface that allows application developers to apply audio distortion procedures to the sound streams (based on source, or renderer).  This includes common distortion such as 3d audio cues, as well as custom distortion that can be applied to a stream.  
  Memory leak should be fixed, the filter chains are now deleted on source destruction, and each filter should delete its children as it\'s deleted.  The code is still ugly though :)

Rhad

AryHann

  • Veteran
  • *
  • Posts: 1244
  • WonderWoman
    • View Profile
(No subject)
« Reply #4 on: March 05, 2006, 03:16:16 pm »
Great work also from you Rhad! And thanks for fixing immediately these things :)
AryHann

http://www.reflex.lth.se/culture/annelov - Virtual Annelöv -
Engine Dep. - One of Talad's Angels - Aka ww & Ahrijani's Goddess