Free a NSImage?

Mac OSX specific forum
GPI
PureBasic Expert
PureBasic Expert
Posts: 1398
Joined: Fri Apr 25, 2003 6:41 pm

Free a NSImage?

Post by GPI »

Code: Select all

ImageID = CocoaMessage(0, CocoaMessage(0, 0, "NSWorkspace sharedWorkspace"), "iconForFileType:$", @"txt")
Debug ImageID
ImageID = CocoaMessage(0, CocoaMessage(0, 0, "NSWorkspace sharedWorkspace"), "iconForFileType:$", @"txt")
Debug ImageID
ImageID = CocoaMessage(0, CocoaMessage(0, 0, "NSWorkspace sharedWorkspace"), "iconForFileType:$", @"txt")
Debug ImageID
Stupid question: It seems that this Cocoa-Function always create a new image. How can I free/destroy this image?
User avatar
TI-994A
Addict
Addict
Posts: 2791
Joined: Sat Feb 19, 2011 3:47 am
Location: Singapore
Contact:

Re: Free a NSImage?

Post by TI-994A »

GPI wrote:How can I free/destroy this image?
Depending on your requirements, there are a few options.

1. Add it to the auto-release pool (which might be the default behaviour):

Code: Select all

ImageID = CocoaMessage(0, CocoaMessage(0, CocoaMessage(0, 0, "NSWorkspace sharedWorkspace"), "iconForFileType:$", @"txt"), "autorelease")
2. Release it manually:

Code: Select all

ImageID = CocoaMessage(0, CocoaMessage(0, 0, "NSWorkspace sharedWorkspace"), "iconForFileType:$", @"txt")
CocoaMessage(0, ImageID, "release")
3. Destroy the image:

Code: Select all

ImportC ""
  object_dispose(object.i)
EndImport

ImageID = CocoaMessage(0, CocoaMessage(0, 0, "NSWorkspace sharedWorkspace"), "iconForFileType:$", @"txt")
object_dispose(ImageID)
However, as with any API function, I can't attest to their safe usage. :wink:
Texas Instruments TI-99/4A Home Computer: the first home computer with a 16bit processor, crammed into an 8bit architecture. Great hardware - Poor design - Wonderful BASIC engine. And it could talk too! Please visit my YouTube Channel :D
wilbert
PureBasic Expert
PureBasic Expert
Posts: 3944
Joined: Sun Aug 08, 2004 5:21 am
Location: Netherlands

Re: Free a NSImage?

Post by wilbert »

TI-994A wrote:1. Add it to the auto-release pool (which might be the default behavior)
2. Release it manually
3. Destroy the image
1. The image object created with iconForFileType is already added to the autorelease pool by the OS itself so you are not supposed to add "autorelease" yourself.
2. Autorelease objects should not be released with "release" as it will cause a crash if the OS already drained or released the autorelease pool.
3. This is the equivalent of method 2 so also not allowed in this case.

What you should do is nothing and let the OS release the object if you are using it within the main event loop.
If you are using the method in a place where there's no autorelease pool, create a pool yourself like this.

Code: Select all

Pool = CocoaMessage(0, 0, "NSAutoreleasePool new")
ImageID = CocoaMessage(0, CocoaMessage(0, 0, "NSWorkspace sharedWorkspace"), "iconForFileType:$", @"txt")
CocoaMessage(0, Pool, "release")
Inside a loop it also may be good to create a pool yourself.

Code: Select all

Workspace = CocoaMessage(0, 0, "NSWorkspace sharedWorkspace")
For i = 1 To 100000
  Pool = CocoaMessage(0, 0, "NSAutoreleasePool new")
  ImageID = CocoaMessage(0, Workspace, "iconForFileType:$", @"txt")
  CocoaMessage(0, Pool, "release")
Next
Windows (x64)
Raspberry Pi OS (Arm64)
User avatar
TI-994A
Addict
Addict
Posts: 2791
Joined: Sat Feb 19, 2011 3:47 am
Location: Singapore
Contact:

Re: Free a NSImage?

Post by TI-994A »

wilbert wrote:If you are using the method in a place where there's no autorelease pool, create a pool yourself like this.

Code: Select all

Pool = CocoaMessage(0, 0, "NSAutoreleasePool new")
ImageID = CocoaMessage(0, CocoaMessage(0, 0, "NSWorkspace sharedWorkspace"), "iconForFileType:$", @"txt")
CocoaMessage(0, Pool, "release")
Inside a loop it also may be good to create a pool yourself.

Code: Select all

Workspace = CocoaMessage(0, 0, "NSWorkspace sharedWorkspace")
For i = 1 To 100000
  Pool = CocoaMessage(0, 0, "NSAutoreleasePool new")
  ImageID = CocoaMessage(0, Workspace, "iconForFileType:$", @"txt")
  CocoaMessage(0, Pool, "release")
Next
Hi wilbert, and thanks for the autoreleasepool examples; great code, as always.

Just to clarify, all objects created immediately after instantiating the autoreleasepool object belong to that pool? And if so, only API created objects, or even PureBasic ones?

Thanks again.
Texas Instruments TI-99/4A Home Computer: the first home computer with a 16bit processor, crammed into an 8bit architecture. Great hardware - Poor design - Wonderful BASIC engine. And it could talk too! Please visit my YouTube Channel :D
wilbert
PureBasic Expert
PureBasic Expert
Posts: 3944
Joined: Sun Aug 08, 2004 5:21 am
Location: Netherlands

Re: Free a NSImage?

Post by wilbert »

TI-994A wrote:all objects created immediately after instantiating the autoreleasepool object belong to that pool? And if so, only API created objects, or even PureBasic ones?
There's a stack of autorelease pools so objects that are autoreleased (PB ones also) get thrown in the last created pool for that particular thread and released when the pool they are thrown into is getting drained or released.
PureBasic itself releases autorelease objects when the WindowEvent() or WaitWindowEvent() command is used.
Windows (x64)
Raspberry Pi OS (Arm64)
GPI
PureBasic Expert
PureBasic Expert
Posts: 1398
Joined: Fri Apr 25, 2003 6:41 pm

Re: Free a NSImage?

Post by GPI »

wilbert wrote:PureBasic itself releases autorelease objects when the WindowEvent() or WaitWindowEvent() command is used.
Thanks. I should read the manual sometimes :) But does this mean, that I shouldn't use CocoaMessage in threads because the main-thread could free the objects?

I Thought, that the object would only released at the end of the program.
wilbert
PureBasic Expert
PureBasic Expert
Posts: 3944
Joined: Sun Aug 08, 2004 5:21 am
Location: Netherlands

Re: Free a NSImage?

Post by wilbert »

Autorelease pools are thread specific.
It should be safe to use CocoaMessage from within a thread.
If you need an object that is set to autorelease to stay valid you can send a "retain" message to the object.
Windows (x64)
Raspberry Pi OS (Arm64)
GPI
PureBasic Expert
PureBasic Expert
Posts: 1398
Joined: Fri Apr 25, 2003 6:41 pm

Re: Free a NSImage?

Post by GPI »

And "Release" it, when not needed any more. Thanks for clarification.

btw. Found this: https://en.wikibooks.org/wiki/Programmi ... nd_Release
Post Reply