hxcpp: null function pointer when using __unsafe_get/__unsafe_set

63 views
Skip to first unread message

Michael Baczynski

unread,
May 1, 2012, 1:05:35 PM5/1/12
to haxe...@googlegroups.com
Hi,
I just stumbled upon this code, which throws an exception when targeting cpp:

class Foo<T> {
    private var data:Array<T>;
    public function new(size:Int) {
        data = new Array<T>();
        for (i in 0...size) data[i] = null; //allocate to prevent crash
        untyped data.__unsafe_get(0); //Error : Null Function Pointer
    }
}

I can avoid this problem by implementing haxe.rtti.Generic so is this a bug or normal behavior?

best,
Michael


Hugh

unread,
May 2, 2012, 12:26:38 AM5/2/12
to haxe...@googlegroups.com
Hi,
Yes, an array of <T> becomes an Array<Dynamic>, and Array<Dynamic> becomes Dynamic, so data is "Dynamic".  So this is the problem.
Consider that you would need completely different implementations for Array<Bool> and Array<SomeObject>, since the  "sizeof element" would be different.
Generic (dynamic) code is never going to be fast, so the "unsafe" option is not going to save anything.

If you knew that you were only dealing with objects, not Bool/Int/etc, then you may be able to use some @:functionCode to inject a static_cast, but you would have to really really want to.

Hugh

Michael Baczynski

unread,
May 2, 2012, 6:32:06 AM5/2/12
to haxe...@googlegroups.com
Thanks Hugh, this is what I have expected. I'm just playing around with generic code + unsafe access and results are astonishing (20-25x faster!).
Reply all
Reply to author
Forward
0 new messages