It crashed in LeafQuadNode::GetAllInSphere on the function all to m_vObjects[n]->InSphere.   This indicates that an invalid object is still existing somewhere.  I must be forgetting to call isValid on a SmartPointer somewhere?

I found the problem.  In the SurvivalEngine::Update function when ever we encounter a bug in the vector of Objects, we have to remove that bug from the object map and then reinsert it.  I was removing it after I changed the bug's position, so sometimes the remove function would not find the bug but it would reinsert it.  Once the bug was deleted from memory, an invalid pointer would be left laying around on a leaf node.
I fixed this problem by removing the bug from the object map and then calling it's update function and then reinserting it into the object map.

8/8: Things to do.
First "fix" the water pools so that they are actually a pool and not just some water sitting around on the map

Second, find out why the children always move around in a circle.

The water is done and the bugs where moving in circles because I forgot to copy the memory over from the genotype into the actual structs.


8/10:
I just started to implement the fighting stuff.  I got a decent spitting effect using a particle system.  I started to write the stuff to cause the bugs to go into a fighting state and choose the target and all that, but now it is crashing.
I used the logger to find out that they are in the Partol state and then the program crashes.  I ran the debugger to "go" and it stopped on a line in the string class.  I am guessing that it has something to do with the function string GetSpecies.

I ran it again with one bug of each species starting right next to each other.  This time one of them entered into the fighting state and stayed there for about 5 seconds.  When it crashed I consulted the log and it crash sometime during the loop that checks the bug's species.

I ran it again with logs inside the loop at the begin and end.  It never made it into the if statement.  I guess that something is wrong with the species check thing.

There must be a problem with the GetSpecies function.  It is crashing during the if part of the second if that checks the species.

There seems to be a problem when I reinterpret cast the SmartPointer to a bug.
I used the SmartPointer::Get function instead of & operator and that seems to have fixed the problem.

Now, after I got the particle system into place and got the particles going in the correct direction and everything, I implemented a DoDamage function for the bug class that does damage to this bug.  It is crashing now.
I think it has something to do with the reinterpret_cast of the m_spTarget in Bug.

I tried calling reinterpret_cast each time I wanted to refer to the bug, but that is not the problem.  I also "go"ed but it stops on some dissembly window.

When I "Run to Cursor" with the cursor inside Bug::DoDamage, it never makes it that far.  The problem has to be before that??? but it is inside the fight state.

It seems like the problem was in using &m_spTarget and not m_spTarget.Get().  I really don't know why that is?  I guess that the compiler is creating a pointer to a SmartPointer and not returning the stored object pointer.

Well, the fighting works fine, but there is still the mysterious crash.  It happened once and stopped on a line if plant.  This time it happened in LeafQuadNode::GetAllInSphere.

8/11:  I am going after a mysterious crash that seems to happen at some random time.  Based on the log that I set up to start over during each update it happens when I am updating the objs.

It crashed in the Object == operator.  Looking at the log I set up, it seems to have crashed on a bug, but before it got into the bug's update function.

It crashed in the object == operator again.  Looking at the log, it crashed during the remove function that takes it out of the quadtree.  I see that the current obj is fine because the get type works fine.  There must be a problem with the quad tree, but I tested that and that means that there must be something that I am doing wrong with inserting and removing objects.

This time it crashed on the SmartPointer::Delete, and the log shows no objects where deleted.  I'm just going to run it again and see what happens.

This time it crashed in the Object::operator==, and the log shows no Objects where deleted.  I have no idea what do to from here.  Actually, I just found that I forgot to not open and close the logger in SurvivalEngine::Update.  So I am going to do this again.

It crashed in the Object::operator==.
It crashed in SmartPointer::Delete.

I am going back to using the logger to trace through the execution of the program.  I don't like doing this because an object could be deleted and then cause a problem during the next update.

OK, I am going to do this the hard way.  I am not going to seed the random number generator and then I can track this bug down faster.  All that I can hope for is that it will crash very soon after I start the program.

