{"record":{"id":"616f6c756ad5c1ef","repo":"flxzt/rnote","slug":"accessing-image-surface-data-failed-err-e","errorCode":null,"errorMessage":"accessing image surface data failed, Err: {e:?}","messagePattern":"accessing image surface data failed, Err: (.+?)","errorType":"exception","errorClass":null,"httpStatus":null,"severity":"error","filePath":"crates/rnote-engine/src/image.rs","lineNumber":357,"sourceCode":"        .map_err(|e| {\n            anyhow::anyhow!(\n                \"creating image surface with dimensions ({}, {}) failed, Err: {e:?}\",\n                width_scaled,\n                height_scaled,\n            )\n        })?;\n\n        {\n            let cairo_cx = cairo::Context::new(&image_surface)?;\n            cairo_cx.scale(image_scale, image_scale);\n            cairo_cx.translate(-bounds.mins[0], -bounds.mins[1]);\n            // Apply the draw function\n            draw_func(&cairo_cx)?;\n        }\n\n        let data = image_surface\n            .data()\n            .map_err(|e| anyhow::anyhow!(\"accessing image surface data failed, Err: {e:?}\"))?\n            .to_vec();\n\n        Ok(Image {\n            data: glib::Bytes::from_owned(convert_image_bgra_to_rgba(\n                width_scaled,\n                height_scaled,\n                data,\n            )),\n            rectangle: Rectangle::from_p2d_aabb(bounds),\n            pixel_width: width_scaled,\n            pixel_height: height_scaled,\n            // cairo renders to bgra8-premultiplied, but we convert it to rgba8-premultiplied\n            memory_format: ImageMemoryFormat::R8g8b8a8Premultiplied,\n        })\n    }\n\n    /// Generates an image with a provided closure that draws onto a [piet_cairo::CairoRenderContext].\n    pub fn gen_with_piet<F>(draw_func: F, bounds: Aabb, image_scale: f64) -> anyhow::Result<Self>","sourceCodeStart":339,"sourceCodeEnd":375,"githubUrl":"https://github.com/flxzt/rnote/blob/bbc5354502ba2fc83eec2670b535348825e679a6/crates/rnote-engine/src/image.rs#L339-L375","documentation":"image_surface.data() returned Err in gen_with_cairo, i.e. Cairo could not give access to the surface's underlying byte buffer after the draw function ran. In Cairo, this happens when the surface is in an error state (e.g. creation failed earlier or the draw func wrote to an invalid context).","triggerScenarios":"Calling gen_with_cairo whose draw_func triggered a Cairo error (invalid surface, context error state) so that data() cannot be mapped.","commonSituations":"Panics or Cairo errors inside draw_func leaving the context in error state; surface creation with degenerate dimensions; running out of memory mapping the buffer.","solutions":["Check the draw_func for Cairo calls that can put the context into an error state (e.g. invalid patterns, huge coordinates).","Verify the surface was created successfully and dimensions are sane before drawing.","Check cairo surface status (image_surface.status()) after drawing to get the underlying error.","Ensure sufficient memory; very large surfaces can fail on map."],"exampleFix":"// before\nlet data = image_surface.data().map_err(|e| anyhow!(\"accessing image surface data failed, Err: {e:?}\"))?;\n// after\nif image_surface.status() != cairo::Status::Success {\n    return Err(anyhow!(\"surface in error state: {:?}\", image_surface.status()));\n}\nlet data = image_surface.data().map_err(|e| anyhow!(\"accessing image surface data failed, Err: {e:?}\"))?;","handlingStrategy":"try-catch","validationCode":"// rust\nif image_surface.status() != cairo::Status::Success {\n    return Err(anyhow!(\"surface in error state before data access: {:?}\", image_surface.status()));\n}","typeGuard":"fn surface_is_readable(s: &cairo::ImageSurface) -> bool {\n    s.status() == cairo::Status::Success && s.width() > 0 && s.height() > 0\n}","tryCatchPattern":"let data = image_surface.data().map_err(|e| {\n    anyhow!(\"accessing image surface data failed (status: {:?}): {e:?}\", image_surface.status())\n})?;","preventionTips":["Check cairo status after every draw operation.","Avoid panics inside draw_func; return Result instead.","Keep surface dimensions within safe limits."],"tags":["cairo","image","buffer-access"],"backgroundTag":"internal-invariant-violation","analyzedSha":"bbc5354502ba2fc83eec2670b535348825e679a6","analyzedAt":"2026-09-08T13:20:33.747Z","contentChangedAt":"2026-09-08T13:20:33.747Z","schemaVersion":2},"datasetVersion":"2026-09-16T04:17:20.429Z"}