Why are my Macruby pointers not surviving being passed as void pointers?
I've implemented the selector alertDidEnd:returnCode:contextInfo:
. The last parameter, contextInfo, is a void pointer. Before calling the method I put the pointer together as follows.
# in windowShouldClose
p = Pointer.new(:boolean)
p.assign(true)
Then I call the method, and inside its body have the following:
# in alertDidEnd
puts p[0] # => a number like 245
puts p.cas开发者_开发知识库t!('B')[0] # => false (rather than true)
What am I doing wrong? Is this because the calling method (windowShouldClose) finishes in its own thread before this selector has a chance (just guessing)?
Or should I be creating the pointer as an object pointer?
# make a pointer to an object and assign to it the instance of TrueClass
p = Pointer.new(:id)
p.assign(true)
I've read what the O'reilly Macruby Book has to say about this.
Thanks!
The most likely reason is that nothing is retaining this object so it gets garbage collected. If you create an object and implement its finalize method and put a puts in there, you will be able to see that this is the case.
Try tracking your state in the controller that is creating the sheet, and passing a pointer to the controller as the contextInfo. If this state is mediating concurrent interactions between ui elements, it is likely the controller that is responsible for all the elements needs to track it anyway. Of course, it is probably then easier to not pass a contextInfo and using which callback you give the sheet to keep track of what conditions it was called under.
精彩评论