Open rhasson opened 12 years ago
Struct instances start of with undefined data (this is true in C as well). You must initialize the struct, either manually or through other means like node-ffi
, before accessing the data from it.
The problem you're describing is true for any "pointer" type, but the solution is simple: don't access uninitialized fields. If this were C, the compiler would be giving you a warning ;)
Before passing these structs to a function (via node-ffi) I initialize them by doing tree = new pst_desc_tree(); The challenge is that within the C library the nested struct pointers are allocated and filled in correctly, but when that pointer is passed back to node-ffi I get an invalide memory address.
Am I not initializing the structs correctly? Do I need to initialize each of the nested structs as well?
Can you give an example of manually initializing the structs?
Can you show me some of your example ffi code?
Cool, and do you have a test script that uses that and crashes?
any time I try to access any of the nested structs inside the instantiated pst_file struct called "f". That means when I try to do f.d_head.d_id for example would cause a seg fault.
In the file on github I try to copy the memory address from the instantiated buffer into another buffer and try to read it but that also fails. I would ignore that bit of code since I don't think it's even correct.
when creating a struct where one or more of its members are pointers to the same struct and used with node-ffi these nested structs are filled in with invalid memory addresses so dereferencing them causes a seg fault.
Example structs:
In this example accessing "next" or "child", etc. would result in a seg fault because the memory address stored at these offsets in the buffer are invalid.
Thanks, Roy