Page 1 of 1
Free a NSImage?
Posted: Sun Dec 13, 2015 12:00 am
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?
Re: Free a NSImage?
Posted: Sun Dec 13, 2015 5:00 am
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.

Re: Free a NSImage?
Posted: Sun Dec 13, 2015 7:54 am
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
Re: Free a NSImage?
Posted: Sun Dec 13, 2015 1:33 pm
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.
Re: Free a NSImage?
Posted: Sun Dec 13, 2015 2:40 pm
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.
Re: Free a NSImage?
Posted: Tue Dec 15, 2015 7:49 pm
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.
Re: Free a NSImage?
Posted: Tue Dec 15, 2015 7:59 pm
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.
Re: Free a NSImage?
Posted: Tue Dec 15, 2015 8:34 pm
by GPI
And "Release" it, when not needed any more. Thanks for clarification.
btw. Found this:
https://en.wikibooks.org/wiki/Programmi ... nd_Release