Code Monkey home page Code Monkey logo

Comments (4)

raysan5 avatar raysan5 commented on June 16, 2024

The crash was probably related to the attached issue. Image data loaded with cgltf_load_buffer_base64() can not be freed with cgltf_free(). It should be freed with corresponding de-allocator provided that by default fallbacks to cgltf_default_free(), that calls CGLTF_FREE().

Current solution was freeing it in raylib side with MemFree() that relies in RL_FREE(), it works as expected because CGLTF_FREE maps to RL_FREE() but not sure if there could be some potential issue if custom allocators are defined in one side or the other.

from cgltf.

jkuhlmann avatar jkuhlmann commented on June 16, 2024

So, am I getting this right, it's not really a bug in cgltf, but the documentation for cgltf_load_buffer_base64() could potentially be clearer?

from cgltf.

raysan5 avatar raysan5 commented on June 16, 2024

@jkuhlmann It's not really a bug but it can be an issue for users and potential library bindings, I had a similar issue with raylib allocators.

When generating a .dll using the library, the allocators (malloc/free pair) could use a different memory context than the ones used by the user.

For example, here it is one function provided by raylib:

char *LoadFileText(const char *fileName);   // Load text data from file (read), returns a '\0' terminated string

This function loads a char array using the internal allocator, only configurable by a macro at raylib compile time (raylib does not provide callbacks for custom allocators config like cgltf).

The problem is that most users could be tempted to use this function in the following way:

char *textData = LoadFileText("game_dialog.txt");
//...
free(textData);      // WARNING: This could generate a crash if the user-side allocator does not match the one used internally by raylib!

The solution I adopted for raylib was always providing a Unload*() equivalent for all functions loading memory to avoid the allocators missmatch, that was my approach (considering most raylib users could be unexperience developers). I also exposed MemFree() calling the internall de-allocator, just in case.

char *textData = LoadFileText("game_dialog.txt");
//...
UnloadFileText(textData);      // Correct internall de-allocator used

cgltf uses a different and more professional approach. It provides the option of setting custom allocators and also cgltf_load_buffer_base64() supports the cgltf_options parameter to configure them. But, in case of a naive use of the function, it could happen the same issue:

void *data = NULL;
cgltf_options options = { 0 };
cgltf_result result = cgltf_load_buffer_base64(&options, outSize, cgltfImage->uri, &data);
//...
free(data);   // WARNING: This could generate a crash if the user-side allocator does not match the one used by cgltf

User side solution is just setting the cgltf_options allocators correctly but there could be some side-case (cgltf build inside a DLL + binding + separate compilations (lib/user-code) + unexperienced user) that code could crash.

Considering cgltf is more intended for coders with experience I think a note in the documentation could be enough.

from cgltf.

zeux avatar zeux commented on June 16, 2024

cgltf_free is perhaps a little unfortunate as far as names go :D looks like this was the source of the bug, not the regular allocator mismatch? We could rename it and keep the old name for compatibility, but worth noting is that even a C compiler should warn on the call to cgltf_free given a high enough warning setting.

from cgltf.

Related Issues (20)

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. 📊📈🎉

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.