Advertisement

Suggestion: Null pointer reference - Null handle reference.

Started by October 25, 2010 04:10 PM
1 comment, last by _orm_ 14 years, 1 month ago
I have been working on an A* implementation for a class in angelscript and one thing that has been kicking my ass is thr lack of debug information that surrounds the null pointer reference. When I see that error, as a C++ programmer, I begin to think it is an error invoplving my code, when in fact it usually is caused by not preceeding a handle assignment with the @ sign.
Handle@ foo;Foo = Bar(); // null pointer reference raised


It would be nice if we had better debug info when this exception is raised because I have been spending more time tracking these bastards down than actual logic errors in the code.

Thanks,
Mike
Whenever the Execute() method returns asEXECUTION_EXCEPTION, you can get the stack trace and program location where the exception occurred. You can also go so far as enumerating the value of the local variables at the moment if you wish.

Here's a function I use to get the location of the exception:

void PrintException(asIScriptContext *ctx){	asIScriptEngine *engine = ctx->GetEngine();	int funcId = ctx->GetExceptionFunction();	const asIScriptFunction *function = engine->GetFunctionDescriptorById(funcId);	printf("func: %s\n", function->GetDeclaration());	printf("modl: %s\n", function->GetModuleName());	printf("sect: %s\n", function->GetScriptSectionName());	printf("line: %d\n", ctx->GetExceptionLineNumber());	printf("desc: %s\n", ctx->GetExceptionString());}


A more complete example, which enumerates the stack and the variables:

void PrintVariables(asIScriptContext *ctx, int stackLevel){	asIScriptEngine *engine = ctx->GetEngine();	int typeId = ctx->GetThisTypeId(stackLevel);	void *varPointer = ctx->GetThisPointer(stackLevel);	if( typeId )	{		print(" this = 0x%x\n", varPointer);	}	int numVars = ctx->GetVarCount(stackLevel);	for( int n = 0; n < numVars; n++ )	{		int typeId = ctx->GetVarTypeId(n, stackLevel); 		void *varPointer = ctx->GetAddressOfVar(n, stackLevel);		if( typeId == engine->GetTypeIdByDecl("int") )		{			print(" %s = %d\n", ctx->GetVarDeclaration(n, stackLevel), *(int*)varPointer);		}		else if( typeId == engine->GetTypeIdByDecl("string") )		{			CScriptString *str = (CScriptString*)varPointer;			if( str )				print(" %s = '%s'\n", ctx->GetVarDeclaration(n, stackLevel), str->buffer.c_str());			else				print(" %s = <null>\n", ctx->GetVarDeclaration(n, stackLevel));		}	}}void ExceptionCallback(asIScriptContext *ctx, void *param){	asIScriptEngine *engine = ctx->GetEngine();	int funcID = ctx->GetExceptionFunction();	const asIScriptFunction *function = engine->GetFunctionDescriptorById(funcID);	print("--- exception ---\n");	print("desc: %s\n", ctx->GetExceptionString());	print("func: %s\n", function->GetDeclaration());	print("modl: %s\n", function->GetModuleName());	print("sect: %s\n", function->GetScriptSectionName());	int col, line = ctx->GetExceptionLineNumber(&col);	print("line: %d,%d\n", line, col);	// Print the variables in the current function	PrintVariables(ctx, -1);	// Show the call stack with the variables	print("--- call stack ---\n");	for( int n = 0; n < ctx->GetCallstackSize(); n++ )	{		funcID = ctx->GetCallstackFunction(n);		const asIScriptFunction *func = engine->GetFunctionDescriptorById(funcID);		line = ctx->GetCallstackLineNumber(n,&col);		print("%s:%s:%d,%d\n", func->GetModuleName(),		                       func->GetDeclaration(),							   line, col);		PrintVariables(ctx, n);	}}

AngelCode.com - game development and more - Reference DB - game developer references
AngelScript - free scripting library - BMFont - free bitmap font generator - Tower - free puzzle game

Advertisement
That helps alot! Thanks.

This topic is closed to new replies.

Advertisement