I've been getting an "Access Violation" error while running this program:

#include "SDL/SDL_ttf.h"

class Font
{
private:
   TTF_Font *font;

public:
   Font()
   {
      font = TTF_OpenFont( "c:\\windows\\fonts\\cour.ttf", 20 );
   }
   ~Font()
   {
      TTF_CloseFont( font ); //Access violation
   }
};

int main( int argc, char *args[] )
{
   if( TTF_Init() < 0 )
   {
	   return 1;
   }

   Font thing;
	
   TTF_Quit();

   return 0;
}

Apparently, calling TTF_Quit before TTF_CloseFont will cause the error. Is there any easy way around this?

Recommended Answers

All 4 Replies

Scope perhaps?

if( TTF_Init() < 0 )
   {
	   return 1;
   }

   {
      Font thing;
   }
   TTF_Quit();

Or call another function.

void foo ( ) {
  Font thing;
}

int main ( ) {
   if( TTF_Init() < 0 )
   {
	   return 1;
   }

   foo();
	
   TTF_Quit();
}

Basically, it's just forcing the destructor to be called before Quit.

Apparently, calling TTF_Quit before TTF_CloseFont will cause the error. Is there any easy way around this?

You might also consider using atexit()

Couldn't he also use pointers?

if( TTF_Init() < 0 )
   {
	   return 1;
   }

     Font* thing = new Font;
     delete thing;

   TTF_Quit();

Thanks, these are much better ways than what I had before.

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.