9 ms·
Honestly even with RAII memory management in C++ can be hard, especially with all lambda/closures. I can totally see this becoming a even bigger mess if combine
by dathinab 7y ago
Honestly even with RAII memory management in C++ can be hard, especially with all lambda/closures. I can totally see this becoming a even bigger mess if combined with corotines.
Most languages which have anything like corotines/async/await avoid this problem by using a GC. Rust kinda is will equipted for this with it's borrow checker (but it's hard for the compiler team to get completely right). But I don't see C++ to be suitable for many coming/common programming patterns with them. It is just way to easy to mess up memory management in the edge cases (like e.g. early cancelation etc.)
- quietbritishjim 7y agoA typical solution is to use shared pointers, and just try to take care to avoid ownership cycles. It's certainly common when using ASIO callbacks, which are a bit like coroutines, especially if they're methods of a class that represents the overall "function". I don't know much about C++2a coroutines but maybe a similar strategy could be used for them too. In a sense, reference counting isn't far off being a basic form of GC. In fact CPython uses reference counting and only uses anything else for detecting cycles.
- gumby 7y agoReference counting is a form of garbage collection. I don’t like the trade offs for many applications (problems with cycles and runtime overhead) but when you can’t use a transporting collector it’s often better than mark/sweep
- gpderetta 7y agoAsio enforces exactly once semantics for callbacks, so if you are careful you can thread your state through your callbacks [1] in a linear way and avoid reference counting. It is not always worth the effort though, but coroutines should make it trivial. [1] last time I checked ASIO didn't support move only callbacks so it was ackward to implement. Things might have changed since.
- asveikau 7y agoI rather like c++ closures in that they are pretty explicit about how capture works. Given the other concepts in the language it is absolutely necessary to consider how closure capture works with the copy constructor, for example. I would contrast it with objc blocks, where capture is pretty implicit and sometimes feel ambiguous. But I think that copy constructor is the root of a lot of evil performance penalty in c++. In the old days it was especially easy to over-allocate with vector or string. Move semantics fixed some of this. But passing around closures without a lot of care is also a consequence. An "easy" solution, to capture a shared_ptr, is not without its costs.
- gumby 7y agoYes, I often delete the copy constructor so that accidental copy cases become compile-time errors, though tttt that doesn’t seem to come up that often. I was bitten by a use-after-move last week but it was my own fault in some startup-time-order-of-initialization undefined behavior that the code was depending on that only worked on one compiler. It’s pretty uncommon.
- gpderetta 7y agoShared_ptr, the solution and cause of most performance problems :)
- jandrewrogers 7y agoAs a counterpoint, I've been ubiquitously using coroutines in C++ as the primary concurrent task scheduling mechanism in database engines for around a decade. It has always been dead simple and mostly straightforward, resource leaks were never an issue, though details vary with coroutine implementation. Since C++11, memory management has been remarkably easy to get right the first time in complex systems. It isn't just RAII, there are many other mechanics that enable resources to be automagically managed optimally from the perspective of the developer. In my experience across millions of lines of code, resources don't leak ever at this level of abstraction (pre-C++11 is a different story). I am perennially baffled by what other people are doing with modern C++ that resource leaks are chronic problem. I get the impression there are a large number of C++ programmers that don't grok basic resource management in C++11 (it really isn't that hard) and blame the language when resource management issues occur.
- jb1991 7y agoWell, just write a simple C++11 lambda that uses a reference capture, and store that lambda somewhere and watch your app crash when it that lambda runs after the referenced object is gone. Even if you stick with writing no "new" or "delete" in your code, just as good modern C++ encourages, this simple problem will always be there, and it doesn't just affect lambdas, though that's the sneakiest place it often happens. Of course you can avoid these problems very easily, but you have to be bitten by them first to know how easy they are to write.
- deleted 7y ago[deleted]
- otec 7y agoDon't capture by reference, as you're misusing the language.
- Gene_Parmesan 7y agoExcuse this ignorance from C# land, but how does one get by in C++ without ever writing "new"?