fix for memory leak

This commit is contained in:
WarmUpTill 2016-08-26 21:01:33 +02:00 committed by GitHub
parent 2ed2ff3de3
commit 2b4c0e2eec
2 changed files with 24 additions and 21 deletions

View File

@ -14,13 +14,10 @@ Switcher *switcher = new Switcher();
struct obs_source_info sceneSwitcherOptionsSource; struct obs_source_info sceneSwitcherOptionsSource;
//Hotkeys //Hotkeys
const char *PAUSE_HOTKEY_NAME = "pauseHotkey"; const char *PAUSE_HOTKEY_NAME = "pauseHotkey";
//const char *OPTIONS_HOTKEY_NAME = "optionsHotkey";
obs_hotkey_id pauseHotkeyId; obs_hotkey_id pauseHotkeyId;
//obs_hotkey_id optionsHotkeyId;
obs_data_array_t *pauseHotkeyData; obs_data_array_t *pauseHotkeyData;
//obs_data_array_t *optionsHotkeyData;
//path to config folder where to save the hotkeybinding (probably later the settings file) //path to config folder where to save the hotkeybinding (probably later the settings file)
string configPath; char* configPath;
//save settings (the only hotkey for now) //save settings (the only hotkey for now)
void saveKeybinding(string name, obs_data_array_t *hotkeyData) { void saveKeybinding(string name, obs_data_array_t *hotkeyData) {
@ -33,11 +30,14 @@ void saveKeybinding(string name, obs_data_array_t *hotkeyData) {
if (file.is_open()) { if (file.is_open()) {
size_t num = obs_data_array_count(hotkeyData); size_t num = obs_data_array_count(hotkeyData);
for (size_t i = 0; i < num; i++) { for (size_t i = 0; i < num; i++) {
string temp = obs_data_get_json(obs_data_array_item(hotkeyData, i)); obs_data_t *data = obs_data_array_item(hotkeyData, i);
string temp = obs_data_get_json(data);
obs_data_release(data);
file << temp; file << temp;
} }
file.close(); file.close();
} }
obs_data_array_release(hotkeyData);
} }
} }
@ -56,13 +56,16 @@ string loadConfigFile(string filename) {
return value; return value;
} }
void loadKeybinding(string name, obs_data_array_t *hotkeyData, obs_hotkey_id hotkeyId) { void loadKeybinding(string name, obs_hotkey_id hotkeyId) {
string temp = loadConfigFile(name.append(".txt")); string temp = loadConfigFile(name.append(".txt"));
hotkeyData = obs_data_array_create();
if (!temp.empty()) if (!temp.empty())
{ {
obs_data_array_insert(hotkeyData, 0, obs_data_create_from_json(temp.c_str())); obs_data_array_t *hotkeyData = obs_data_array_create();
obs_data_t *data = obs_data_create_from_json(temp.c_str());
obs_data_array_insert(hotkeyData, 0, data);
obs_hotkey_load(hotkeyId, hotkeyData); obs_hotkey_load(hotkeyId, hotkeyData);
obs_data_release(data);
obs_data_array_release(hotkeyData);
} }
} }
@ -70,7 +73,6 @@ OBS_DECLARE_MODULE()
void SceneSwitcherPauseHotkey(void *data, obs_hotkey_id id, obs_hotkey_t *hotkey, bool pressed) { void SceneSwitcherPauseHotkey(void *data, obs_hotkey_id id, obs_hotkey_t *hotkey, bool pressed) {
UNUSED_PARAMETER(data); UNUSED_PARAMETER(data);
UNUSED_PARAMETER(id);
UNUSED_PARAMETER(hotkey); UNUSED_PARAMETER(hotkey);
if (pressed) if (pressed)
{ {
@ -83,8 +85,9 @@ void SceneSwitcherPauseHotkey(void *data, obs_hotkey_id id, obs_hotkey_t *hotkey
switcher->start(); switcher->start();
} }
} }
//save the keybinding here since it is currently not possible in obs_module_unload //save the keybinding here since it is not possible in obs_module_unload
pauseHotkeyData = obs_hotkey_save(pauseHotkeyId); obs_data_array_release(pauseHotkeyData);
pauseHotkeyData = obs_hotkey_save(id);
} }
const char *sceneSwitcherOptionsGetName(void *type_data) { const char *sceneSwitcherOptionsGetName(void *type_data) {
@ -111,7 +114,9 @@ void sceneSwitcherOptionsSourceGetDefaults(obs_data_t *settings) {
//load settings file //load settings file
string temp = loadConfigFile("settings.txt"); string temp = loadConfigFile("settings.txt");
//set values from file //set values from file
obs_data_apply(settings, obs_data_create_from_json(temp.c_str())); obs_data_t *data = obs_data_create_from_json(temp.c_str());
obs_data_apply(settings, data);
obs_data_release(data);
} }
obs_properties_t *sceneSwitcherOptionsSourceGetProperties(void *data) { obs_properties_t *sceneSwitcherOptionsSourceGetProperties(void *data) {
UNUSED_PARAMETER(data); UNUSED_PARAMETER(data);
@ -142,7 +147,9 @@ void sceneSwitcherOptionsSourceLoad(void *data, obs_data_t *settings) {
//load settings file //load settings file
string temp = loadConfigFile("settings.txt"); string temp = loadConfigFile("settings.txt");
//set values from file //set values from file
obs_data_apply(settings, obs_data_create_from_json(temp.c_str())); obs_data_t *loadedSettings = obs_data_create_from_json(temp.c_str());
obs_data_apply(settings, loadedSettings);
obs_data_release(loadedSettings);
} }
void sceneSwitcherOptionsSourceSetup() { void sceneSwitcherOptionsSourceSetup() {
@ -163,13 +170,11 @@ void sceneSwitcherOptionsSourceSetup() {
bool obs_module_load(void) { bool obs_module_load(void) {
//set config path and check if config dir was set up //set config path and check if config dir was set up
configPath = obs_module_config_path(""); configPath = obs_module_config_path("");
boost::filesystem::create_directories(configPath); boost::filesystem::create_directories(configPath);//<- memory leak here somehow
//register hotkey //register hotkey
pauseHotkeyId = obs_hotkey_register_frontend(PAUSE_HOTKEY_NAME, "Toggle automatic scene switching", SceneSwitcherPauseHotkey, switcher); pauseHotkeyId = obs_hotkey_register_frontend(PAUSE_HOTKEY_NAME, "Toggle automatic scene switching", SceneSwitcherPauseHotkey, NULL);
//optionsHotkeyId = obs_hotkey_register_frontend(OPTIONS_HOTKEY_NAME, "Open the Scene Switcher options menu", SceneSwitcherOptionsHotkey, switcher);
//load hotkey binding if set already //load hotkey binding if set already
loadKeybinding(PAUSE_HOTKEY_NAME, pauseHotkeyData, pauseHotkeyId); loadKeybinding(PAUSE_HOTKEY_NAME, pauseHotkeyId);
//loadKeybinding(OPTIONS_HOTKEY_NAME, optionsHotkeyData, optionsHotkeyId);
//load settings file //load settings file
switcher->setSettingsFilePath(configPath); switcher->setSettingsFilePath(configPath);
switcher->firstLoad(); switcher->firstLoad();
@ -181,11 +186,10 @@ bool obs_module_load(void) {
} }
void obs_module_unload(void) { void obs_module_unload(void) {
bfree(configPath);
switcher->stop(); switcher->stop();
//save settings (only hotkey for now) //save settings (only hotkey for now)
saveKeybinding(PAUSE_HOTKEY_NAME, pauseHotkeyData); saveKeybinding(PAUSE_HOTKEY_NAME, pauseHotkeyData);
//saveKeybinding(OPTIONS_HOTKEY_NAME, optionsHotkeyData);
//obs_hotkey_unregister(pauseHotkeyId);
obs_data_array_release(pauseHotkeyData); obs_data_array_release(pauseHotkeyData);
delete switcher; delete switcher;
switcher = NULL; switcher = NULL;

View File

@ -30,7 +30,6 @@ private:
bool isWindowFullscreen(); bool isWindowFullscreen();
string GetActiveWindowTitle(); string GetActiveWindowTitle();
pair<int, int> getCursorXY(); pair<int, int> getCursorXY();
bool quit;
mutex mtx; mutex mtx;
condition_variable terminate; condition_variable terminate;
}; };