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

vector<weak_ptr> null after filtering: items | transform(<lock()>) | filter(<not null>) #1828

Open
helmesjo opened this issue Aug 22, 2024 · 0 comments

Comments

@helmesjo
Copy link

helmesjo commented Aug 22, 2024

This one hit me with a bit of surprise but I guess there might be a rationale (caching related), but couldn't find the answer.

I have some list items containing weak pointers that I would like to transform (weak.lock()) then filter (!= nullptr) where it may be destructed by another thread simultaneously, but I noticed a race condition where it passed the filtering but was still null.

auto items = std::vector<std::weak_ptr<my_type>>{};
// [..] - fill with items
auto range = items |
             views::transform(
               [](auto const& w)
               {
                 return w.lock();
               }) |
             views::filter(
               [](auto const& s)
               {
                 return s != nullptr;
               });

// while filtering the owner might have released the object.
auto res = std::vector<std::shared_ptr<my_type>>{std::begin(range), std::end(range)};

I might be totally wrong here, but conceptually I imagine it as transform extracting the instance of interest, forward (as-is, lifetime intact) to the filter which again maintains the lifetime so the end result can only be a non-null shared pointer.

Is this a bug or have I just misunderstood it?

@helmesjo helmesjo changed the title vector<weak_ptr> returns null-pointer after filtering: ... = items | transform(/* ptr.lock() */) | filter(/* ptr != nullptr*/) vector<weak_ptr> null after filtering: ... = items | transform(<lock()>) | filter(<not nullptr>) Aug 22, 2024
@helmesjo helmesjo changed the title vector<weak_ptr> null after filtering: ... = items | transform(<lock()>) | filter(<not nullptr>) vector<weak_ptr> null after filtering: items | transform(<lock()>) | filter(<not nullptr>) Aug 22, 2024
@helmesjo helmesjo changed the title vector<weak_ptr> null after filtering: items | transform(<lock()>) | filter(<not nullptr>) vector<weak_ptr> null after filtering: items | transform(<lock()>) | filter(<not null>) Aug 22, 2024
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

1 participant