When is it needed to use NSAutoReleasePool?

Mac OSX specific forum
Quin
Addict
Addict
Posts: 1133
Joined: Thu Mar 31, 2022 7:03 pm
Location: Colorado, United States
Contact:

When is it needed to use NSAutoReleasePool?

Post 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!
wilbert
PureBasic Expert
PureBasic Expert
Posts: 3942
Joined: Sun Aug 08, 2004 5:21 am
Location: Netherlands

Re: When is it needed to use NSAutoReleasePool?

Post 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.
Windows (x64)
Raspberry Pi OS (Arm64)
User avatar
mk-soft
Always Here
Always Here
Posts: 6244
Joined: Fri May 12, 2006 6:51 pm
Location: Germany

Re: When is it needed to use NSAutoReleasePool?

Post 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()
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
Quin
Addict
Addict
Posts: 1133
Joined: Thu Mar 31, 2022 7:03 pm
Location: Colorado, United States
Contact:

Re: When is it needed to use NSAutoReleasePool?

Post 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!
Post Reply