[MacRuby-devel] Accessing memory address in MacRuby

Laurent Sansonetti lsansonetti at apple.com
Fri Dec 4 23:40:54 PST 2009


Hi Yehuda,

C pointers are mapped to the Pointer class. You can create Pointer  
objects by yourself, and if you override an Objective-C method with a  
C pointer argument, MacRuby will hand you an instance of Pointer.

You code could be rewritten in MacRuby as:

   retValuePtr = Pointer.new(:int) # this creates a C pointer of sizeof 
(int) of 1 element
   retValuePtr[0] = 1 # assigns 1 as the first (and only) element of  
the pointer
   historyController.repository outputForArguments:['update-ref', '- 
mUpdate from GitX', ref.ref, dropCommit.realSha], retValue: retValuePtr
   if retValuePtr[0] == 1 # dereference the first element of the pointer
     return false
   end
   # ...

If you are overriding the outputForArguments:retValue: method in  
MacRuby, the last argument should be of Pointer type.

   def outputForArguments(args, retValue: retValuePtr)
     # ...
     if some_error
       retValuePtr[0] = 1
     end
   end

It is very easily to crash the program while using a Pointer object  
that was created by MacRuby, because MacRuby has no way to know the  
boundaries of the original C pointer.

Laurent

On Dec 4, 2009, at 4:16 PM, Yehuda Katz wrote:

> I'm currently working with an API that sends me a memory address for  
> an int and expects me to populate it with a value:
>
> It essentially looks like this:
>
> int retValue = 1;
> [historyController.repository outputForArguments:[NSArray  
> arrayWithObjects:@"update-ref", @"-mUpdate from GitX", [ref ref],  
> [dropCommit realSha], NULL] retValue:&retValue];
> if (retValue) return NO;
>
> I am writing code that implements outputForArguments:retValue:, but  
> it seems that MacRuby hands me an opaque primitive that I can't use.  
> When I try to do anything with it, I get EXC_BAD_ACCESS.
>
> Any ideas?
>
> Yehuda Katz
> Developer | Engine Yard
> (ph) 718.877.1325
> _______________________________________________
> MacRuby-devel mailing list
> MacRuby-devel at lists.macosforge.org
> http://lists.macosforge.org/mailman/listinfo.cgi/macruby-devel



More information about the MacRuby-devel mailing list