954,500 Members — Technology Publication meets Social Media
Username:
Password:
Lost login information?
Have something to say? Contribute New Article Reply to this Article

Need an easy way around an access violation

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?

Evan M
Light Poster
42 posts since Sep 2007
Reputation Points: 11
Solved Threads: 5
 

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.

Salem
Posting Sage
Team Colleague
11,531 posts since Dec 2005
Reputation Points: 5,862
Solved Threads: 953
 
Apparently, calling TTF_Quit before TTF_CloseFont will cause the error. Is there any easy way around this?

You might also consider using atexit()

mitrmkar
Posting Virtuoso
1,809 posts since Nov 2007
Reputation Points: 1,105
Solved Threads: 395
 

Couldn't he also use pointers?

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

     Font* thing = new Font;
     delete thing;

   TTF_Quit();
CoolGamer48
Posting Pro in Training
401 posts since Jan 2008
Reputation Points: 77
Solved Threads: 40
 

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

Evan M
Light Poster
42 posts since Sep 2007
Reputation Points: 11
Solved Threads: 5
 

This question has already been solved

Post: Markdown Syntax: Formatting Help
You