{"record":{"id":"ea9b6940f4062ca7","repo":"DioxusLabs/dioxus","slug":"from-raw-called-with-a-null-pointer","errorCode":null,"errorMessage":"from_raw called with a null pointer","messagePattern":"from_raw called with a null pointer","errorType":"panic","errorClass":null,"httpStatus":null,"severity":"error","filePath":"packages/manganis/manganis-macro/src/ffi.rs","lineNumber":849,"sourceCode":"                            // `alloc` + `init` returns a +1 retained object; `Retained::from_raw`\n                            // takes over that reference and sends `release` on drop.\n                            let inner = manganis::objc2::rc::Retained::from_raw(instance)\n                                .ok_or(\"Failed to initialize instance\")?;\n\n                            Ok(Self { inner })\n                        }\n                    }\n\n                    /// Create from an existing object pointer.\n                    ///\n                    /// # Safety\n                    ///\n                    /// `ptr` must be a valid, non-null pointer to an Objective-C object,\n                    /// and the caller must transfer ownership of one retain count (+1)\n                    /// to the returned wrapper, which sends `release` on drop.\n                    pub unsafe fn from_raw(ptr: *mut manganis::objc2::runtime::AnyObject) -> Self {\n                        let inner = manganis::objc2::rc::Retained::from_raw(ptr)\n                            .expect(\"from_raw called with a null pointer\");\n                        Self { inner }\n                    }\n                }\n            };\n            output.extend(type_def);\n        }\n\n        // Generate function implementations\n        for func in &self.functions {\n            let func_code = self.generate_ios_function(func);\n            output.extend(func_code);\n        }\n\n        // Generate linker metadata\n        let metadata = self.generate_ios_metadata();\n        output.extend(metadata);\n\n        // Wrap in cfg","sourceCodeStart":831,"sourceCodeEnd":867,"githubUrl":"https://github.com/DioxusLabs/dioxus/blob/393d190a801ccb441d41923e232289b4f8a5c669/packages/manganis/manganis-macro/src/ffi.rs#L831-L867","documentation":"manganis generates Objective-C wrappers for Xcode asset-catalog APIs. from_raw converts a raw AnyObject pointer into an objc2 Retained wrapper, and Retained::from_raw returns None when the pointer is null, so a nil object coming back from the Objective-C side triggers this expect inside the generated code.","triggerScenarios":"Calling a generated objc2 binding whose underlying Objective-C API returned nil (lookup miss such as a named asset/color not present in the .xcassets, allocation failure), or an ownership bug where the pointer was already released before from_raw.","commonSituations":"iOS asset catalog integration requesting an asset name that doesn't exist; double-release making the pointer nil; hand-written bridging code with wrong retain/release counts.","solutions":["Check for null/nil on the Objective-C side before passing the pointer into from_raw","Verify the requested asset name exists in the asset catalog","Audit ownership: from_raw must receive a +1 retain count exactly once","Prefer nullable/Option-returning API variants instead of non-null from_raw wrappers"],"exampleFix":null,"handlingStrategy":"type-guard","validationCode":"// unsafe { if ptr.is_null() { /* handle nil result instead of calling from_raw */ } }","typeGuard":"// Generated-wrapper equivalent of a nullable check:\nunsafe fn try_from_raw(ptr: *mut AnyObject) -> Option<Wrapper> {\n    if ptr.is_null() { None } else { Some(Wrapper::from_raw(ptr)) }\n}","tryCatchPattern":null,"preventionTips":["Audit every objc API you bind for nil-returning failure modes and mark those as nullable","Verify asset names exist in the .xcassets before requesting them","Follow the +1 retain-count contract exactly once per from_raw call","Add null checks on the Swift/ObjC side before bridging pointers out"],"tags":["manganis","ffi","objc","ios","null-safety","panic"],"backgroundTag":null,"analyzedSha":"393d190a801ccb441d41923e232289b4f8a5c669","analyzedAt":"2026-08-16T11:27:45.815Z","schemaVersion":2},"datasetVersion":"2026-08-16T13:17:31.715Z"}