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

Initializing a Vec<QPdf> with QPdf::empty() results in all elements of the Vec pointing to the same handler #5

Open
protochron opened this issue Oct 22, 2023 · 1 comment

Comments

@protochron
Copy link

Something about this code isn't working well with the way that QPdf::empty() is implemented:

let pdfs = vec![QPdf::empty(); number_to_init as usize];

What does work is initializing each element of the Vec using separate QPdf::empty() calls:

let mut pdfs: Vec<QPdf> = Vec::new();
for _ in 0..length
  pdfs.push(QPdf::empty());
}

Using rust-lldb I was able to verify that initializing a Vec with QPdf::empty() set all elements of the Vec to use the same pointer in the Handler, or specifically whatever is pointed to by qpdf_sys::qpdf_init()

@ancwrd1
Copy link
Owner

ancwrd1 commented Oct 22, 2023

Yeah, the vec! macro expects that the element has a Clone implementation. The QPdf structure has a reference-counted internal handle and a cheap "clone" operation so it can be easily passed over as a function parameter, pointing to the same data. This was originally by design, which unfortunately has this side-effect when constructing a Vec.
I am not sure how to resolve it easily.

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

No branches or pull requests

2 participants