Thread overview
problem with Access Violation, and I'm not sure where
Jun 11, 2014
Matt
Jun 11, 2014
Matt
Jun 11, 2014
Mike Parker
June 11, 2014
I previously asked this over in the DigitalMars.D board(http://forum.dlang.org/thread/huftyrtbomaimuqkmkmy@forum.dlang.org#post-hrqvqlzzbkgafvjdtjnb:40forum.dlang.org), but it was suggested I ask it over here instead.

I have the following code and, despite trying to pinpoint it with try-catch blocks, I keep getting an Access Violation, and I'm really not sure where.

I'm using dub to build, but I've tried manually building as well, and I just get the same problem comes up.

The only module in the program is as follows (with the wrapper modules ripped out, so it's straight calls);

---

module app;

// own modules
import derelict.sdl2.sdl;

// standard library modules
import std.json;
import std.file;
import std.conv;
import std.stdio;
import std.datetime;

void main (string[] args){
	DerelictSDL2.load();
	if (!DerelictSDL2.isLoaded) assert(0, "DerelictSDL2 failed to load!");
	SDL_Init( SDL_INIT_EVERYTHING );
	
	scope(exit) SDL_Quit();
	
	SDL_Window* window;
	JSONValue cfg;
	
	try{
		cfg = loadConfig;
		
		auto width = to!int (cfg["window"]["width"].integer);
		auto height = to!int (cfg["window"]["height"].integer);
		
		window = SDL_CreateWindow (cfg["window"]["caption"].str.ptr, SDL_WINDOWPOS_UNDEFINED, SDL_WINDOWPOS_UNDEFINED, width, height, SDL_WINDOW_SHOWN);
	}
	catch(Error e){
		writeln ("error occurred before timing variable setup");
		return;
	}
	
	// set up timing variables
	auto duration = TickDuration (cfg["graphics"]["ticks"].integer);
	
	auto current_tick = Clock.currAppTick;
	auto target_tick = current_tick + duration;
	auto target_margin = target_tick + (duration/2);
	
	// main loop
	try{
		int* 	keys;
		ubyte*	numkeys;
		
		while (true){
			SDL_PumpEvents();
			numkeys = SDL_GetKeyboardState (keys);
			
			if (keys[SDL_SCANCODE_LALT] && keys[SDL_SCANCODE_F4]){
				SDL_DestroyWindow (window);
				break;
			}
			
			current_tick = Clock.currAppTick;
			if (current_tick >= target_tick){
				// this way, it will wait for the next frame, if there is less than half the time to the next frame left
				if (current_tick < target_margin){
					// update graphics and physics
				}
				// prep for next tick
				target_tick += duration;
				target_margin += duration;
			}
			
		}
	}
	catch(Error e){
		writeln ("error occurred during main loop");
	}
}

---

If anyone can help me with this, I would very much appreciate it.
June 11, 2014
On Wednesday, 11 June 2014 at 02:59:40 UTC, Matt wrote:
> I previously asked this over in the DigitalMars.D board(http://forum.dlang.org/thread/huftyrtbomaimuqkmkmy@forum.dlang.org#post-hrqvqlzzbkgafvjdtjnb:40forum.dlang.org), but it was suggested I ask it over here instead.
>
> I have the following code and, despite trying to pinpoint it with try-catch blocks, I keep getting an Access Violation, and I'm really not sure where.
>
> I'm using dub to build, but I've tried manually building as well, and I just get the same problem comes up.
>
> The only module in the program is as follows (with the wrapper modules ripped out, so it's straight calls);
>
> ---
>
> module app;
>
> // own modules
> import derelict.sdl2.sdl;
>
> // standard library modules
> import std.json;
> import std.file;
> import std.conv;
> import std.stdio;
> import std.datetime;
>
> void main (string[] args){
> 	DerelictSDL2.load();
> 	if (!DerelictSDL2.isLoaded) assert(0, "DerelictSDL2 failed to load!");
> 	SDL_Init( SDL_INIT_EVERYTHING );
> 	
> 	scope(exit) SDL_Quit();
> 	
> 	SDL_Window* window;
> 	JSONValue cfg;
> 	
> 	try{
> 		cfg = loadConfig;
> 		
> 		auto width = to!int (cfg["window"]["width"].integer);
> 		auto height = to!int (cfg["window"]["height"].integer);
> 		
> 		window = SDL_CreateWindow (cfg["window"]["caption"].str.ptr, SDL_WINDOWPOS_UNDEFINED, SDL_WINDOWPOS_UNDEFINED, width, height, SDL_WINDOW_SHOWN);
> 	}
> 	catch(Error e){
> 		writeln ("error occurred before timing variable setup");
> 		return;
> 	}
> 	
> 	// set up timing variables
> 	auto duration = TickDuration (cfg["graphics"]["ticks"].integer);
> 	
> 	auto current_tick = Clock.currAppTick;
> 	auto target_tick = current_tick + duration;
> 	auto target_margin = target_tick + (duration/2);
> 	
> 	// main loop
> 	try{
> 		int* 	keys;
> 		ubyte*	numkeys;
> 		
> 		while (true){
> 			SDL_PumpEvents();
> 			numkeys = SDL_GetKeyboardState (keys);
> 			
> 			if (keys[SDL_SCANCODE_LALT] && keys[SDL_SCANCODE_F4]){
> 				SDL_DestroyWindow (window);
> 				break;
> 			}
> 			
> 			current_tick = Clock.currAppTick;
> 			if (current_tick >= target_tick){
> 				// this way, it will wait for the next frame, if there is less than half the time to the next frame left
> 				if (current_tick < target_margin){
> 					// update graphics and physics
> 				}
> 				// prep for next tick
> 				target_tick += duration;
> 				target_margin += duration;
> 			}
> 			
> 		}
> 	}
> 	catch(Error e){
> 		writeln ("error occurred during main loop");
> 	}
> }
>
> ---
>
> If anyone can help me with this, I would very much appreciate it.

Apparently, it's in another part of my code, and I was doing something extremely silly. Ah well, live and learn.
June 11, 2014
On 6/11/2014 2:14 PM, Matt wrote:

>>
>>         window = SDL_CreateWindow (cfg["window"]["caption"].str.ptr,
>> SDL_WINDOWPOS_UNDEFINED, SDL_WINDOWPOS_UNDEFINED, width, height,
>> SDL_WINDOW_SHOWN);

I'm curious -- does cfg[""][""].str ensure that the string is null terminated? Because if it doesn't, you've got a potential problem here (aside from the fact that you aren't checking the return value).