www.digitalmars.com         C & C++   DMDScript  

digitalmars.D.learn - problem with Access Violation, and I'm not sure where

reply "Matt" <webwraith fastmail.fm> writes:
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.
Jun 10 2014
parent reply "Matt" <webwraith fastmail.fm> writes:
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.
Jun 10 2014
parent Mike Parker <aldacron gmail.com> writes:
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).
Jun 11 2014