24 May '13 - 14:10 *
Welcome, Guest. Please login or register.
Did you miss your activation email?

Login with username, password and session length
 
   Home   Help Search Login Register  
Pages: [1]
  Reply  |  Print  
Author Topic: WinXP, VC++ 2005: Problem with calling BASS_Init from overloaded constructor.  (Read 312 times)
AlexP
Posts: 2


« on: 12 Jun '12 - 22:45 »
Reply with quoteQuote

Here is C++ code written in Visual Studio 2005:

#pragma comment(lib, "bass.lib")
#pragma comment(lib, "bassmix.lib")
#include <stdio.h>
#include <windows.h>
#include <bass.h>
#include <bassmix.h>
#define I_WANT_TO_FAIL

const char *filepath = "C:\\file.mp3";

class AudioFile {
public:

  AudioFile() {
    BASS_Init(0, 44100, BASS_DEVICE_MONO, 0, NULL);
  }

  AudioFile(const char *filepath) {
#ifdef I_WANT_TO_FAIL
    AudioFile();
#else
    BASS_Init(0, 44100, BASS_DEVICE_MONO, 0, NULL);
#endif

    HSTREAM stream = BASS_StreamCreateFile(FALSE, filepath, 0, 0, BASS_STREAM_DECODE);
    if (!stream) {
      printf("Error %d\n", BASS_ErrorGetCode()); // Error 8 (aka BASS_ERROR_INIT)
      return;
    }
    printf("No errors\n");
    BASS_StreamFree(stream);
  }

  ~AudioFile() {
    BASS_Free();
  }
};

int main() {
  AudioFile file(filepath);

  system("pause");
  return 0;
}

It's a simplified version of a class that only loads audio file into RAM (but doesn't play it).
If macro I_WANT_TO_FAIL is defined, then BASS_StreamCreateFile() fails, if not - everything's fine. Also, if macro is defined, BASS_GetDevice() returns 0 while in AudioFile() and -1 while in AudioFile(const chat*). Is it a bug? Am I doing something wrong?
« Last Edit: 13 Jun '12 - 10:31 by AlexP » Logged
Ian @ un4seen
Administrator
Posts: 15270


« Reply #1 on: 13 Jun '12 - 12:38 »
Reply with quoteQuote

If the BASS_StreamCreateFile call is resulting in a BASS_ERROR_INIT error, then that indicates that the BASS_Init call has failed. So please also check the error code from that.

    if (!BASS_Init(0, 44100, BASS_DEVICE_MONO, 0, NULL))
      printf("Init Error %d\n", BASS_ErrorGetCode());
Logged
AlexP
Posts: 2


« Reply #2 on: 13 Jun '12 - 19:02 »
Reply with quoteQuote

Thanks for the reply!
I modified the code in both places. None of ifs triggers (with or without macro defined).

But I've figured it out. If I understood correctly, line
    AudioFile();
means construction+destruction of an instance (in a single line) and not function call! By the time I get to BASS_StreamCreateFile(), BASS_Free() has already been called. My fault.

Solution: Call BASS_Init() in each constructor.

By the way, BASS rocks in every known-to-me way!
Logged
Pages: [1]
  Reply  |  Print  
 
Jump to:  

Powered by SMF 1.1.18 | SMF © 2013, Simple Machines