I seeded the random number generator with 2 and it crashed in less than a minute.  Now lets track this damn bug down.  It crashed in Object::operator==.

Step one: Figure out which update it is crashing on.
	It crashed on update number 1293.

Step two: Break on the update before it and see what happens.
Now it seems to be crashing one sooner each time.  ummm.....
You are going to think that I am crazy but I told it break on update 1293 and it crashed on update 1292, so I told it to break on update 1292 and it crashed on update 1291, so I told it to crash on update 1290 and it crashed on update 1289.

I finally got it to stop on update 1292.  I think that it had something to do with the fact that I am using time to update my bugs.  I need to remove that variable, an replace it with a constant amount of time.

I set elapsedSec to .5 and for some reason it did not crash?  I then set elaspedSec to .05 and it crashed on update 344.

It crashed during the update when n was equal to 25.
It crashes on the line m_qtObjectMap.Remove( m_vecObjects[n] ); when n = 25 and m_uiUpdateNum = 345.

It crashed in LeafQuadNode::Remove on the first iteration of the loop to find a match.  It is reporting an access violation.  I am going to go back to update 344 and see what is going on.

It seems to be reinserting the obj just fine after it removes it.  There has to be a problem with the SmartPointers.

I told the program to break in SmartPointer::Destructor on the line just under the line that checks the reference count.  No memory is being deleted.

I told the program to break in SmartPointer::Delete and it didn't break which means that it is not reaching that point in the program.  This means that there is problem in creating something somewhere.  But if that is the case, then why didn't the program crash earlier.  The problem is that it is referencing bad memory in LeafQuadNode.  Something is happening before that which is causing the memory in LeafQuadNode to be invalid.

I'm going to try changing the Object::operator== to take an Object* as it's left hand operand.  It didn't help.

In the error that I get when it crashes is that this cannot be evaluated.  One must ask one's self, just what is this, the object being deleted or the object stored in the LeafQuadNode?

The bad memory is memory stored in LeafQuadNode.  I am going to have LeafQuadNode reference the memory when it is being inserted, that way I can find out just which object is kludging things up.

I added a line in LeafQuadNode::Insert that gets the id of the object being inserted and it still crashed on the remove function. @#!$#  That can only mean that sometime after it is inserted, but before it is removed again, the memory is being deleted.

This is really confusing.  If it cannot evaluate a function in object, then why doesn't crash before it even gets into object?  The memory must be valid for it to even get into that function, right?  So there has to be some other problem.  Also, I put log statements at each place in SmartPointer where it deletes target, and there where none logged, which means that it is not deleting anything.

I changed SmartPointer::operator== to return true if the addresses of target where the same and ran it.  It crashed at the same time but in the LeafQuadNode::GetAllInSphere.  Somewhere or another there is bad memory getting in the quadtree.  I am only inserting valid memory, I'm not deleting any memory, and the object's don't delete them selfs.  The only possible solution is that at some point, some memory is going out of scope or something, and that causes a SmartPointer to point to memory that is no longer allocated.  This must be why it is crashing.  Now all I have to do is find out where this is happening.

After consulting the log, I see that on the 344th update, the quadtree remove function did not find a match.  It then went on to update 359 before it crashed.  There is bad memory getting into the quadtree.  I know that I can't be putting it in because there is only like 3 places in the code that I put obj's into the tree, and two places that I remove them from.  One of each of those places being during the update for a bug, because the bug moves and you have to remove it, move it, and then reinsert it.  The million dollar question now is, where is this memory going bad at?
- SmartPointer::Delete is not called before the crash
- No new objects are inserted before the crash
- My copy constructor is being called for SmartPointer
- None of the places in SmartPointer where target is being deleted are reached

I am going to try removing the fighting state check in Bug::CheckState.  Without the fighting state, it still crashes, much later on update 801, but it still crashes on the same line with seemingly the same problem.

New game plan.
Step one: Find the index of the LeafQuadNode where the problem is happening
On the update of the crash, index = 2851 and obj = 35
On the update before it removed from node 2851 and obj = 35

