{"record":{"id":"c29f80185b5b8527","repo":"stride3d/stride","slug":"item-belongs-to-another-prioritynodequeue","errorCode":null,"errorMessage":"Item belongs to another PriorityNodeQueue.","messagePattern":"Item belongs to another PriorityNodeQueue\\.","errorType":"exception","errorClass":"InvalidOperationException","httpStatus":null,"severity":"error","filePath":"sources/core/Stride.Core/Collections/PriorityNodeQueue.cs","lineNumber":115,"sourceCode":"        item.Index = -1;\n    }\n\n    /// <summary>Add an element to the priority queue - O(log(n)) time operation.</summary>\n    /// <param name=\"item\">The item to be added to the queue</param>\n    /// <returns>A node representing the item.</returns>\n    public PriorityQueueNode<T> Enqueue(T item)\n    {\n        var result = new PriorityQueueNode<T>(item);\n        Enqueue(result);\n        return result;\n    }\n\n    /// <summary>Add an element to the priority queue - O(log(n)) time operation.</summary>\n    /// <param name=\"item\">The item to be added to the queue</param>\n    public void Enqueue(PriorityQueueNode<T> item)\n    {\n        if (item.Index != -1)\n            throw new InvalidOperationException(\"Item belongs to another PriorityNodeQueue.\");\n\n        // We add the item to the end of the list (at the bottom of the\n        // tree). Then, the heap-property could be violated between this element\n        // and it's parent. If this is the case, we swap this element with the \n        // parent (a safe operation to do since the element is known to be less\n        // than it's parent). Now the element move one level up the tree. We repeat\n        // this test with the element and it's new parent. The element, if lesser\n        // than everybody else in the tree will eventually bubble all the way up\n        // to the root of the tree (or the head of the list). It is easy to see \n        // this will take log(N) time, since we are working with a balanced binary\n        // tree.\n        var n = items.Count;\n        items.Add(item);\n        item.Index = n;\n        while (n != 0)\n        {\n            var p = n / 2;    // This is the 'parent' of this item\n            if (comparer.Compare(items[n].Value, items[p].Value) >= 0)","sourceCodeStart":97,"sourceCodeEnd":133,"githubUrl":"https://github.com/stride3d/stride/blob/96fad776d210c221682aac1ccdf4c79dc046fc38/sources/core/Stride.Core/Collections/PriorityNodeQueue.cs#L97-L133","documentation":"PriorityNodeQueue tracks each PriorityQueueNode's Index so it can locate items in the heap. Enqueue throws InvalidOperationException('Item belongs to another PriorityNodeQueue.') when item.Index != -1, meaning the node is already enqueued (in this queue or another); a node can only live in one queue at a time.","triggerScenarios":"Enqueueing a PriorityQueueNode that is already in a queue (Index >= 0); reusing node instances across queues; enqueueing the same node twice after forgetting a Dequeue/Remove.","commonSituations":"Re-prioritizing an item by enqueueing a node still in the queue; pooling/sharing node objects across multiple queues; scheduler logic that re-adds tasks without removing them first.","solutions":["Remove the item from its current queue (Dequeue or Remove) before enqueueing it elsewhere","Update the priority in place if the queue supports it instead of re-enqueueing","Create a fresh PriorityQueueNode for each enqueue","Initialize/reset item.Index = -1 only after the node has truly left a queue"],"exampleFix":"// before\nqueue.Enqueue(existingNode); // throws if already in a queue\n// after\nif (existingNode.Index != -1)\n    queue.Remove(existingNode); // or use the queue's update API\nqueue.Enqueue(existingNode);","handlingStrategy":"validation","validationCode":"if (item.Index == -1) queue.Enqueue(item);","typeGuard":null,"tryCatchPattern":"try { queue.Enqueue(item); } catch (InvalidOperationException) { /* item already queued: remove first or update in place */ }","preventionTips":["Only enqueue nodes with Index == -1","Remove or update-in-place instead of re-enqueueing","Never share node instances across queues","Create a new PriorityQueueNode per item"],"tags":["csharp","collections","priority-queue","invalid-state"],"backgroundTag":"invalid-state-transition","analyzedSha":"96fad776d210c221682aac1ccdf4c79dc046fc38","analyzedAt":"2026-09-14T02:59:31.279Z","contentChangedAt":"2026-09-14T02:59:31.279Z","schemaVersion":2},"datasetVersion":"2026-09-15T23:17:13.987Z"}