I am getting a really weird unhandled exception that just doesn't make sense to me. I've annotated my code so hopefully it will make sense to you aswell.
Any help on this would and will be appreciated :)

void DarkGDK( )
{
	dbSyncOn( );
	dbSyncRate( 60 );

	CBaseEntity *ent = CreateEntity( "sp_camfollower" );

        //ent is valid here, in the calls to Precache and Spawn
	ent->Precache( );
	ent->Spawn( );

	while( LoopGDK( ) )
	{
		G_RunFrame( );

                //i get an unhandled exception here, at the call to Update
		ent->Update( );

		dbControlCameraUsingArrowKeys( 0,1,2 );
		dbSync( );
	}

	ent->Destroy( );

	return;
}

Yes, I stepped through the debugger, that's how I found out when ent was valid :)

Here is the code inside Update:

void C_CameraFollower::Update( )
{
	if( edict.position.DistanceTo( camera_position ) < 3 )
	{
		state = CFS_WAITING;
		SetThink( ( thinkfunc_t )&C_CameraFollower::WaitThink );
		SetNextThink( g_globals.curtime + 2.0f );
		return;
	}
	
	state = CFS_FOLLOWING;
	SetThink( ( thinkfunc_t )&C_CameraFollower::FollowThink );
	SetNextThink( g_globals.nexttick );
}

So when you step through the debugger, were you able to goto your update function?
If so, then where did it stop in there?

I just found out that ent get's invalidated after the call to Spawn, but I have no idea why. This is the code in Spawn:

void C_CameraFollower::Spawn( )
{
	dbShowObject( cur_model );
	SetModel( "box.x" );
	state = CFS_FOLLOWING;
}

Ok, so now one of those lines is causing it. Now try to step into line 3 and see what
happens, similarly to line 4 as well. What does showObject and setModel look like?

dbShowObject is part of the DarkGDK game engine that i'm using, and it's part of a DLL so i can't look at the code, but this is my SetModel function:

void SetModel( char *filename )
	{
		for( int i = 0; i < models.Count( ); i++ )
		{
			if( !strcmp( models[i]->filename, filename ) )
			{
				cur_model = models[i]->id;
				return;
			}
		}

		//late precache
		PrecacheModel( filename );
		cur_model = models.Front( )->id;
	}

and this is the PrecacheModel function:

int i = FindModelIndex( );

		dbLoadObject( filename, i );
		dbHideObject( i );
		
		model_t *m = ( model_t* )malloc( sizeof( model_t ) );
		m->id = i;
		m->filename = filename;

		models.Append( m );

models is a UtlVector, which is basically just a wrapper for std::vector, so Append does the same as push_back

I have done a deeper analysis of when ent becomes invalid and it's valid all through the call to Spawn, but right after that, even though no lines of code has been run(i put a breakpoint) it gets invalid, which really makes no sense.

I just restarted my computer, and without any changes to the code, it worked fine. But then after I ran the code a second time, again without any changes to the code, it gave me that exception again.. super confusing

>> I am getting a really weird unhandled exception
What is the exact error message? (there are many exceptions)

Are you using Visual Studio?

I'm using Visual Studio 2008 C++

This is the exception:
Unhandled exception at 0x004ce726 in whatt.exe: 0xC0000005: Access violation reading location 0x00001b5e.

I just restarted my computer, and without any changes to the code, it worked fine. But then after I ran the code a second time, again without any changes to the code, it gave me that exception again.. super confusing

I'd say that something is wrong with the memory management. After ent->Spawn() has the address of the pointer changed or is it still the same?

<< I'm using Visual Studio 2008 C++

From the Visual Studio's menu, can you find an option titled
Debug / Exceptions

If you can, then select it, and in the dialog box that opens, select the option;

Win32 Exceptions
..... c0000005 access violation

(I'm looking at VS 2005 at the moment, so your GUI may be different)

After that, just run the game in the debugger and once the access violation occurs, you should be able to locate the code that's causing the exception. See how it goes.

<< I'm using Visual Studio 2008 C++

From the Visual Studio's menu, can you find an option titled
Debug / Exceptions

If you can, then select it, and in the dialog box that opens, select the option;

Win32 Exceptions
..... c0000005 access violation

(I'm looking at VS 2005 at the moment, so your GUI may be different)

After that, just run the game in the debugger and once the access violation occurs, you should be able to locate the code that's causing the exception. See how it goes.

The debugger itself in 2K8 and onwards will give you the line the exception is thrown on and the stack trace will show you what's been called up to that point.

The problem, in my opinion, is that his pointer is either A) Being re-assigned, B) Being over-written C) Has a pointer overlap problem where something else has partially over-written the memory.

I'm not 100% sure but looking at the thread that's the only explanation I can think of as people have tried most of everything else.

Edited 6 Years Ago by Ketsuekiame: n/a

Comments
A good catch.

>> The debugger itself in 2K8 and onwards will give you the line the exception is thrown
>> on and the stack trace will show you what's been called up to that point.
Actually quite so, I think I was on autopilot - sorry. The above method would do the job when first-chance exceptions would be needed - but in this case, it's not of practical use.

It says the unhandled exception is happening at the closing } in Spawn for some reason

oh wait, it's changed it's mind, it's now saying it's happening at the call to ent->Update

i looked again while debugging and:

at the calls to CreateEntity and ent->Precahce, ent's address is 0xcbaf4c0
but after the call to ent->Spawn it's address is 0x00001b5e

could this be the cause of the problem?

sorry for the triple post btw

Edited 6 Years Ago by tomtetlaw: n/a

Ok, After which function are you *sure* the pointer changes? Make sure you check it after each function call. The function after which it changes is the problematic one.

it changed right after the spawn function FINISHES, i've checked and it's valid all through that function, then when the next break point is hit(the line of code directly after) it get's changed

It's a bit of a big step, but can you upload the code and required libraries somewhere so I can try and compile and debug it myself? I can't think of anything to do remotely, but sometimes when it's in front of you, you have flashes of inspiration and see things you miss before :)

I'll have to look at this when I get home from work then.

Just looking at the code I can't see anything except perhaps that when you spawn the camera using the constructor, the memory there is being corrupted or lost somehow.

Unless someone here knows the GDK better, I'm afraid I won't be much more help till I can get home

it's okay :)
could my problam be the use of malloc when i create new models in PrecacheModel ?

This article has been dead for over six months. Start a new discussion instead.