Re: [Vala] Why does a simple struct need a special copy function?



2013/7/12 Nadav Vinik <nadavvin gmail com>



On 6 July 2013 22:06, Jonas Kulla <nyocurio gmail com> wrote:




2013/7/6 Nadav Vinik <nadavvin gmail com>



On 6 July 2013 11:15, Jonas Kulla <nyocurio gmail com> wrote:




2013/7/6 Nadav Vinik <nadavvin gmail com>

 Hello

I get an error:
undefined reference to `pcap_packet_header_copy'



I try to write the missing copy function:

public void pcap_packet_header_copy(PCap.packet_header source,
PCap.packet_header dest) {
    dest = PCap.packet_header();
    dest.ts = source.ts;
    dest.caplen = source.caplen;
    dest.len = source.len;
}

but I get an error:
error: Invalid assignment from owned expression to unowned variable
    dest = PCap.packet_header();
    ^^^^^^^^^^^^^^^^^^^^^^^^^^^
if I do:
dest = (owned)PCap.packet_header();

I get an error:
error: Reference transfer not supported for this expression

What should I do?

thanks
Nadav



why can't vala copy? it's not only allocate memory of size of the
struct
and copy it to it?


struct pcap_pkthdr {
    struct timeval ts;    /* time stamp */
    bpf_u_int32 caplen;    /* length of portion present */
    bpf_u_int32 len;    /* length this packet (off wire) */
};

[CCode(cname = "struct pcap_pkthdr", has_type_id = false,
destroy_function
= "")]
    public struct packet_header {
        Posix.timeval ts;
        int32 caplen;
        int32 len;
    }

thanks
Nadav
_______________________________________________
vala-list mailing list
vala-list gnome org
https://mail.gnome.org/mailman/listinfo/vala-list


Hi,

I assume that declaration is from one of your vapis?
Vala always requires copy functions for structs, because
gobject is oblivious to the contents of your structs.
A simple example such as

struct MyStruct {
  int a;
  string b;
}

demonstrates why you can't simply shallow memcpy them.
For structs defined within vala code, valac generates all necessary
functions for you, but if you're interfacing with other C libs,
these need to be manually provided.

Can you try adding "has_copy_function = false" to
the struct attributes?


I tried, it compile, but than I have segmentation fault.



Jonas




--
הבלוג שלי:
http://nadavvin.com


Uhh, do you know how to debug programs width gdb?
It's pretty hard to tell from that what is going on.

Jonas




--
הבלוג שלי:
http://nadavvin.com



AFAIK the 'copy' function isn't responsible for allocating a new instance.
You should treat the 'dest' object as already allocted. Have you tried
without
that first problematic line in your function?

Jonas


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