Hi Wolfgang,

OK, I don't think you're going to like this answer too much...

Firstly, you aren't allocating enough memory for the pointer. You are initialising it with just 1 byte of memory.  You need to allocate enough to be able to hold the NSError object (the real one, not the RubyCocoa proxy.  So let's just allocate 1000 bytes to be sure...

  errorPtr = ObjcPtr.new(1000)

The next problem is that you have to convince the type checking that is done during ObjcPtr.assign that the assign is ok.  Here's the magic line to do that:


If you want more information on how that works, you'll have to read up on BridgeSupport

The source code that you can look at to figure out how ObjcPtr works is in cls_objcptr.m and cls_objcptr.h

That said, you're trying to do this the wrong way.  If you try the changes that I mentioned above, your test program still won't work, because NSAlert.alertWithError expects a Ruby NSError as it's argument, not an ObjcPtr that points to an Objective-C NSError.  If you have an Objective-C API that uses a pointer to a pointer to an object, have a look at what is returned by that method in RubyCocoa - normally you can simply skip that parameter when you call the function, and the object is added to the array of returned objects, so it looks a bit like this:

result = NSObject.someMadeUpFunction_withError_()

result = ['a result', error]

At least, that's how it used to work, last time I tried to do this...  The documentation is here (http://rubycocoa.sourceforge.net/WorkingWithPointers) but I don't know if it is up to date...

So, what's the deal with ObjcPtr?  It is used when you need to be able to manipulate a raw memory buffer - for example trying to send an array of vertex data to an OpenGL call.  That said, there are nicer ways of doing this, such as packing an array, like this:

    @whiteLight = [ 0.2, 0.2, 0.2, 1.0 ].pack('f*')

sNote once again the funny string 'f*'. That's telling pack what format the ObjcPtr that is about to be generated should be considered as, as well as how to convert the ruby numbers. In this case, it is saying that the buffer generated by pack should be treated as a pointer to a bunch of floats.

Once again, I hope that helps...

Le 16 févr. 10 à 20:11, Wolfgang Kittenberger a écrit :

I try to return an errorpointer as shown in http://rubycocoa.sourceforge.net/AssignValueToPointerArgument.

Anyway, I get a runtime error when using "errorPtr.assign(error)":
2010-02-13 19:59:49.816 ErrorTest[4848:a0f] Contr#trigger: TypeError: can't convert OSX::NSError into Integer

Looks like I have to somehow tell the ObjcPtr instance, what type of object I want to assign, perhaps when generating it.

But I did not find any documentation about ObjcPtr.new. 

Best regards

this is my test code.:

require 'osx/cocoa'
class Contr < OSX::NSObject
include OSX
ib_action :trigger

def generateError(errorPtr)
error = NSError.alloc.initWithDomain_code_userInfo(
4711, nil)
end # generate_error

def trigger(sender)
errorPtr = ObjcPtr.new(1)
a = NSAlert.alertWithError(errorPtr)
end # trigger
end # Contr

SOLARIS 10 is the OS for Data Centers - provides features such as DTrace,
Predictive Self Healing and Award Winning ZFS. Get Solaris 10 NOW
Rubycocoa-talk mailing list