Using Apple autorelease pools without Objective-C

I am developing an application that should work on Linux, Windows, and Mac OS X. For this purpose I am using C ++ with Qt.

For many reasons, on Mac OS X, I need to use CoreFoundation features (for example, CFBundleCopyBundleURL) that create the main objects that need to be released using CFRelease. But this generates many of these warnings:

*** __NSAutoreleaseNoPool(): Object 0x224f7e0 of class NSURL autoreleased with no pool in place - just leaking

All the code that I saw regarding these autostart pools is written in Objective-C. Does anyone know how to create / use autostart pools in C or C ++?

+3
source share
4 answers

id - C. , , cpp, :

autorelease_pool.hpp

class t_autorelease_pool {
public:
    t_autorelease_pool();
    ~t_autorelease_pool();
private:
    id d_pool; // << you may opt to preprocess this out on other platforms.
private:
    t_autorelease_pool(const t_autorelease_pool&);
    t_autorelease_pool& operator=(const t_autorelease_pool&);
};

autorelease_pool.mm

t_autorelease_pool::t_autorelease_pool() : d_pool([NSAutoreleasePool new]) {}
t_autorelease_pool::~t_autorelease_pool() { [this->d_pool drain]; }

cpp:

void UpdateUI() {
    t_autorelease_pool pool;
    // your/their autoreleasing code here
}

( ) ObjC, C:

#include <objc/runtime.h>
#include <objc/message.h>
...
id pool = objc_msgSend(objc_getClass("NSAutoreleasePool"), sel_getUid("new")); 
/* do stuff */
objc_msgSend(pool, sel_getUid("drain"));
+1

, , Objective-C.

Cocoa Cocoa Touch.

- , / C ++?

- Cocoa ( ) C. , .

, , , ( ) .

+1

, , -, Objective-C (NSURL) static static [NSURL urlWithString:]. , , "alloc" "copy", . , .

, , - :

NSAutoreleasePool * pool = [[NSAutoreleasePool alloc] init];
doStuff();
[pool release];

- .

0

Source: https://habr.com/ru/post/1739402/


All Articles