DioxusLabs/dioxus · error

from_raw called with a null pointer

Error message

from_raw called with a null pointer

What it means

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.

Source

Thrown at packages/manganis/manganis-macro/src/ffi.rs:849

                            // `alloc` + `init` returns a +1 retained object; `Retained::from_raw`
                            // takes over that reference and sends `release` on drop.
                            let inner = manganis::objc2::rc::Retained::from_raw(instance)
                                .ok_or("Failed to initialize instance")?;

                            Ok(Self { inner })
                        }
                    }

                    /// Create from an existing object pointer.
                    ///
                    /// # Safety
                    ///
                    /// `ptr` must be a valid, non-null pointer to an Objective-C object,
                    /// and the caller must transfer ownership of one retain count (+1)
                    /// to the returned wrapper, which sends `release` on drop.
                    pub unsafe fn from_raw(ptr: *mut manganis::objc2::runtime::AnyObject) -> Self {
                        let inner = manganis::objc2::rc::Retained::from_raw(ptr)
                            .expect("from_raw called with a null pointer");
                        Self { inner }
                    }
                }
            };
            output.extend(type_def);
        }

        // Generate function implementations
        for func in &self.functions {
            let func_code = self.generate_ios_function(func);
            output.extend(func_code);
        }

        // Generate linker metadata
        let metadata = self.generate_ios_metadata();
        output.extend(metadata);

        // Wrap in cfg

View on GitHub (pinned to 393d190a80)

Solutions

  1. Check for null/nil on the Objective-C side before passing the pointer into from_raw
  2. Verify the requested asset name exists in the asset catalog
  3. Audit ownership: from_raw must receive a +1 retain count exactly once
  4. Prefer nullable/Option-returning API variants instead of non-null from_raw wrappers
Defensive patterns

Strategy: type-guard

Validate before calling

// unsafe { if ptr.is_null() { /* handle nil result instead of calling from_raw */ } }

Type guard

// Generated-wrapper equivalent of a nullable check:
unsafe fn try_from_raw(ptr: *mut AnyObject) -> Option<Wrapper> {
    if ptr.is_null() { None } else { Some(Wrapper::from_raw(ptr)) }
}

Prevention

When it happens

Trigger: 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.

Common situations: 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.

Related errors


AI-assisted analysis of DioxusLabs/dioxus@393d190a80 (2026-08-16). Data as JSON: /api/errors/ea9b6940f4062ca7. Report an issue: GitHub.