-
Notifications
You must be signed in to change notification settings - Fork 86
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Ownership of Models + Double Frees #299
Comments
For Context, // Load model from generated mesh
// WARNING: A shallow copy of mesh is generated, passed by value,
// as long as struct contains pointers to data and some values, we get a copy
// of mesh pointing to same data as original version... be careful!
Model LoadModelFromMesh(Mesh mesh)
{
Model model = { 0 };
model.transform = MatrixIdentity();
model.meshCount = 1;
model.meshes = (Mesh *)RL_CALLOC(model.meshCount, sizeof(Mesh));
model.meshes[0] = mesh; ///< here Seems like the |
Good catch 👍 What would be the best way around this? Something similar to the TextureUnmanaged approach we took? |
Maybe a I also had look into void SetTexture(const ::Texture& newTexture) = delete;
void SetTexture(::Texture&& newTexture) {
texture = newTexture;
newTexture = NullTexture;
} rtextures.c // Unload render texture from GPU memory (VRAM)
void UnloadRenderTexture(RenderTexture2D target)
{
if (target.id > 0)
{
if (target.texture.id > 0)
{
// Color texture attached to FBO is deleted
rlUnloadTexture(target.texture.id);
}
// NOTE: Depth texture/renderbuffer is automatically
// queried and deleted before deleting framebuffer
rlUnloadFramebuffer(target.id);
}
}
|
Hello, I noticed this could happen with Shaders as well, as seen in Raylib's
I suppose a ShaderUnamanged class may also be required. |
Good catch, @DenisBelmondo! Let's re-work this issue for Shaders too. |
https://github.com/RobLoach/raylib-cpp/blob/master/include/Material.hpp#L54 GETTERSETTER(::Shader, Shader, shader)
GETTERSETTER(::MaterialMap*, Maps, maps)
// TODO(RobLoach): Resolve the Material params being a float[4].
// GETTERSETTER(float[4], Params, params) Seem like the getter for void SetShader(const Shader&) = delete;
void SetShader(::Shader*) { ... } // keep the old API ?, risk of dangling pointer, who is the owner of this pointer ???
//void SetShader(const ShaderUnamanged&) = delete; // not sure about setter for ShaderUnamanged
void SetShader(Shader&&) { ... }
void SetShader(::Shader&&) { ... } I guess you load the Shader via raylib (C API) or use the raylib::Material mat;
{
raylib::Shader shr (...);
mat.SetShader(shr);
}
{
mat.SetShader(LoadShader(...))
} EDIT: |
https://github.com/RobLoach/raylib-cpp/blob/master/include/Material.hpp#L75
// Unbind (disconnect) shader from car.material[0]
// to avoid UnloadMaterial() trying to unload it automatically
car.materials[0].shader = (Shader){ 0 }; |
Rather than making a new issue, let's just keep this rolling to keep the history in place. Thanks a lot, @furudbat, for the work on cleaning up some of the Shader work. |
It would be really awesome to "automate" or RAII the ownership of resources. (IMHO it should be one of THE feature of this project) We already try to use move-semantic and dtor to free resources and memory. ImageThis one is very simple, AudioStream and WaveLike FontSelf contained, but a bit special, only Unload non-default fonts, see UnloadFont. Sound and MusicWhen using // Unload music stream
void UnloadMusicStream(Music music)
{
UnloadAudioStream(music.stream); make Texture and Shader
Be aware when constructing RenderTextureThis one holds an RenderTexture(unsigned int id, ::Texture&& texture, const ::Texture& depth)
: ::RenderTexture{id, texture, depth} {
texture = { 0 };
} Not sure about // Unload render texture from GPU memory (VRAM)
void UnloadRenderTexture(RenderTexture2D target)
{
if (target.id > 0)
{
if (target.texture.id > 0)
{
// Color texture attached to FBO is deleted
rlUnloadTexture(target.texture.id);
}
// NOTE: Depth texture/renderbuffer is automatically
// queried and deleted before deleting framebuffer
rlUnloadFramebuffer(target.id);
}
} MaterialShader and maps (pointer) can be set (beware of ownership). // Unload material from memory
void UnloadMaterial(Material material)
{
// Unload material shader (avoid unloading default shader, managed by raylib)
if (material.shader.id != rlGetShaderIdDefault()) UnloadShader(material.shader);
// Unload loaded texture maps (avoid unloading default texture, managed by raylib)
if (material.maps != NULL)
{
for (int i = 0; i < MAX_MATERIAL_MAPS; i++)
{
if (material.maps[i].texture.id != rlGetTextureIdDefault()) rlUnloadTexture(material.maps[i].texture.id);
}
}
RL_FREE(material.maps);
}
Mesh??? Model??? Value Classes
"Global" Management ClassesSome classes like |
When loading the Models with Mesh the ownership is not clear and the mash(es)(?) got double freed.
Logs
models example
rmodel.c
The text was updated successfully, but these errors were encountered: