Jump to content
  • Advertisement
Sign in to follow this  
parrotbait

Strange NULL goings on in game

This topic is 3973 days old which is more than the 365 day threshold we allow for new replies. Please post a new topic.

If you intended to correct an error in the post then please contact us.

Recommended Posts

Hi there, I've tried searching for the problem but wasn't sure exactly how to search for it so here goes. I have this snippet of code
Agent* targAgent = blackboard->GetDamagedBy();
if(targAgent != NULL )
{	if(targAgent->GetAIModule()->GetBlackboard()->GetHealth() > 0.0f)
	{	
		blackboard->SetTargetAgent(targAgent);
		blackboard->SetDamagedBy(NULL);
	}
}
The problem I'm having is that even though in the game my program is getting past the first if check(targAgent!= NULL), Visual Studio is breaking out at the second if statement with the error: 'Unhandled exception at 0x0048a066 in DominationGame.exe: 0xC0000005: Access violation reading location 0x000001a0'. This indicates to me that targAgent is NULL but how is it getting past the first if check?When I hover over the variable in debug mode in Visual Studio I see the variable has a memory location(think thats what it is?!) of 0x00000100 and everything in the targAgent pointer class is ????? or 'Expression cannot be evaluated'. This problem is wrecking my head as I cannot figure out why the first check is passing and then the second is failing, any tips or ideas would be great. Cheers, Eddie

Share this post


Link to post
Share on other sites
Advertisement
Hmm yeah I did, it comes up as 0x00000100 which isn't exactly NULL is it? 0x0000000 is NULL I thought and thats why the if is passing. However it still crashes out on the next line again, strange thing is that I've also set debug points whereever the SetDamagedBy(Agent* agent) kicks in and it isn't called anywhere yet targAgent is still not NULL.

Share this post


Link to post
Share on other sites
You have a few possible failures that are not catched by your
first if:

GetAIModule() - returns NULL (or a memory location that has been freed before)
GetBlackboard() - returns NULL (or a memory location that has been freed before)

if it's really the second if i can't think of any other error... (right now)

but maybe iam still wrong :)

PS: i knew i forgot something... the possibility of having a variable
not initialized

Share this post


Link to post
Share on other sites
Quote:
Original post by parrotbait
... strange thing is that I've also set debug points whereever the SetDamagedBy(Agent* agent) kicks in and it isn't called anywhere yet targAgent is still not NULL.


It seems like the wrong signuture for an uninitialised windows pointer (in debug mode which is normally 0xdddddddd), but do you set this value in a constructor or something?

Share this post


Link to post
Share on other sites
Yeah I'm going to put those in, its weird though this problem should never occur. will try it and post back if it works.

Share this post


Link to post
Share on other sites
My blackboard basically has a two methods in blackboard.h

Agent* GetDamagedBy(){ return damageEncurredBy; };
void SetDamagedBy(Agent* agent){ damageEncurredBy = agent; };


and I set the variable damageEncurredBy = NULL at the constructor of the blackboard.cpp.


Edit: Oh and even with the extra checks, the program is still breaking at the second if!!!!!


Agent* targAgent = ai->GetBlackboard()->GetDamagedBy();
if(targAgent != NULL && targAgent->GetAIModule() != NULL && targAgent->GetAIModule()->GetBlackboard() != NULL)
{ if(targAgent->GetAIModule()->GetBlackboard()->GetHealth() > 0.0f)
{
blackboard->SetTargetAgent(targAgent);
blackboard->SetDamagedBy(NULL);
}
}

Share this post


Link to post
Share on other sites
Is blackboard a validly constructed instance?
It could be a duff pointer that happens to work.

Assuming it is a valid instance, you could try putting this at the top:
blackboard.SetDamagedBy(NULL);
If it still passes the first if statement then something is happening between SetDamagedBy and GetDamagedBy (or as I said, it could be a dangling pointer to the blackboard class).

Share this post


Link to post
Share on other sites
Somewhere in your code you do this:
blackboard->SetDamagedBy(&m_pSomething);

That is, you set the damaged by agent to the address of a member of a class, don't you?
The class which m_pSomething (in my example) is a member of is NULL. So, the address of that member variable turns out to be 0x00000100, which then percolates through your code until you finally come to use it, where it all blows up.

So, look where you're setting the "damaged by" agent, and check that this isn't null there.

Share this post


Link to post
Share on other sites
Quote:
Original post by Evil Steve
Somewhere in your code you do this:
blackboard->SetDamagedBy(&m_pSomething);

That is, you set the damaged by agent to the address of a member of a class, don't you?
The class which m_pSomething (in my example) is a member of is NULL. So, the address of that member variable turns out to be 0x00000100, which then percolates through your code until you finally come to use it, where it all blows up.

So, look where you're setting the "damaged by" agent, and check that this isn't null there.



Yes thats what I was thinking so I set up a debug breakpoint in the actual SetDamagedBy method in the blackboard to test when it was being set. However the program broke out at the second 'if' even though it never broke for the SetDamagedBy method. So although it looks like its not being set anywhere else in the program but comes out as being 'set' for the if statements.

@dmatter, my blackboard reference is setup at the beginning and isn't NULL and is a correct instance of the blackboard. I tried what you said to do (i.e. set blackboard->SetDamagedBy(NULL) before this statement and indeed the program doesn't crash out any more. So it has to be getting set somewhere.

Appreciate all your help with this,will keep plugging away at it.

Share this post


Link to post
Share on other sites
Sign in to follow this  

  • Advertisement
×

Important Information

By using GameDev.net, you agree to our community Guidelines, Terms of Use, and Privacy Policy.

Participate in the game development conversation and more when you create an account on GameDev.net!

Sign me up!