While trying to debug a program for heap corruption errors, I decided to use gflags in the Debugging Tools package for Windows as suggested in this stack overflow post. However, when I try to use this tool and enable full page heap checking by executing gflags -p /enable <program name> /full
(with program name being my program's path), one of the libraries I use, SDL_ttf, throws an exception saying some variation of "Exception thrown at 0x6CD3D0FC (libfreetype-6.dll) in Jetfuel Graphical Example.exe: 0xC0000005: Access violation reading location 0x0A02A000" after the function TTF_OpenFont is used with valid arguments. Does this mean my version of SDL_TTF is causing my heap corruption errors, or does it mean my code is doing something wrong?
Here is my code(When I was testing this, I had filename set correctly to "default.ttf", no face index, and font size set to 20):
#include "font.h"
namespace jetfuel {
namespace draw {
bool Font::m_ttfinit = false;
Font::Font(const std::string filename) {
m_filename = filename;
m_isfontloaded = true;
}
Font::Font(const std::string filename, const long faceindex) {
m_filename = filename;
m_faceindex = faceindex;
m_isfontloaded = true;
}
void Font::Load_font(const std::string filename){
Set_file_name(filename);
Set_is_font_loaded(true);
}
void Font::Load_font(const std::string filename, const long faceindex){
Set_file_name(filename);
Set_face_index(faceindex);
Set_is_font_loaded(true);
}
void Font::Change_size(const unsigned int size){
// Open Font with font index if index is set.
if (!Get_ttf_init()) {
if (TTF_Init() != 0) {
std::cerr << "SDL TTF ERROR:" << TTF_GetError() << "\n";
throw exceptions::SDL_ttf_exception();
}
Set_ttf_init(true);
}
if(Get_file_name()!=""){
if (Get_ttf_font() != nullptr) {
TTF_CloseFont(Get_ttf_font());
}
std::string filename = Get_file_name();
char *filenamecstring = new char[filename.length()+1];
TTF_Font *ttffont;
strcpy(filenamecstring, filename.c_str());
SDL_ClearError();
if(Get_face_index()!=-1){
ttffont = TTF_OpenFontIndex(filenamecstring, size,
Get_face_index()); // And also can happen here!!
}else{
ttffont = TTF_OpenFont(filenamecstring, size); // Error happens here!!
}
Set_ttf_font(ttffont);
if(Get_ttf_font()==nullptr){
std::cerr << "SDL TTF ERROR:" << TTF_GetError() << "\n";
throw exceptions::SDL_ttf_exception();
}
}
}
} /* namespace draw */
} /* namespace jetfuel */
And here are the versions of libraries and my OS:
- Windows 8.1 64-bit
- Visual Studio 2017 Community
- SDL 2.0.6
- SDL_ttf 2.0.14