Skip to content

ModelUnmanaged default constructor leaves members uninitialized #411

Description

@RobLoach

Problem

The default constructor of ModelUnmanaged at include/ModelUnmanaged.hpp:24 is:

ModelUnmanaged() {}

This does not initialize any of the base ::Model struct members. Fields like meshCount, materialCount, meshes, materials, meshMaterial, skeleton, currentPose, and boneMatrices are left with indeterminate values.

This causes two concrete problems:

  1. Double-free / crash on destruction: If a default-constructed Model is destroyed without ever being loaded, the destructor calls UnloadModel() which tries to free the garbage pointer values in meshes, materials, etc.
  2. Undefined behavior on query: Calling any getter (e.g. GetMeshCount()) on a default-constructed model returns garbage.

This is tracked in issue #351.

Suggested Implementation

Zero-initialize all members in the default constructor. The simplest approach:

ModelUnmanaged() : ::Model{} {}

This value-initializes the base struct, zeroing all scalar and pointer members. Alternatively, explicitly set each member:

ModelUnmanaged() {
    meshCount = 0;
    materialCount = 0;
    meshes = nullptr;
    materials = nullptr;
    meshMaterial = nullptr;
    transform = MatrixIdentity();
    skeleton = {0, nullptr, nullptr};
    currentPose = nullptr;
    boneMatrices = nullptr;
}

The first approach (: ::Model{}) is preferred — it's shorter and automatically covers any new fields added to raylib's Model struct in future versions.

QA

  • Default-constructed ModelUnmanaged and Model can be destroyed without crash
  • GetMeshCount() returns 0 on a default-constructed model
  • All pointer getters return nullptr on a default-constructed model
  • Existing tests continue to pass
  • No regressions in move constructor/assignment (which already null out the source)

Activity

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Labels

    bugSomething isn't working

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions