Hello everyone,
I am trying to figure out a problem that I am having with deleting my ttf_Font wrapper class. Everything is working fine expect when I place my wrapper class inside another class.
My ttf_Font wrapper class looks like this:
//font.h
class Font {
public:
TTF_Font* font = NULL;
Font();
Font(std::string file, int size);
~Font();
};
//font.cpp
Font::Font() {
}
Font::Font(std::string file, int size) {
font = TTF_OpenFont(file.c_str(), size);
if (font == NULL)
std::cout << "Could not open font file " << file << "\n" << TTF_GetError() << "\n\n";
}
Font::~Font() {
std::cout << "del" << std::endl;
TTF_CloseFont(font);
font = NULL;
}
Everything works when the rest of my code code looks like this:
(I have removed error checking code on purpose.)
void run() {
Font font("res/fonts/pixelBubble.ttf", 50);
}
int main(int argc, char *argv[]) {
SDL_Init(SDL_INIT_EVERYTHING);
TTF_Init();
run();
TTF_Quit();
SDL_Quit();
return 0;
}
But if I try to execute this code…:
struct Test {
Font font;
Test() {
font = Font("res/fonts/pixelBubble.ttf", 50);
}
};
void run() {
Test test;
}
int main(int argc, char *argv[]) {
SDL_Init(SDL_INIT_EVERYTHING);
TTF_Init();
run();
TTF_Quit();
SDL_Quit();
return 0;
}
…the program crashes on TTF_CloseFont().
Visual Studio gives me this error:
Exception thrown at 0x6CD08DDB (libfreetype-6.dll) in SDL_Project.exe: 0xC0000005: Access violation reading location 0x00000038.
And when I break the program, it opens a “No Symbols Loaded” file:
Symbol file not loaded
No symbol file for libfreetype-6.dll
Module Information
Version:
Original Location: C:\CppProjects\SDL_Project\SDL_Project\libfreetype-6.dll
I have no idea why I get this error. So I hope someone can explain the problem, and can provide a solution. Thanks in advance.