C++ Interfacing Segmentation fault problem

Hello everybody,

I’m new to kdb+, feel very excited about it. Now I’m coding some kind of broker API adapter to feed data into and send orders directly from kdb+.

Everything went fine, when I was just feeding data to kdb+ tables. But now I really need some help figuring out whats wrong with the code:

kdbh = khp(“localhost”, 12001); if(kdbh < 0) exit(1);

    K r;

    while(1) {

r = k(kdbh,(S)0);

if(r) {

if(r->t == 0) {

// r is 3 item list of format (“.u.upd”; `trade; updateTable)

printf(“\n%s update”, kK(r)[1]->s); // table name symbol atom

    }

}

r0(r); // decrement reference count, free memory

}

It looks OK, according to documentation, I took it from http://www.timestored.com/kdb-guides/kdb-c-api-example

The problem is, when I’m trying test writing from kdb+, I’m getting Segmentation fault in c++ program. Here is test request I’m making:

q) (neg h) (“.u.upd”;trade;(A;123))

After some investingation, I found out that things went wrong exactly at printf line. How to make it work?

I will appreciate any help.

Best regards,

Yury Smykalov

Hi Yury,

Welcome to kx community!

the code more or less looks ok - btw, you should call r0 on r only if r is not null.
It’s not clear from your description whether this crash happens immediately on the first msg, or some time later.

Which platform is this? linux? windows? If windows note the loadlib issues at bottom of

http://code.kx.com/wiki/Cookbook/InterfacingWithC

it sounds like the pointer to the interned symbol (x->s) is invalid, and that would suggest either the heap was not initialized correctly (e.g. the loadlib issue) or you’ve perhaps missed the KXVER definition so it is using the wrong header? If binding with latest c.o/c.obj etc, you’d need KXVER=3.

thanks

Hi!

Thank you for your response, I really appreciate it. 

It works! Actually, I found out I was using old c.o file. Stupid mistake, I know. But thanks for pointing at c.o issues, you made me double-check that file.

Another thing I learned from this bug - printf statement didn’t have newline character in the end - so it wasn’t flushing to the stdout immediately. It can be a source of confusion, If you don’t know that.

About r0 - thanks, I moved that statement up in the if block.

Best regards,

Yury Smykalov

???, 29 ??? 2014 ?., 20:03:30 UTC+4 ??? Charles Skelton ???:

Hi Yury,

Welcome to kx community!

the code more or less looks ok - btw, you should call r0 on r only if r is not null.
It’s not clear from your description whether this crash happens immediately on the first msg, or some time later.

Which platform is this? linux? windows? If windows note the loadlib issues at bottom of

http://code.kx.com/wiki/Cookbook/InterfacingWithC

it sounds like the pointer to the interned symbol (x->s) is invalid, and that would suggest either the heap was not initialized correctly (e.g. the loadlib issue) or you’ve perhaps missed the KXVER definition so it is using the wrong header? If binding with latest c.o/c.obj etc, you’d need KXVER=3.

thanks