{"record":{"id":"36269e25aaee6935","repo":"stride3d/stride","slug":"initializecontententity-has-already-been-invoked","errorCode":null,"errorMessage":"InitializeContentEntity has already been invoked.","messagePattern":"InitializeContentEntity has already been invoked\\.","errorType":"exception","errorClass":"InvalidOperationException","httpStatus":null,"severity":"error","filePath":"sources/editor/Stride.Assets.Presentation/AssetEditors/Gizmos/EntityGizmo.cs","lineNumber":55,"sourceCode":"\n        /// <summary>\n        /// Gets the associated scene entity.\n        /// </summary>\n        public Entity ContentEntity { get; private set; }\n\n        /// <summary>\n        /// Gets or sets whether this gizmo is currently selected.\n        /// </summary>\n        public virtual bool IsSelected { get; set; }\n\n        private IEditorGameComponentGizmoService gizmos;\n\n        private IEditorGameCameraService camera;\n\n        protected EntityGizmo(Entity contentEntity)\n        {\n            if (contentEntity == null) throw new ArgumentNullException(nameof(contentEntity));\n            if (ContentEntity != null) throw new InvalidOperationException(\"InitializeContentEntity has already been invoked.\");\n            ContentEntity = contentEntity;\n        }\n\n        public override void Initialize(IServiceRegistry services, Scene editorScene)\n        {\n            base.Initialize(services, editorScene);\n\n            if (GizmoRootEntity != null)\n                CollectComponentIds(GizmoRootEntity);\n\n            gizmos = Game.EditorServices.Get<IEditorGameComponentGizmoService>();\n            camera = Game.EditorServices.Get<IEditorGameCameraService>();\n        }\n\n        /// <inheritdoc/>\n        public virtual void Update()\n        {\n            if (ContentEntity == null || GizmoRootEntity == null || gizmos == null || camera == null)","sourceCodeStart":37,"sourceCodeEnd":73,"githubUrl":"https://github.com/stride3d/stride/blob/96fad776d210c221682aac1ccdf4c79dc046fc38/sources/editor/Stride.Assets.Presentation/AssetEditors/Gizmos/EntityGizmo.cs#L37-L73","documentation":"The protected EntityGizmo constructor assigns ContentEntity and guards that it has not been set before. Because ContentEntity is only assigned here, the guard 'InitializeContentEntity has already been invoked' fires when a gizmo's constructor is somehow executed twice for the same instance (chained constructors calling base twice is impossible in C#, so this surfaces mainly through reflection-based instantiation reusing or re-entering a partially initialized instance).","triggerScenarios":"Constructing the same gizmo instance path twice such that the guard sees a non-null ContentEntity — typically via Activator patterns that cache and re-invoke constructors, or a derived class calling a base constructor chain that assigns ContentEntity more than once.","commonSituations":"Custom gizmo frameworks that re-run construction on cached instances; refactors where a derived gizmo class invokes another constructor of itself that already set ContentEntity via instance state.","solutions":["Ensure each gizmo instance is constructed exactly once; create a new instance instead of re-running constructors.","In derived gizmos, make sure only one base constructor assigning ContentEntity is invoked per instance.","If using custom instantiation code, always call Activator.CreateInstance rather than reusing a half-initialized object."],"exampleFix":"// before\nvar gizmo = cachedGizmo;\ngizmo.Reinitialize(component); // constructor re-entered\n// after\nvar gizmo = (EntityGizmo<TComponent>)Activator.CreateInstance(gizmoType, component);","handlingStrategy":"validation","validationCode":"if (gizmo.ContentEntity != null)\n    throw new InvalidOperationException(\"Gizmo already initialized with a content entity.\");","typeGuard":"bool IsUninitialized(EntityGizmo g) => g.ContentEntity == null;","tryCatchPattern":"try { gizmo = CreateGizmo(type, component); }\ncatch (InvalidOperationException) { gizmo = CreateGizmo(type, component); // construct a fresh instance }","preventionTips":["Construct each gizmo instance exactly once","Never re-run constructors on cached gizmo instances","Use Activator.CreateInstance for all gizmo instantiation"],"tags":["invalid-state","constructor","gizmo","double-initialization"],"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"}