When I was taught Objective-C, I learned to always wrap everything in your main method in an NSAutoReleasePool, so it gets auto-released on program termination. My question is, as someone who's fairly inexperienced with Cocoa dev in PB, when is it needed to use one in PB? Any time I create an NSObject?
Thanks!
When is it needed to use NSAutoReleasePool?
Re: When is it needed to use NSAutoReleasePool?
Within the PureBasic event loop, the objects marked for autorelease, are released regularly.
If you need them to be released more often you can use your own autorelease pool.
If you need them to be released more often you can use your own autorelease pool.
Windows (x64)
Raspberry Pi OS (Arm64)
Raspberry Pi OS (Arm64)
Re: When is it needed to use NSAutoReleasePool?
If CocoaMessage is used in threads, you must create your own pool.
Code: Select all
Procedure foo()
Protected pool
Pool = CocoaMessage(0, 0, "NSAutoreleasePool new")
; do any
If Pool
CocoaMessage(0, Pool, "release")
EndIf
EndProcedure
foo()
My Projects ThreadToGUI / OOP-BaseClass / EventDesigner V3
PB v3.30 / v5.75 - OS Mac Mini OSX 10.xx - VM Window Pro / Linux Ubuntu
Downloads on my Webspace / OneDrive
PB v3.30 / v5.75 - OS Mac Mini OSX 10.xx - VM Window Pro / Linux Ubuntu
Downloads on my Webspace / OneDrive
Re: When is it needed to use NSAutoReleasePool?
Only in threads? What do you mean exactly?
This function will be called from both the UI loop and the networking loop of my application. DO I need the autoreleasepool?
Furthermore, I have this function that's only called from the main thread, at the start of my application. I assume I don't need one here?
Thanks!
This function will be called from both the UI loop and the networking loop of my application. DO I need the autoreleasepool?
Code: Select all
Protected.i NSPool, NSDateFormatter, NSString, NSLocale, NSDate
Protected Res$
NSPool = CocoaMessage(0, 0, "NSAutoreleasePool new")
NSLocale = CocoaMessage(0, 0, "NSLocale currentLocale")
NSDateFormatter = CocoaMessage(0, 0, "NSDateFormatter new")
CocoaMessage(0, NSDateFormatter, "setLocalizedDateFormatFromTemplate:$", @"MMddyyyyHHmmss")
NSDate = CocoaMessage(0, 0, "NSDate date")
NSString = CocoaMessage(0, NSDateFormatter, "stringFromDate:", NSDate)
Res$ = CocoaString(NSString)
CocoaMessage(0, NSPool, "release")
ProcedureReturn Res$
Code: Select all
Protected NSArray.i
NSArray = NSSearchPathForDirectoriesInDomains_(#NSDocumentDirectory, #NSUserDomainMask, #YES)
ProcedureReturn CocoaString(CocoaMessage(0, NSArray, "firstObject")) + #PS$ + "Log.txt"