Step Two: Watch that node and keep track of each object that is inserted or removed.
35 is in the resting state.  Maybe it is a target.
35 is reinserted into node 2851, the same node it was removed form

STep Three: Find which object the problem is happening to
35

Step Four: Find all points of interaction between that object and leafnode
I added a line in Bug::Update 
	if( m_spTarget.isValid )
	{
		if( m_spTarget->GetId() == 35 )
		{
			int NotUsingThisVariable = 0;
		}// End if
	}// End if

It seems that 29 is targeting 35, and 35 is in the fighting state.

Update 342:
35 resting
29 patroling

Update 343:
35 resting
29 patroling

Update 344:
crash

Inserted 35 into node 2851

8/12: Still going.

The error is CXX0030: Error: expression cannot be evaluated

This web site I found says that that error is caused when you try to reference something that is a pointer to null.  This sounds possible, but I don't know when I could be setting one of my SmartPointers back to NULL.

"As for the "cannot evaluate expression", that means that the variable is either out of scope, or has no valid value at that point in the code. Make sure that you aren't overstepping the bounds of the arrays that you're using, the pointers are all valid, etc..."

The objects are going invalid inside the quadtree.  The object returned 3452698624 as it's id number.  This is strange.  I am thinking that somewhere in my code, a pointer is being moved to another address, which is valid memory.

I wrote a LogAll function that prints the id of each object stored in the quadtree.  After update 801 there is a new object with the id of 3452698624.  All the other objects are still there in each update.  Somewhere there has to be a new object being created with default values or something and it is being put into the quadtree.  The default unsigned int value is 3435973836, so we know that somewhere the unsigned int is being set.

During update 800, there is no object 3452698624.  During update 801, there is, but only in the quadtree, not in the vector of objects.  Something is going wrong in between the time that we remove object 22 and then reinsert it.  During the next update there is an extra update.

I just realize that during the update in which 22 is not found, the object with the id 3452698624 appears and 22 does not.

Something is happening during update 801.  Before the update object 22 is in the quadtree and after update 801, object 3452698624 is there.  On the next update it comes back again.

During update 801 I logged all after each update.  After n = 14 the mysterious 3452698624 is there.  Since I logged all before the update, then that means that when n equaled 13, something happened.

The bug in the 13 position of the vector of objects is eating.  It did a check state which involved another bug, and a plant.  It then went into the eating state. I forgot the check the MoveTowardTarget.

Directly after the object is finished updating, the LogAll function shows 3452698624 and not 22.

The problem is being caused during the update function.  It must have something to do with the QuadNode::GetAllInSphere.

Finally, I found it.  In the Update function, when ever there was a target in the eating or drinking state, I reinterpret_cast'ed on the return value of SmartPointer::operator&.  I had this problem several days ago in the fighting state and I solved it by using SmartPointer::Get.  I changed it and now it works fine.

8/16:  We need to decide on a key mapping scheme.  What keys on the keyboard or mouse buttons/movements control what aspects of the game.

I want the mouse to control all movements of the bug.  Moving the mouse left or right could turn the bug left or right.  Holding down the left mouse button could cause the unit move forward.  We still have the right mouse button.

A list of all states the unit can be in.
Fighting.
Patroling.
Eating.
Drinking.
Reproducing.
Resting.

Any time the unit is moving and we are not indicating another state, the bug is in the patroling state so we don't need a button for that state.  Anytime the bug is not moving and not in any other state, the bug in is a resting state.  The only states are:
Fighting    = F
Eating      = E
Drinking    = D
Reproducing = R

When we press any of the buttons a targeter will appear that will show the user the closest target for the given state.  If there are no valid targets in site then an error will be displayed informing the user that there are no valid targets in site.  If there is a valid target then the targeter will indicate it to the user and a calculation will be preformed setting the corresponding value in the bugs thresholds.  The user will have to hold down the right mouse button to spit on the target.  The attack will only actually damage the target if it is in range and in front of the target.  If the bug does not have enough energy or is not fertile enough to have a kid then there will be an error message informing the player.  