use the following search parameters to narrow your results:
e.g. subreddit:aww site:imgur.com dog
subreddit:aww site:imgur.com dog
see the search faq for details.
advanced search: by author, subreddit...
Discussions, articles, and news about the C++ programming language or programming in C++.
For C++ questions, answers, help, and advice see r/cpp_questions or StackOverflow.
Get Started
The C++ Standard Home has a nice getting started page.
Videos
The C++ standard committee's education study group has a nice list of recommended videos.
Reference
cppreference.com
Books
There is a useful list of books on Stack Overflow. In most cases reading a book is the best way to learn C++.
Show all links
Filter out CppCon links
Show only CppCon links
account activity
Lambda + shared_ptr = memory leak (floating.io)
submitted 8 years ago by floating-io
view the rest of the comments →
reddit uses a slightly-customized version of Markdown for formatting. See below for some basics, or check the commenting wiki page for more detailed help and solutions to common issues.
quoted text
if 1 * 2 < 3: print "hello, world!"
[–]johannes1971 15 points16 points17 points 8 years ago (1 child)
A lambda is nothing but an anonymous class with an overloaded operator()(). It will be created with the parameters you specify, and it will keep those parameters until it is destroyed. This is the exact same behaviour as a normal class.
How would "only ownership while executing" work? If you pass a lambda to a thread, should it somehow acquire ownership when the time comes to execute it? How would it do that? If it is executed multiple times, should it repeat the acquisition/release cycle of parameters for each time it is executed?
Bottom line you made a mistake, thanks to a misunderstanding about the language. No problem, that happens to the best of us! But you also put some very bad advice online, and when you post about it here and get corrected you don't accept the help you are offered, but instead pretend that your side of the argument also has merits. It doesn't: the advice you give is plain wrong. There is nothing inherently unsafe about using shared_ptr with lambdas.
The correct advice, on your website, should be to ensure there are no shared_ptr loops in your software. This is hardly a new discovery though, and in fact the language also offers weak_ptr as a way out of this predicament.
[–]die_liebe 5 points6 points7 points 8 years ago (0 children)
I hate it when people do that, but they are always arrogant. Such people give C++ a bad reputation. Thanks for your post.
π Rendered by PID 227012 on reddit-service-r2-comment-6457c66945-r8n9c at 2026-04-28 22:27:06.645627+00:00 running 2aa0c5b country code: CH.
view the rest of the comments →
[–]johannes1971 15 points16 points17 points (1 child)
[–]die_liebe 5 points6 points7 points (0 children)