I'm running into a problem when using the SDL2 Mixer library in my C++ project. As soon as I'm calling a function on a class that #includes SDL_mixer.h, the project will not run. When trying to Run it, I get the following message:
When debugging the project, the process finishes with exit code 0, but with an additional message:
and
As you might be able to deduce, adding breakpoints to the code does not provide any additional information as the project doesn't start at all.
Commenting out the lines that use the class that include mixer makes the project runnable again seemingly without issues. I've tried to google the error code, but I can't find any documentation on what it means.
Other, possibly relevant information: I'm using conan to install my libraries and have it set to auto-install any dependencies.
I'll post the header code of the class in question here, though I don't believe the problem lies here as the project won't run to even reach the code anyway.
#include <SDL.h>
#include <SDL_mixer.h>
#include <memory>
#include <string>
#include <map>
class SoundService {
public:
/*******************************************************************************
* @brief Gets the singleton instance of the SoundService class.
*
* @return A reference to the instance of the SoundService.
******************************************************************************/
static std::unique_ptr<SoundService> &getInstance() {
if (instance == nullptr) {
if(Mix_OpenAudio(44100, MIX_DEFAULT_FORMAT, 2, 4096) < 0){
SDL_Log("Mixer Initialization Error: %s", Mix_GetError());
}
instance = std::unique_ptr<SoundService>(new SoundService());
}
return instance;
}
/**
* @brief Loads a music file and stores it with a given id string.
* @param id
* @param filePath
* @return True if the asset has been loaded successfully, otherwise false.
*/
bool loadMusic(const std::string &id, const std::string &filePath);
void playMusic(const std::string &id, int loops = -1);
void pauseMusic();
void resumeMusic();
/**
* @brief Removes a music file by id.
* @param id
* @return True if the asset has been removed successfully, otherwise false.
*/
bool removeMusic(std::string &id);
/**
* @brief Loads an sfx file and stores it with a given id string.
* @param id
* @param filePath
* @return True if the asset has been loaded successfully, otherwise false.
*/
bool loadSFX(const std::string &id, const std::string &filePath);
void playSFX(const std::string &id, int loops = 0, int channel = 0);
/**
* @brief Removes an sfx file by id.
* @param id
* @return True if the asset has been removed successfully, otherwise false.
*/
bool removeSFX(std::string &id);
/**
* @brief Removes all the music files and sfx files from the sound service
*/
void clear();
private:
SoundService() = default;
static std::unique_ptr<SoundService> instance;
std::map<std::string, Mix_Music *> musicMap;
std::map<std::string, Mix_Chunk *> sfxMap;
};
The function that I'm calling that breaks the project is just a simple line of testcode:
auto& soundService = SoundService::getInstance();
Here is the conanfile used to declare the required libraries:
from conans import ConanFile, CMake
from conans.tools import os_info
class ModularMagicConan(ConanFile):
name = "ModularMagic"
version = "0.1"
url = "edited out"
description = "A 2D game"
settings = "os", "compiler", "build_type", "arch"
generators = "cmake"
def requirements(self):
self.requires("sdl2/2.0.12@bincrafters/stable") # SDL2, rendering and window creation library
self.requires("sdl2_image/2.0.5@bincrafters/stable") # SDL2 image, texture library
self.requires("sdl2_mixer/2.0.4@bincrafters/stable") # SDL2 mixer, sound library
self.requires("gtest/1.10.0") # Used for unit testing
self.requires("box2d/2.4.0") # Box2D, physics library
def build(self):
cmake = CMake(self)
cmake.configure()
cmake.build()
def test(self):
cmake.test()
Anyone familiar with this issue or has any idea what's causing it and how to resolve it?
Well, I don't know if it really counts as an answer, but perhaps it will help others with the same problem. Me and a colleague found this sketchy suggestion somewhere to throw a copy of glib-2.0-0.dll (which you should be able to find in a .conan folder in your :C drive) in System32. This somehow works, I can run my project now. I don't know if this is something that's wrong with SDL_mixer, as it certainly doesn't seem like the right way to solve it, but at least I can work on my project again.