19 FEB 2026

rahulmnavneeth

memory ownership

homedocs

Principles

  1. Every allocation has exactly one owner.
  2. Ownership never transfers implicitly.
  3. Every create has a matching destroy.
  4. The application never frees internal pointers.

Ownership Table

ResourceOwnerCreated byFreed by
MopViewportApplicationmop_viewport_createmop_viewport_destroy
MopMesh (handle)Viewportmop_viewport_add_meshmop_viewport_remove_mesh or viewport destroy
RHI DeviceViewportrhi->device_createrhi->device_destroy
RHI FramebufferViewportrhi->framebuffer_createrhi->framebuffer_destroy
RHI BufferViewportrhi->buffer_createrhi->buffer_destroy
Vertex/index arraysApplicationApplication codeApplication code
Color buffer pointerBackendBackend internalBackend internal

Copy Semantics

When the application calls mop_viewport_add_mesh, the engine copies vertex and index data into RHI buffers. The application may free its arrays immediately.

Application                    Engine
     │                            │
     │  mop_viewport_add_mesh()   │
     │ ─────────────────────────> │
     │   (vertices[], indices[])  │
     │                            │── rhi->buffer_create (copies data)
     │  returns MopMesh*          │
     │ <───────────────────────── │
     │                            │
     │  free(vertices)            │   (safe: engine has its own copy)

Readback Pointers

mop_viewport_read_color returns a borrowed pointer. The application must not free it. Invalidated by:

Leak Prevention

mop_viewport_destroy walks the mesh array and destroys every active mesh's buffers before destroying the framebuffer and device. No leaks even if the application skips mop_viewport_remove_mesh.