Blender V5.3
Blender memory allocation and freeing

About the MEM allocator module

memory allocation

MEM provides guarded memory management when using the –debug-memory option. All allocated memory is then enclosed by pads, to detect out-of-bound writes. All allocations are named to detect the source of memory leaks and print memory usage at runtime with the "Memory Statistics" operator.

to use the MEM API

MEM provides C++ template versions of the new/delete operators (#MEM_new and #MEM_delete), which are the preferred way to create and delete data in new code.

It also provides MEM_new_uninitialized and MEM_new_zeroed functions that behave like malloc and calloc respectively. These provide improved type safety, ensure that the allocated types are trivial, and reduce the casting verbosity by directly returning a pointer of the expected type. This memory should be freed with either #MEM_delete or MEM_delete_void.

There are a few reasons to use these functions:

  • Performance: When allocating large arrays like image buffers or mesh attributes that will be initialized soon after, doing an uninitialized memory allocation is faster.
  • Low level code: Core data structures and other low level code that directly allocate untyped memory buffers and provide their own type safety.
  • Legacy: Code that has not yet been updated to follow current conventions.

Dependencies

  • stdlib
  • stdio

API Documentation

See MEM_guardedalloc.h

About the MEM allocator module

memory allocation

MEM provides guarded memory management when using the –debug-memory option. All allocated memory is then enclosed by pads, to detect out-of-bound writes. All allocations are named to detect the source of memory leaks and print memory usage at runtime with the "Memory Statistics" operator.

to use the MEM API

MEM provides C++ template versions of the new/delete operators (#MEM_new and #MEM_delete), which are the preferred way to create and delete data in new code.

It also provides MEM_new_uninitialized and MEM_new_zeroed functions that behave like malloc and calloc respectively. These provide improved type safety, ensure that the allocated types are trivial, and reduce the casting verbosity by directly returning a pointer of the expected type. This memory should be freed with either #MEM_delete or MEM_delete_void.

There are a few reasons to use these functions:

  • Performance: When allocating large arrays like image buffers or mesh attributes that will be initialized soon after, doing an uninitialized memory allocation is faster.
  • Low level code: Core data structures and other low level code that directly allocate untyped memory buffers and provide their own type safety.
  • Legacy: Code that has not yet been updated to follow current conventions.

Dependencies

  • stdlib
  • stdio

API Documentation

See MEM_guardedalloc.h