raylib-cpp: raylib::Model Causing a Malloc error on Copy
Hey guys!
I’ve run into a segfault that only seems to be caused by the destructor of both raylib::Model and raylib::Texture. I’m not sure where the full issue actually lies, however I thought I’d make it known here.
The context here is that I’m trying to build a resource manager that stores and tracks all Models and Textures that were loaded into the project. To allow multiple different types of objects, I’m using a std::variant class to store either a texture or Model, with a string acting as the key. The data structure is as follows:
std::map<std::string, std::variant<raylib::Model, raylib::Texture2D>>
The issue is, once I insert an element to this map, my program crashes with the following error:
app(31394,0x10fc30e00) malloc: *** error for object 0x7f9100000000: pointer being freed was not allocated
app(31394,0x10fc30e00) malloc: *** set a breakpoint in malloc_error_break to debug
make: *** [execute] Abort trap: 6
To test this further, I set up a few test cases to see whether I could isolate this issue. Here’s my code for storing a standard Raylib Model is a std::variant class:
auto model = LoadModel("assets/models/cube/cube.obj");
std::variant<Model, Texture> variant = std::variant<Model, Texture>();
variant = model;
When running this, I get no issues and the program runs normally. I can even add this variant to a map with no issue:
std::map<std::string, std::variant<Model, Texture>> map = std::map<std::string, std::variant<Model, Texture>>();
map.insert({"model", variant});
Doing this has no issues at all with any part of the program. It runs correctly and closes correctly.
When trying the same tests with raylib::Model, I get a very different outcome:
raylib::Model model2 = raylib::Model("assets/models/cube/cube.obj");
std::variant<raylib::Model, raylib::Texture> variant2 = std::variant<raylib::Model, raylib::Texture>(model2);
Running the following causes no errors initially, however closing the program results in the following segfault:
app(31580,0x1115eae00) malloc: *** error for object 0x9000000000000000: pointer being freed was not allocated
app(31580,0x1115eae00) malloc: *** set a breakpoint in malloc_error_break to debug
make: *** [execute] Abort trap: 6
When I then try and add the variant to a map of variants:
std::map<std::string, std::variant<raylib::Model, raylib::Texture>> map = std::map<std::string, std::variant<raylib::Model, raylib::Texture>>();
map.insert({"variant", variant});
This causes an instant error:
INFO: MODEL: model has 1 material meshes
INFO: VAO: [ID 2] Mesh uploaded successfully to VRAM (GPU)
INFO: VAO: [ID 2] Unloaded vertex data from VRAM (GPU)
INFO: MODEL: Unloaded model from RAM and VRAM
app(31580,0x1115eae00) malloc: *** error for object 0x9000000000000000: pointer being freed was not allocated
app(31580,0x1115eae00) malloc: *** set a breakpoint in malloc_error_break to debug
make: *** [execute] Abort trap: 6
I’ll also note that this issue seems to persist whenever a raylib::Model is added to any container type. For example:
std::vector<raylib::Model> vector = std::vector<raylib::Model>();
vector.push_back(raylib::Model("assets/models/cube/cube.obj"));
This will run alright, but when the application is closed another crash will occur:
app(31634,0x11b0fee00) malloc: *** error for object 0x7ffedfe0b918: pointer being freed was not allocated
app(31634,0x11b0fee00) malloc: *** set a breakpoint in malloc_error_break to debug
make: *** [execute] Abort trap: 6
The reason why I made all these cases was to try and illustrate that there seems to be some sort of issue with the way the raylib::Model class is being destroyed. Looking over Raylib’s logs, I can see the following sequence of events:
INFO: MODEL: [assets/models/cube/cube.obj] OBJ data loaded successfully: 1 meshes / 1 materials
INFO: MODEL: model has 1 material meshes
INFO: VAO: [ID 2] Mesh uploaded successfully to VRAM (GPU)
INFO: VAO: [ID 2] Unloaded vertex data from VRAM (GPU)
INFO: MODEL: Unloaded model from RAM and VRAM
Looking at this, it appears as if the model is being loaded in successfully, but is immediately being unloaded as soon as the model is added to a container. I’m not sure why this is the case, however I assume it might have to do with how the model is copied?
I’m not convinced that this is an issue with the normal raylib library, since these issues don’t seem to be persisting with traditional raylib objects. Would you happen to have any insights into this?
About this issue
- Original URL
- State: open
- Created 4 years ago
- Comments: 17 (10 by maintainers)
We may need move constructors for all the objects? Perhaps? 🤔 https://en.cppreference.com/w/cpp/language/move_constructor
After “deleting” the copy-ctor (for Texture and Mesh), and make them only-movable, you can use lambdas to “move” the return -value.
@RobLoach
@Raelr
At least every class with an custom dtor to apply the rule-of-five .
I’m personally not a fan of the implicit casting, but IMO the POD raylib types (like
::Model) should also be movable, e.g. when I’m passing the same::Modelto 2raylib::Models and both uses then same pointers (who is then the owner ?).Or at least it’s need to be copyable like
raylib::Imageorraylib::Wave.The Font Loading example might be a good place to test out using vector for construction and deconstruction of these data types; https://github.com/RobLoach/raylib-cpp/blob/master/examples/text/text_font_loading.cpp
Hey @RobLoach,
Thanks for getting back as quickly as you did!
I’ll go ahead and test out the direct assignment! I’ll also see if I can write up a short demonstration for you. I’ll get back as soon as I have the info you asked for 😃
Is there an example we could port that could demonstrate this?
Curious if the variant is copy assigning instead using the model by reference.
Also, how does C++ respond to the assignment rather than just the constructor?
Would we need to use
std::reference_wrapper? Seems like an ugly solution.