Page 1 of 1
When is it needed to use NSAutoReleasePool?
Posted: Sun Apr 27, 2025 3:44 pm
by Quin
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!
Re: When is it needed to use NSAutoReleasePool?
Posted: Sun Apr 27, 2025 5:15 pm
by wilbert
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.
Re: When is it needed to use NSAutoReleasePool?
Posted: Sun Apr 27, 2025 6:45 pm
by mk-soft
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()
Re: When is it needed to use NSAutoReleasePool?
Posted: Wed Apr 30, 2025 6:52 pm
by Quin
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?
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$
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?
Code: Select all
Protected NSArray.i
NSArray = NSSearchPathForDirectoriesInDomains_(#NSDocumentDirectory, #NSUserDomainMask, #YES)
ProcedureReturn CocoaString(CocoaMessage(0, NSArray, "firstObject")) + #PS$ + "Log.txt"
Thanks!