AddressOf Method for EventHandler (Declare Obj-C)

Hi,

my application crashes when trying to pass a Pointer to a XOJO Method which acts as an EventHandler for the result of a declared Object-C function. The function expects a “block”. I am passing the “AddressOf” Result of the XOJO Method. It crashes with EXC_BAD_ACCESS / KERNEL_PROTECTION_FAILURE.

I am using this guide to create an EventHandler for a declared function: https://www.declaresub.com/ideclare/declares/functionpointers.html

Is this wrong?

Which event handler are you trying to use? The page you linked to is for Carbon and event handlers are done differently in Obj-C. If you are using this on OSX you will need to use the blocks plugin made by @Joe Ranieri if for iOS look into the iOSBlock class.

You cannot use a Xojo method of class for that, you must use a shared method. Or a method in a module.

[quote=24713:@Joe Ranieri]I am not speaking or acting as a representative of Xojo, Inc. in this post. All statements, views, or content below are my own.

Here’s a plugin I wrote that will wrap any delegate in an Objective-C block and doesn’t use private APIs <Blocks Plugin >.

Usage:

  • To create a block from a Delegate, call ObjCBlocks.CreateBlock, passing in your Delegate object.
  • When done with the block, call ObjCBlocks.ReleaseBlock on the pointer you got back from CreateBlock. Failing to do so will create memory leaks.

Note that some APIs that take blocks will invoke them on non-main threads. Even though this plugin lets you create a block, it does not magically become safe to use these APIs. Crashes or other hilarity will ensue.[/quote]