Giter VIP home page Giter VIP logo

Comments (8)

unknownue avatar unknownue commented on July 19, 2024 2

A newer version of this crate has been published to crates.io.
It should fix the problem of this issue.
If the problem still exists, reopen this issue.

from gli-rs.

unknownue avatar unknownue commented on July 19, 2024

That is probably a memory bug of my code and also the difficult part of writing this crate.
In deed, Drop is not implemented for exposed texture struct like Texture2D, FSampler2D.
Instead I implement Drop for the ffi texture base struct(which is a shared member of all exposed texture structs):

gli-rs/src/texture.rs

Lines 185 to 198 in c633c17

impl Drop for crate::ffi::root::gli::texture {
fn drop(&mut self) {
// In original gli::texture class(C++), it contains member wrapped with std::shared_ptr.
// Here manually call destructor(`~texture()`) on texture object to decrease shared_ptr counter for its inner member.
// Dangerous operation. This operation is not fully tested.
// It does make sense since Rust can't dual with the class member with shared_ptr in texture class.
// If you find better method to dual with this problem, welcome to create an issue on github.
unsafe {
bindings::destroy_texture(self)
}
}
}

In the original C++ implementation, the fsampler2D is just a plain struct(so there no memory problem on it), but the texture base class hold a shared pointer to the actual texture data. This shared pointer may increase when creating sub image class like GliImage by Texture2D::get_level() method.

And I implement this crate just for loading small amount of ktx texture, so I have not tested the case of heavy loading. I had written some code to confirm that the shared pointer counter would decrease when the above drop method is called. However further investigation about memory consumption have not been fully tested yet. Maybe the destructor of Texture class in C++ is not called correctly even the shared pointer counter has been decreased to 0.

from gli-rs.

unknownue avatar unknownue commented on July 19, 2024

Could you provide the program to reproduce this problem(just a test problem to eat out the memory using this crate)? It would be helpful for me to debugging.

from gli-rs.

nickkuk avatar nickkuk commented on July 19, 2024

Oh, thank you for pointing out Drop impl for texture, I didn't notice it. Let's focus on sampler.

When I add

for _ in 0..1_000_000_000 {
  FSampler2D::new(&texture_loaded, Wrap::CLAMP_TO_EDGE, Filter::LINEAR, Filter::LINEAR);
}

before this line and run

cargo t --release

it eats 1 GB per second.

from gli-rs.

nickkuk avatar nickkuk commented on July 19, 2024

Does this line really contains shared_ptr to texture data?

If so, in Rust we should

  1. add impl Drop for sampler to decrement shared_ptr counter on C++ side;
  2. maybe remove lifetime type parameter for FSampler1D, FSampler2D, etc, as they contain analogue of Arc<Texture>.

from gli-rs.

unknownue avatar unknownue commented on July 19, 2024

I have reproduced the memory consumption when testing with FSampler2D. The memory is leaked when dropping FSampler2D, but that not reproduced on GliImage.

For the advice you recommended,

  1. It seems that I neglect the inner texture member in sampler class when implementing this crate. I am trying to apply a quick fix to FSampler2D.
  2. The lifetime type parameter in FSampler* structs are to remind user that the data in Texture* are shared to FSampler*. And in this way, the rust compiler also makes sure that the FSampler* is dropped before Texture*, which corresponds to the ordinary use case. Although it may not cause problem without the lifetime, the lifetime parameter does introduce the semantic relationship between Texture* and FSampler*. I want the FSampler* to use in local, instead of storing it in struct. Construct a FSampler* struct would not introduce much overhead, since it would not copy the texture data.

from gli-rs.

unknownue avatar unknownue commented on July 19, 2024

The binding.rs must be rebuilt. However I encounter some link errors to the C++ compiler(as both the rust and clang compiler versions are upgraded on my computer), it may take some time to fix it.

from gli-rs.

unknownue avatar unknownue commented on July 19, 2024

Ok, I apply a quick fix special for FSampler2D. You can try it if you are in urgent need:

# In Cargo.toml
[dependencies]
gli = { git = "https://github.com/unknownue/gli-rs", branch = "memory-fix" }

This fix is implemented in somewhat ugly way.
It should take me a few days to fix remaining parts and eliminate the warning toggled by newer version of rustc.

from gli-rs.

Related Issues (3)

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.