[haiku-commits] r42453 - haiku/trunk/src/kits/game

  • From: marcusoverhagen@xxxxxxxxx
  • To: haiku-commits@xxxxxxxxxxxxx
  • Date: Tue, 19 Jul 2011 20:14:29 +0200 (CEST)

Author: marcusoverhagen
Date: 2011-07-19 20:14:29 +0200 (Tue, 19 Jul 2011)
New Revision: 42453
Changeset: https://dev.haiku-os.org/changeset/42453
Ticket: https://dev.haiku-os.org/ticket/3241

Modified:
   haiku/trunk/src/kits/game/FileGameSound.cpp
Log:
This should fix crashes due to bad initialization. Completely untested. Might 
help with ticket #3241


Modified: haiku/trunk/src/kits/game/FileGameSound.cpp
===================================================================
--- haiku/trunk/src/kits/game/FileGameSound.cpp 2011-07-19 17:42:36 UTC (rev 
42452)
+++ haiku/trunk/src/kits/game/FileGameSound.cpp 2011-07-19 18:14:29 UTC (rev 
42453)
@@ -353,10 +353,18 @@
 status_t
 BFileGameSound::Init(const entry_ref* file)
 {
-       fAudioStream = new _gs_media_tracker;
+       fAudioStream = new(std::nothrow) _gs_media_tracker;
+       if (!fAudioStream)
+               return B_NO_MEMORY;
+
        memset(fAudioStream, 0, sizeof(_gs_media_tracker));
-
-       fAudioStream->file = new BMediaFile(file);
+       fAudioStream->file = new(std::nothrow) BMediaFile(file);
+       if (!fAudioStream->file) {
+               delete fAudioStream;
+               fAudioStream = NULL;
+               return B_NO_MEMORY;
+       }
+       
        status_t error = fAudioStream->file->InitCheck();
        if (error != B_OK)
                return error;
@@ -365,19 +373,28 @@
 
        // is this is an audio file?
        media_format playFormat;
-       if ((error = fAudioStream->stream->EncodedFormat(&playFormat)) != B_OK)
+       if ((error = fAudioStream->stream->EncodedFormat(&playFormat)) != B_OK) 
{
+               fAudioStream->file->ReleaseTrack(fAudioStream->stream);
+               fAudioStream->stream = NULL;
                return error;
+       }
 
-       if (!playFormat.IsAudio())
+       if (!playFormat.IsAudio()) {
+               fAudioStream->file->ReleaseTrack(fAudioStream->stream);
+               fAudioStream->stream = NULL;
                return B_MEDIA_BAD_FORMAT;
+       }
 
        gs_audio_format dformat = Device()->Format();
 
        // request the format we want the sound
        memset(&playFormat, 0, sizeof(media_format));
        playFormat.type = B_MEDIA_RAW_AUDIO;
-       if (fAudioStream->stream->DecodedFormat(&playFormat) != B_OK)
+       if (fAudioStream->stream->DecodedFormat(&playFormat) != B_OK) {
+               fAudioStream->file->ReleaseTrack(fAudioStream->stream);
+               fAudioStream->stream = NULL;
                return B_MEDIA_BAD_FORMAT;
+       }
 
        // translate the format into a "GameKit" friendly one
        gs_audio_format gsformat;
@@ -410,6 +427,9 @@
 bool
 BFileGameSound::Load()
 {
+       if (!fAudioStream || !fAudioStream->stream)
+               return false;
+  
        // read a new buffer
        int64 frames = 0;
        fAudioStream->stream->ReadFrames(fBuffer, &frames);


Other related posts:

  • » [haiku-commits] r42453 - haiku/trunk/src/kits/game - marcusoverhagen