Cześć widziałem parę podobnych, tematów, lecz wydaje mi się że u mnie jest problem złej organizacji projektu.
Problem polega wyrzucenie wyjątka "Segmentation fault", w momencie usuwania textury,
Żeby nie zaciemniać:
int main(void)
{
DisplayMenager &dispMngr = DisplayMenager::getInstance();
dispMngr.startup({800, 600});
(...)
glfwTerminate();
return 0;
}
Display menager jest singletonem (coś mi się wydaje że tutaj już pierwszy błąd popełniłem), robi startup operacji.
void DisplayMenager::startup(glm::vec2 wndSize)
{
this->width = wndSize.x;
this->height = wndSize.y;
this->init();
this->enableDebug();
this->prepare();
}
void DisplayMenager::prepare()
{
//SETUP THE BLENDING MODE r-(1-a) g-(1-a) b-(1-a)
glBlendFunc(GL_SRC_ALPHA, GL_ONE_MINUS_SRC_ALPHA);
glEnable(GL_BLEND);
this->LoadResource();
}
void DisplayMenager::LoadResource()
{
std::shared_ptr<Shader> tempShader(new Shader("res/shaders/Basic.vert"));
this->_rscMngr.Add<Shader>("BasicShader", tempShader);
std::shared_ptr<Texture> tempTexture(new Texture("res/point.png"));
this->_rscMngr.Add<Texture>("point", tempTexture);
}
Pole this->_rscMngr wyglada tak:
class DisplayMenager
{
private:
DisplayMenager();
DisplayMenager(DisplayMenager const &);
void operator=(DisplayMenager const &);
ResourceMenager &_rscMngr=ResourceMenager::getInstance();
(...)
public:
static DisplayMenager &getInstance()
{
static DisplayMenager instance;
return instance;
}
};
Sam ResourceMengaer również jest singletonem - zależało mi żeby mieć dostęp do wszystkich zasobów z każdego miejsca programu - tak wiem że nie jest to thread safe, ale na razie ni przychodzi mi nic innego do głowy, no może factory pattern ...
class ResourceMenager
{
private:
std::unordered_map<std::string, std::shared_ptr<Shader>> Shaders;
std::unordered_map<std::string, std::shared_ptr<Texture>> Textures;
ResourceMenager() {}
ResourceMenager(ResourceMenager const &) {}
void operator=(ResourceMenager const &) {}
public:
static ResourceMenager &getInstance()
{
static ResourceMenager instance;
return instance;
}
~ResourceMenager(){
}
template <typename T>
void Add(const std::string &name, T &resource);
template <typename T>
void Add(const std::string &name, std::shared_ptr<T> resource);
template <typename T>
T getResource(const std::string &name);
};
oraz cześć cpp
template <>
void ResourceMenager::Add<Texture>(const std::string &name, std::shared_ptr<Texture> resource){
this->Textures.emplace(name, resource);
}
Segmentation fault wchodzi przy wychodzeniu z programu, przy wywoływaniu (niejawnie) metody clear na unordered_map,
i prowadzi do
Texture::~Texture()
{
glDeleteTextures(1, &(this->_RendererID));
}
Wygląda mi to na jakiś problem z wywłaszczaniem, bo gdy ResourceMenager jest w main() nic zlego sie nie dzieje.
Wszystko generowane w cmake , a kompilowane :
gcc (Rev1, Built by MSYS2 project) 7.3.0
Copyright (C) 2017 Free Software Foundation, Inc.
This is free software; see the source for copying conditions. There is NO
warranty; not even for MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.
Flagi:
add_definitions(-DGLEW_STATIC)
add_definitions(-D_WIN32=1)
add_definitions(-Dfpermissive)
add_definitions(-Dj8)
Pozdrawiam.