Skip to main content

Memory Management

Rules

  1. All allocated strings must be freed
    • boxlite_box_id() -> boxlite_free_string()
    • boxlite_list_info() -> boxlite_free_string()
    • Info/metrics JSON -> boxlite_free_string()
  2. Error structs must be freed
    • CBoxliteError -> boxlite_error_free()
  3. Results must be freed
    • CBoxliteExecResult -> boxlite_result_free()
  4. All cleanup functions are NULL-safe

Functions

boxlite_free_string

Free a string allocated by BoxLite.
Use this for any string returned by BoxLite API functions such as boxlite_box_id(), boxlite_list_info(), boxlite_get_info(), boxlite_box_info(), boxlite_runtime_metrics(), and boxlite_box_metrics(). Safe to call with NULL.

boxlite_error_free

Free error struct (message only - struct itself is stack-allocated).
Safe to call with NULL.

boxlite_result_free

Free an execution result.
Safe to call with NULL.

boxlite_simple_free

Free a simple box (auto-stops and removes).
Safe to call with NULL.

boxlite_runtime_free

Free a runtime instance.
Safe to call with NULL. Automatically frees all boxes.

Memory Ownership Summary

JSON Schema Reference

BoxOptions Schema

Required Fields

All BoxOptions JSON must include these fields:
Omitting any of the required fields will result in a JSON parsing error. Always include rootfs, env, volumes, network, and ports even if they are empty.

Field Reference

RootfsSpec

or

VolumeSpec

PortSpec

Building JSON in C

Since C has no native JSON support, here are common approaches for constructing BoxOptions JSON:

String Concatenation

Using snprintf for Dynamic Values

With Environment Variables

With Volumes and Ports

Thread Safety

The safe pattern is: share a single CBoxliteRuntime across threads, but have each thread create and manage its own box handles.

Safe Multi-threaded Usage

Callback Threading

Callbacks passed to boxlite_execute are always invoked on the calling thread. This means:
  • You do not need to synchronize within the callback itself
  • The callback blocks the execution until it returns
  • Heavy processing in callbacks will slow down command execution