ArgumentException: You have already claimed the path with that object

I noticed that I’m getting this error on pooled objects, everything works fine without errors with a fresh object. I’m using a version of the AIPath.cs with some extras bolted on to hook up to own systems.

So I suspect there are some things related to Astar that I’ve have missed to take into considering when pooling an object?

The exception is continuously being raised when canSearch is true, i.e. this is the search path code:

` /** Requests a path to the target */
public override void SearchPath ()
{

    if (target == null) throw new System.InvalidOperationException ("Target is null");
    
    lastRepath = Time.time;

    //This is where we should search to
    Vector3 targetPosition = target.position;
    
    if (restrictTo2D==true) targetPosition.y=transform.position.y;

    canSearchAgain = false;

    //We should search from the current position
    seeker.StartPath (GetFeetPosition(), targetPosition);
}`

Hi

That is being thrown when there is something wrong with the pooling.
Take a look at this page: http://arongranberg.com/astar/docs/pooling.php

Thanks Aron, I read through the pooling stuff and it seems to have been very helpful as I seem to have managed to fix the issue. Kudos for the ultra quick reply! :slight_smile:

Enabling some more of that debug stuff I get this:

Log At Start:

`Pool Is Leaking. See list of claims:
- Claim 1 is by a AstarPath
- Claim 2 is by a Seeker
- Claim 3 is by a CustomAIPath`

Log when the Exception is thrown on a pooled Object during runtime:

`ArgumentException: You have already claimed the path with that object

Pool Is Leaking. See list of claims:
- Claim 2 is by a Seeker
- Claim 3 is by a CustomAIPath`

So looking closer inside AIPath.cs I saw this( warning I do not have university degree in computer science =O).

It seems to claim the new path before releasing the old path in the OnPathComplete function, generating the error:

`public virtual void OnPathComplete (Path _p) {
ABPath p = _p as ABPath;
if (p == null) throw new System.Exception (“This function only handles ABPaths, do not use special path types”);

	canSearchAgain = true;
	
	//Claim the new path
	p.Claim (this);
	
	// Path couldn't be calculated of some reason.
	// More info in p.errorLog (debug string)
	if (p.error) {
		p.Release (this);
		return;
	}
	
	//Release the previous path
	if (path != null) path.Release (this);
	
	//Replace the old path
	path = p;

`

And changing it into this removed the error:

`public virtual void OnPathComplete (Path _p) {
ABPath p = _p as ABPath;
if (p == null) throw new System.Exception (“This function only handles ABPaths, do not use special path types”);

	canSearchAgain = true;

// Path couldn't be calculated of some reason.
// More info in p.errorLog (debug string)
if (p.error==false) 
    {
        //Release the previous path
        if (path != null) path.Release (this);
        
        //Claim the new path
        p.Claim (this);

		//Replace the old path
		path = p;
    }

`

My change was to check the path for errors and releasing the old path before claiming the new, while the old code claimed it directly…

All seems good here…at least right now… was this the right approach or did I do something crazy? =O

Ok… that could only have done anything relevant if the new path object was the same instance as the old object, and that should only have happened if the previous path had been released previously (which we know it was not)… so I have no idea why that works…
Sorry, I don’t really know.
I guess if it works you can continue with it, but I don’t think that was the correct solution…

Hmm… in this particular instance I had only one game object that was recycled…and respawned again…hmm but I guess I’ll just go with the fix for now instead of digging deeper as everything seems to be working fine with he change