asyncplusplus icon indicating copy to clipboard operation
asyncplusplus copied to clipboard

MacOS: missing Obj-C autorelease pool handling

Open kikaxa opened this issue 1 year ago • 3 comments

Most functions end up using native apis which create autorelease objects. These objects would be effectively "leaked" if run through async++.

The thread pool/loop needs local per-iteration pool to match user expectations(or lack of).

sample Qt pool handling:

#ifdef __APPLE__
// Use the direct runtime interface to manage autorelease pools, as it
// has less overhead then allocating NSAutoreleasePools, and allows for
// a future where we use ARC (where NSAutoreleasePool is not allowed).
// https://clang.llvm.org/docs/AutomaticReferenceCounting.html#runtime-support
extern "C" {
    void *objc_autoreleasePoolPush(void);
    void objc_autoreleasePoolPop(void *pool);
}
    #define AUTORELEASE_THREAD_GUARD() auto poolDone = ScopedDefer([pool = objc_autoreleasePoolPush()](){ objc_autoreleasePoolPop(pool); });
#else
    #define AUTORELEASE_THREAD_GUARD()
#endif

kikaxa avatar Feb 09 '24 16:02 kikaxa

due to the need to also cover inline and thread schedulers, the patch is really invasive and must be done at the loop level of thread pool. one can find the changes at https://github.com/kikaxa/asyncplusplus

kikaxa avatar Feb 15 '24 08:02 kikaxa

@kikaxa I'm no longer actively maintaining this project. Would you like me to add you as a member so you can merge PRs and commit directly?

Amanieu avatar Feb 15 '24 23:02 Amanieu

@kikaxa I'm no longer actively maintaining this project. Would you like me to add you as a member so you can merge PRs and commit directly?

I can do that, except my requirements are a bit different to original ones. As example, i no longer target c++11, with 14-17 being preferred and 20 used in many places. As such, i fear i may miss something and make it worse for whatever users actually want older standards working. (some CI additions might be able to help that) (There are also ideas to try integrating coroutines and newer standards really simplify some template magic)

Again, im not very active on projects outside my work so probably only minimal support and trivial changes are expected.

Maybe, current code can be stabilised as tag/release and a new "experimental" branch can be started that has less requirements

kikaxa avatar Feb 16 '24 08:02 kikaxa