c++ – compiler complaining that memmory is uninitialized


I am writing a Class that is supposed to read a config file and fill a map that contains the configs, while writing the file reading algorithm a pointer to an enum class which contains different config names is throwing a access violation and the compiler complains that it is using uninitialized memmory, as a solution i turned the pointer to a simple variable but the results are the same, the compiler now complains that the variable is uninitialized!

Here is the function that reads the file:

std::map<const char*, Config> conf_str_table = {
    { "screen_size", Config::screen_size },
    { "window_name", Config::window_name },
    { "cam_mov", Config::cam_mov },
};

void FileManager::GetConfigFromFile()
{
    std::ifstream* tmp = new std::ifstream(config_file_name);
    std::string* current_line = new std::string;
    std::pair<Config, std::string>* current_config = new std::pair<Config, std::string>;

    if (tmp->is_open()) {
        while (std::getline(*tmp, *current_line)) {

            // ignoring comments
            if (current_line->at(0) == '#')
                continue;

            bool is_reading_value = false;

            std::string* tmp_conf = new std::string;
            std::string* tmp_value = new std::string;

            for (auto c : *current_line) {

                if (c == ' ')
                    continue;

                if (c == '=') {
                    is_reading_value = true;
                    continue;
                }

                if (is_reading_value) {
                    tmp_value->push_back(c);
                } else {
                    tmp_conf->push_back(c);
                }
            }
            Config* conf_ptr = new Config;

            if (auto search = conf_str_table.find(tmp_conf->c_str()); search != conf_str_table.end()) {
                *conf_ptr = search->second;
            } else {
            }
            delete tmp_conf;
            current_config->first = *conf_ptr;
            current_config->second = *tmp_value;
            delete tmp_value;
        }
        delete current_line;
        delete current_config;
    } else {
        
    }
    delete tmp;
}

and Here is the Class (most of these are not fully implemented yet)

#pragma once

#include "raywin.h"
#include <fstream>
#include <map>
#include <raylib.h>
#include <vector>

constexpr const char* config_file_name = "config.cfg";

enum class Config {
    screen_size,
    window_name,
    cam_mov,
};

enum class Config_Error {
    property_not_found,
    config_file_not_found,
};

struct FileManager {
    /*
         CANNOT throw exceptions !
         if an error occurs it will load
         default settings and notify the user
 */
    void GetConfigFromFile();
    int GetInt(std::string& key);
    std::string GetString(std::string& key);
    double GetDouble(std::string& key);
    Vector2 GetVector2(std::string& key);
    void ErrorUseDefaultConfig(Config_Error& err);

private:
    std::map<Config, std::string> values = {
        { Config::screen_size, "1920x1080" },
        { Config::window_name, "Untitled" },
        { Config::cam_mov, "0.05" },
    };
};

the error occurs on conf_ptr

Leave a Reply

Your email address will not be published. Required fields are marked *