www.digitalmars.com         C & C++   DMDScript  

digitalmars.D.learn - Problem calling objc function with dmd

I have some problem calling an Objective-C runtime function when I 
compile with dmd, it works with gdc. DMD version v1.041

I first have the function declaration like this:
module bindings;

extern (C) id objc_msgSend (id self, SEL op, ...);



Then a wrapper like this:

module objc;
import bindings;

alias objc_object* id;


struct objc_object
{
     Class isa;
	
     R msgSend (R = id, ARGS...) (SEL op, ARGS args)
     {
         return (cast(R function (id, SEL, 
ARGS))&bindings.objc_msgSend)(this, op, args);
     }
}



Then I use the function like this:

void main ()
{
     id cls = objc_getClass("NSAutoreleasePool");
     id o = cls.msgSend(sel_registerName("alloc"));
}

And I get the following error during runtime:

2009-05-10 20:55:09.203 main[14534:10b] *** _NSAutoreleaseNoPool(): 
Object 0x109d70 of class NSCFString autoreleased with no pool in place - 
just leaking
Stack: (0x9290573f 0x92811e32 0x9281d28a 0x941a523d 0x941a5331 
0x941a390c 0x941a39d2 0x1d84 0x1d01 0x2d29b 0x2d1d2 0x2d2da)
2009-05-10 20:55:09.205 main[14534:10b] *** (null): unrecognized 
selector sent to class 0xa03b09e0
2009-05-10 20:55:09.205 main[14534:10b] *** _NSAutoreleaseNoPool(): 
Object 0x10b350 of class NSCFString autoreleased with no pool in place - 
just leaking
Stack: (0x9290573f 0x92811e32 0x9281d28a 0x941a523d 0x941a5371 
0x941a390c 0x941a39d2 0x1d84 0x1d01 0x2d29b 0x2d1d2 0x2d2da)
2009-05-10 20:55:09.206 main[14534:10b] *** _NSAutoreleaseNoPool(): 
Object 0x10b2f0 of class NSCFString autoreleased with no pool in place - 
just leaking
Stack: (0x9290573f 0x92811e32 0x9281d28a 0x941a5392 0x941a390c 
0x941a39d2 0x1d84 0x1d01 0x2d29b 0x2d1d2 0x2d2da)
2009-05-10 20:55:09.206 main[14534:10b] *** _NSAutoreleaseNoPool(): 
Object 0x10b510 of class NSException autoreleased with no pool in place 
- just leaking
Stack: (0x9290573f 0x92811e32 0x941a53c2 0x941a390c 0x941a39d2 0x1d84 
0x1d01 0x2d29b 0x2d1d2 0x2d2da)
2009-05-10 20:55:09.206 main[14534:10b] *** _NSAutoreleaseNoPool(): 
Object 0x10b340 of class _NSCallStackArray autoreleased with no pool in 
place - just leaking
Stack: (0x9290573f 0x92811e32 0x9286bcb8 0x9419e10b 0x92c14e3b 
0x941a53ca 0x941a390c 0x941a39d2 0x1d84 0x1d01 0x2d29b 0x2d1d2 0x2d2da)
2009-05-10 20:55:09.207 main[14534:10b] *** _NSAutoreleaseNoPool(): 
Object 0x10bfb0 of class NSCFString autoreleased with no pool in place - 
just leaking
Stack: (0x9290573f 0x92811e32 0x9281d28a 0x9419e08e 0x92c14e3b 
0x941a53ca 0x941a390c 0x941a39d2 0x1d84 0x1d01 0x2d29b 0x2d1d2 0x2d2da)
2009-05-10 20:55:09.207 main[14534:10b] *** _NSAutoreleaseNoPool(): 
Object 0x10c140 of class NSCFData autoreleased with no pool in place - 
just leaking
Stack: (0x9290573f 0x92811e32 0x92826505 0x928260e1 0x9419e0a2 
0x92c14e3b 0x941a53ca 0x941a390c 0x941a39d2 0x1d84 0x1d01 0x2d29b 
0x2d1d2 0x2d2da)
2009-05-10 20:55:09.208 main[14534:10b] *** Terminating app due to 
uncaught exception 'NSInvalidArgumentException', reason: '*** (null): 
unrecognized selector sent to class 0xa03b09e0'
2009-05-10 20:55:09.208 main[14534:10b] Stack: (
     2484723979,
     2462142011,
     2484753354,
     2484746508,
     2484746706,
     7556,
     7425,
     184987,
     184786,
     185050
)
Trace/BPT trap

If I don't cast the objc_msgSend function or move the msgSend function 
outside the struct it works. (The function has to be casted to work in 
some cases).	
May 10 2009