www.digitalmars.com         C & C++   DMDScript  

digitalmars.D - Getting an access violation before main starts

reply "Matt" <webwraith fastmail.fm> writes:
I was wondering if anyone could help with a problem I'm having.

My program compiles properly, and has all up-to-date files and 
DLLs (SDL2, SDL2-image, SDL2-ttf, all the other DLLs that are 
required by these). However, when I run it, I get "object.Error: 
Access Violation", which, of course, means something isn't 
getting created.

I'm using dub to build (although I get the same problem when 
trying to build manually).

My main program looks like this:

---


void main (string[] args){
	Window window;
	JSONValue cfg;
	Input input;
	
	try{
		window = new Window ();
		cfg = loadConfig;
		
		window.create (cfg["window"]["caption"].str, to!int 
(cfg["window"]["width"].integer), -1, -1, to!int 
(cfg["window"]["height"].integer), 
(cfg["window"]["windowed"].type == JSON_TYPE.FALSE) ? true : 
false );
		window.releaseMouse;
		
		input = window.getInput ();
	}
	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{
		while (!window.is_dead){
			auto keys = input.getKeyboardState ();
			
			/// TODO: there is a range error occurring in keys.keys when 
holding down left alt. Check for other modifier keys
			if (/*(keys.mods & 
KeyMods.LeftAlt)*/keys.scans[ScanCode.LeftAlt] && 
keys.scans[ScanCode.F4]){
				window.destroy;
				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");
	}
}

---

Both Window and Input are my own classes, which I've checked for 
leaks, and don't appear to have any. They are basically wrappers 
around SDL functionality. I can include the source if people 
want, but it's pretty long.

The try-catch blocks were put in to see if I could track down 
which part of the code the error's occurring in, which is why I 
believe it to be happening before main() is called.

I'd appreciate any help that you guys can give, and many thanks 
in advance.
Jun 10 2014
next sibling parent "safety0ff" <safety0ff.dev gmail.com> writes:
On Wednesday, 11 June 2014 at 00:00:47 UTC, Matt wrote:
 Both Window and Input are my own classes, which I've checked 
 for leaks, and don't appear to have any. They are basically 
 wrappers around SDL functionality. I can include the source if 
 people want, but it's pretty long.

 The try-catch blocks were put in to see if I could track down 
 which part of the code the error's occurring in, which is why I 
 believe it to be happening before main() is called.

My shot in the dark is: the SDL wrappers are trying to load the DLL in the module constructor (static this) and something is causing them to fail (perhaps the DLLs aren't found.) Just a guess, good luck.
Jun 10 2014
prev sibling next sibling parent "Matt" <webwraith fastmail.fm> writes:
On Wednesday, 11 June 2014 at 00:05:25 UTC, safety0ff wrote:
 On Wednesday, 11 June 2014 at 00:00:47 UTC, Matt wrote:
 Both Window and Input are my own classes, which I've checked 
 for leaks, and don't appear to have any. They are basically 
 wrappers around SDL functionality. I can include the source if 
 people want, but it's pretty long.

 The try-catch blocks were put in to see if I could track down 
 which part of the code the error's occurring in, which is why 
 I believe it to be happening before main() is called.

My shot in the dark is: the SDL wrappers are trying to load the DLL in the module constructor (static this) and something is causing them to fail (perhaps the DLLs aren't found.) Just a guess, good luck.

I'm not sure how it could be? The DLLs are sat in the same directory as the executable. Not only that, but (if you're talking about my Window and Input classes when you say "wrappers") I've tried putting the Derelict loading into the Window.create() function, which means it would be caught by the first try{} block in main(), which it isn't.
Jun 10 2014
prev sibling next sibling parent "Kapps" <opantm2+spam gmail.com> writes:
The digitalmars.D.learn newsgroup is a more appropriate place for 
this, but as for your issue, one thing to keep in mind is that 
static constructors are run before main:

import std.stdio;

static this() {
	writeln("Foo");
}

void main() {
	writeln("main");
}

$ rdmd test.d
Foo
main
Jun 10 2014
prev sibling next sibling parent "Matt" <webwraith fastmail.fm> writes:
On Wednesday, 11 June 2014 at 01:52:49 UTC, Kapps wrote:
 The digitalmars.D.learn newsgroup is a more appropriate place 
 for this, but as for your issue, one thing to keep in mind is 
 that static constructors are run before main:

 import std.stdio;

 static this() {
 	writeln("Foo");
 }

 void main() {
 	writeln("main");
 }

 $ rdmd test.d
 Foo
 main

Thank you for that, but I've tried moving everything out of all static constructors I used (one, in the Window module, which consisted of DerelictSDL2.load(); SDL_Init(SDL_INIT_EVERYTHING);, without any change to the problem.
Jun 10 2014
prev sibling parent Jacob Carlborg <doob me.com> writes:
On 11/06/14 02:00, Matt wrote:
 I was wondering if anyone could help with a problem I'm having.

 My program compiles properly, and has all up-to-date files and DLLs
 (SDL2, SDL2-image, SDL2-ttf, all the other DLLs that are required by
 these). However, when I run it, I get "object.Error: Access Violation",
 which, of course, means something isn't getting created.

Run it through a debugger to get a stack trace. -- /Jacob Carlborg
Jun 11 2014