{"record":{"id":"7449f3aa2e98fd94","repo":"stride3d/stride","slug":"could-not-find-a-store-operation-right-after-interop-pin","errorCode":null,"errorMessage":"Could not find a store operation right after Interop.Pin","messagePattern":"Could not find a store operation right after Interop\\.Pin","errorType":"exception","errorClass":"InvalidOperationException","httpStatus":null,"severity":"error","filePath":"sources/core/Stride.Core.AssemblyProcessor/InteropProcessor.cs","lineNumber":280,"sourceCode":"        else if (nextStoreInstruction.OpCode == OpCodes.Stloc_2)\n        {\n            variableIndex = 2;\n        }\n        else if (nextStoreInstruction.OpCode == OpCodes.Stloc_3)\n        {\n            variableIndex = 3;\n        }\n        else if (nextStoreInstruction.OpCode == OpCodes.Stloc_S)\n        {\n            variableIndex = ((VariableReference)nextStoreInstruction.Operand).Index;\n        }\n        else if (nextStoreInstruction.OpCode == OpCodes.Stloc)\n        {\n            variableIndex = ((VariableReference)nextStoreInstruction.Operand).Index;\n        }\n        else\n        {\n            throw new InvalidOperationException(\"Could not find a store operation right after Interop.Pin\");\n        }\n\n        // Transform variable from:\n        //   valuetype Struct s\n        // to:\n        //   valuetype Struct& modopt([mscorlib]System.Runtime.CompilerServices.IsExplicitlyDereferenced) pinned s,\n        var variable = method.Body.Variables[variableIndex];\n        variable.VariableType = variable.VariableType\n            .MakeByReferenceType()\n            //.MakeOptionalModifierType(typeof(IsExplicitlyDereferenced))\n            .MakePinnedType();\n\n        // Remove call to Interop.Pin:\n        ilProcessor.Remove(pinToPatch);\n\n        // Transform all ldloca with this variable into ldloc:\n        for (int index = 0; index < ilProcessor.Body.Instructions.Count; index++)\n        {","sourceCodeStart":262,"sourceCodeEnd":298,"githubUrl":"https://github.com/stride3d/stride/blob/96fad776d210c221682aac1ccdf4c79dc046fc38/sources/core/Stride.Core.AssemblyProcessor/InteropProcessor.cs#L262-L298","documentation":"ReplacePinStructGeneric expects an Interop.Pin to be followed by a store instruction (stloc) that captures the pinned result; when the next instruction is not a recognized store, it throws this InvalidOperationException since the pinned-variable transform cannot proceed.","triggerScenarios":"PatchMethod processing Interop.Pin on a generic struct where the IL after the pin call does not store the result into a local (result discarded, used inline, or compiler reordered the code).","commonSituations":"Discarding the return of Interop.Pin instead of assigning it; expression-level usage where the result is consumed on the stack directly; debug vs release IL differences altering store placement.","solutions":["Assign the Interop.Pin result to a local variable so a stloc follows the call","Rewrite single-expression usage into explicit statement form (var h = Interop.Pin(...))","Match the pattern used by Stride's own samples for pinning generic structs","Update Stride if your compiler emits a pattern the processor doesn't recognize"],"exampleFix":"// before\n_ = Interop.Pin(myStruct, out handle); // result not stored to tracked local\n// after\nvar pinned = Interop.Pin(myStruct, out handle); // followed by stloc","handlingStrategy":"validation","validationCode":"// ensure the pin result is assigned: `var pinned = Interop.Pin(x, out h);` guarantees a stloc follows","typeGuard":null,"tryCatchPattern":"catch (InvalidOperationException ex) when (ex.Message.Contains(\"store operation right after Interop.Pin\"))\n{\n    // assign the pin result to a local variable and retry the build\n}","preventionTips":["Never discard the Interop.Pin return value","Write explicit statements rather than inline expressions when pinning","Compare against Stride sample IL patterns for generic struct pinning"],"tags":["il-weaving","interop","pinning","generics"],"backgroundTag":"internal-invariant-violation","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"}