Permalink
Browse files

Added a wrapper to make to clear an ogg vorbis struct only if initial…

…ized.
  • Loading branch information...
1 parent 2de8456 commit 638dc36a4334127b452fcdfc693ee34e4c3bae39 Yohann Ferreira committed Oct 5, 2012
Showing with 19 additions and 4 deletions.
  1. +10 −0 src/engine/audio/audio_input.cpp
  2. +9 −4 src/engine/audio/audio_input.h
@@ -208,7 +208,16 @@ uint32 WavFile::Read(uint8* buffer, uint32 size, bool& end) {
// OggFile class methods
////////////////////////////////////////////////////////////////////////////////
+OggFile::~OggFile() {
+ if (_initialized) {
+ ov_clear(&_vorbis_file);
+ _initialized = false;
+ }
+}
+
bool OggFile::Initialize() {
+ _initialized = false;
+
// Windows requires a special loading method in order load ogg files
// properly when dynamically linking vorbis libs. The workaround is
// to use the ov_open_callbacks function
@@ -248,6 +257,7 @@ bool OggFile::Initialize() {
_sample_size = _number_channels * _bits_per_sample / 8;
_data_size = _total_number_samples * _sample_size;
+ _initialized = true;
return true;
} // bool OggFile::Initialize()
@@ -182,10 +182,11 @@ class WavFile : public AudioInput {
class OggFile : public AudioInput {
public:
OggFile(const std::string& file_name) :
- AudioInput(), _read_buffer_position(0), _read_buffer_size(0) { _filename = file_name; }
+ AudioInput(), _read_buffer_position(0), _read_buffer_size(0),
+ _initialized(false)
+ { _filename = file_name; }
- ~OggFile()
- { ov_clear(&_vorbis_file); }
+ ~OggFile();
//! \brief Inherited functions from AudioInput class
//@{
@@ -209,6 +210,10 @@ class OggFile : public AudioInput {
//! \brief Size of available data on the buffer (for the emporal buffer).
uint16 _read_buffer_size;
+ //! \brief Tells whether the ogg/vorbis structures are successfully allocated.
+ //! It is used to know whether they can be deallocated.
+ bool _initialized;
+
/** \brief A wrapper function for file seek operations
*** \param ffile A pointer to the FILE struct which represents the input stream
*** \param off The number of bytes to offset from the stream's origin
@@ -239,7 +244,7 @@ class AudioMemory : public AudioInput {
*** fill that memory with the audio data read from the input argument
**/
AudioMemory(AudioInput* input);
-
+
AudioMemory(const AudioMemory& audio_memory);
AudioMemory& operator=(const AudioMemory& other_audio_memory);

0 comments on commit 638dc36

Please sign in to comment.