Skip to content

[Python/macOS] malloc: double free crash when assigning numpy arrays to software_video_frame.pixels (Memory Ownership Bug) #14751

@srikar0805

Description

@srikar0805

Issue Description:
When using rs.software_device and injecting offline frames via rs.software_video_frame(), assigning a standard Python/Numpy byte buffer to the .pixels attribute results in a fatal malloc: double free crash on macOS.

This appears to be a memory-ownership conflict between the pyrealsense2 C++ backend and Python's Garbage Collector. When the frame processing concludes, the C++ engine attempts to free the memory buffer, but Python's GC also attempts to free the same numpy array, resulting in a dual-deletion crash.

In the C++ SDK, this is usually handled via a custom deleter callback, but the deleter attribute is not exposed in the pyrealsense2 Python wrapper, leaving no official way to safely manage memory lifecycle for synthetic frames.

Environment:

  • OS: macOS (Apple Silicon / Intel)
  • Language: Python 3.x
  • Library: pyrealsense2
  • Camera: No camera connected (Offline processing via software_device)

Steps to Reproduce

Here is a minimal reproducible example (MRE) that instantly triggers the crash:

import pyrealsense2 as rs
import numpy as np

# 1. Initialize Software Device
dev = rs.software_device()
depth_sensor = dev.add_sensor("Depth")
depth_stream = rs.video_stream()
depth_stream.type = rs.stream.depth
depth_stream.width, depth_stream.height = 1280, 720
depth_stream.bpp = 2
depth_stream.fmt = rs.format.z16
depth_profile = depth_sensor.add_video_stream(depth_stream)

# 2. Create Dummy Numpy Array (Simulating an offline raw depth map)
dummy_depth = np.zeros((720, 1280), dtype=np.uint16)

# 3. Inject Frame
d_frame = rs.software_video_frame()
d_frame.pixels = np.ascontiguousarray(dummy_depth).flatten() # <-- Issue originates here
d_frame.stride = 1280 * 2
d_frame.bpp = 2
d_frame.timestamp = 1.0
d_frame.domain = rs.timestamp_domain.system_time
d_frame.frame_number = 1
d_frame.profile = depth_profile.as_video_stream_profile()

depth_sensor.on_video_frame(d_frame)

# 4. Crash occurs shortly after or when script terminates

Expected Behavior

The SDK should accept the python buffer (e.g., Numpy array), process the frame (e.g., via rs.align), and safely release the memory without crashing the Python interpreter. Ideally, software_video_frame should copy the memory internally or the Python wrapper should expose the deleter property so users can tell C++ not to free the memory.

Actual Behavior

The script fatally aborts with a C++ memory exception:

Python(86335,0x1f329b080) malloc: double free for ptr 0x120b78000
Python(86335,0x1f329b080) malloc: * set a breakpoint in malloc_error_break to debug
zsh: abort      python script.py 

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions