Skip to content
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

Canvas::fill_rect does not work at all #44

Open
avitran0 opened this issue Nov 25, 2024 · 3 comments · May be fixed by #84
Open

Canvas::fill_rect does not work at all #44

avitran0 opened this issue Nov 25, 2024 · 3 comments · May be fixed by #84

Comments

@avitran0
Copy link

i don't know if this is an issue with this crate or sdl3-sys, but Canvas::fill_rect does not work at all, when passing in an FRect. draw_rect works just fine.

System:
Fedora 41
Wayland
Nvidia RTX 3080

using version 0.11 and feature build-from-source

@jajo-11
Copy link

jajo-11 commented Jan 7, 2025

Can reproduce on Windows using 483fde4 and build-from-source

replacing the implementation with the following fixes the issue but I am not sure what is actually wrong with the current implementation

#[doc(alias = "SDL_RenderFillRect")]
pub fn fill_rect<R: Into<Option<FRect>>>(&mut self, rect: R) -> Result<(), String> {
    let result= if let Some(rect) = rect.into() {
        unsafe {
            sys::render::SDL_RenderFillRect(
                self.context.raw,
                &rect.to_ll(),
            )
        }
    } else {
        unsafe {
            sys::render::SDL_RenderFillRect(
                self.context.raw,
                ptr::null(),
            )
        }
    };
    if !result {
        Err(get_error())
    } else {
        Ok(())
    }
}

@kitt159
Copy link

kitt159 commented Jan 24, 2025

In the original code, a reference to a temporary struct created by the to_ll function is created. This struct lives only inside of lambda, but the reference is then converted to a pointer, so the borrow checker can't catch it.
rect.into().map_or(ptr::null(), |r| &r.to_ll()),

I have created similar code in safe rust which shows the error exactly
https://play.rust-lang.org/?version=stable&mode=debug&edition=2021&gist=d2e86745124d8be508851184054d2885

@kitt159 kitt159 linked a pull request Feb 1, 2025 that will close this issue
@kitt159
Copy link

kitt159 commented Feb 1, 2025

I believe there are additional issues in the code. This pattern appears frequently:

match src.into() {
    Some(ref rect) => &rect.to_ll(),
    None => ptr::null(),
}

The same problem occurs here as well. A temporary structure is created using to_ll, and a pointer is created to it. However, once the match block ends, the structure goes out of scope, causing the pointer to become invalid.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging a pull request may close this issue.

3 participants