Re: [Vala] trying vapi file for ngspice, segfaults



On Tue, 2014-05-20 at 19:55 +0200, Steven Vanden Branden wrote:
hello,
seems like the mail got lost but anyway have read the answer on
mailarchive about the bool, got the vapi to compile but get an segment
fault error when i try to run the application so i would like to review
the vapi for the function that causes it:

#if __GNUC__ >= 4
    #define IMPEXP __attribute__ ((visibility ("default")))
    #define IMPEXPLOCAL  __attribute__ ((visibility ("hidden")))


IMPEXP
int  ngSpice_Init(SendChar* printfcn, SendStat* statfcn, ControlledExit*
ngexit, SendData* sdata, SendInitData* sinitdata, BGThreadRunning*
bgtrun, void* userData);

the arguments are almost all pointer to callback functions and these i
need to get in my vala program.

vapi code :

[CCode (cname = "ngSpice_Init")]
            public int init(out SendOutput* a,out SendSimulationStatus* b,out ControlledExit* c
            , out SendVectorData* d, out SendInitializationData* e, out IsBackgroundThreadRunning* f, out 
void * userData);

Do not use pointers in Vala.  They are there for some corner cases which
will not work any other way, but in general if you are using pointers in
Vala you are doing it wrong.

I don't know why you think these are out parameters—based on a quick
look at the documentation they seem like in parameters.

It's untested, but you should try something along the lines of:

        [CCode (cname = "SendChar", has_target = false, simple_generics
        = true)]
        public delegate int SendChar<T> (string str, int id, T data);
        [CCode (cname = "SendStat", has_target = false, simple_generics
        = true)]
        public delegate int SendStat<T> (string str, int id, T data);
        [CCode (cname = "ControlledExit", has_target = false,
        simple_generics = true)]
        public delegate int ControlledExit<T> (int status, bool
        immediate, bool quit, int id, T data);
        [CCode (cname = "SendData", has_target = false, simple_generics
        = true)]
        public delegate int SendData<T> (VecValuesAll[] vectors, int id,
        T data);
        [CCode (cname = "SendInitData", has_target = false,
        simple_generics = true)]
        public delegate int SendInitData<T> ([CCode (array_length =
        false)] VecInfoAll[] vecs, int id, T data);
        [CCode (cname = "BGThreadRunning", has_target = false,
        simple_generics = true)]
        public delegate int BGThreadRunning<T> (bool running, int id, T
        data);
        
        [CCode (cname = "ngSpice_Init", simple_generics = true)]
        public static int init<T> (Ngspice.SendChar<T>? send_char,
        Ngspice.SendStat<T>? send_stat, Ngspice.ControlledExit<T>
        controlled_exit, Ngspice.SendData<T>? send_data,
        Ngspice.SendInitData<T>? send_data_init,
        Ngspice.BGThreadRunning<T>? bg_thread_running, T data);

A copy (without the wrapping): http://pastebin.com/u3HbaVpF

The only weird thing about this is you can't use closures.  The C API
takes a single parameter to pass to all the callbacks, instead of one
per callback like Vala expects, so you'll have to deal with that data
param manually.  Also, make sure it stays alive as long as you need it,
otherwise you'll probably end up with a use-after-free bug.


-Evan

Attachment: signature.asc
Description: This is a digitally signed message part



[Date Prev][Date Next]   [Thread Prev][Thread Next]   [Thread Index] [Date Index] [Author Index]