I'm working on DWG files using LibreDWG Library.
I succeeded to open a file, and put all the data in a dwg structure which contains many different fields.
The dwg structure description is here.
The problem comes when i try to print data from object_ref which type is Dwg_Object_Ref ** , I take the value num_object_refs to go through the object_ref array and then every element contains 3 fields (see here). I can print te value absolute_ref which is a long unsigned int, but then I need to get a value from obj which is a pointer to a dwg_object structure. From obj I want to get the value "index" and my code to do that is:
#include <stdio.h>
#include <string.h>
#include <stdlib.h>
#include "dwg.h"
int load_dwg(char *filename, unsigned int opts) {
unsigned int i;
int success;
Dwg_Data dwg;
memset(&dwg, 0, sizeof(Dwg_Data));
dwg.opts = opts;
success = dwg_read_file(filename, &dwg);
FILE *f = fopen("lines.txt", "w");
for (i = 0; i < dwg.num_object_refs; i++)
{
struct _dwg_object *object;
object = malloc(sizeof(struct _dwg_object));
object = dwg.object_ref[i][0].obj;
printf("%d\n", object->index);
}
fclose(f);
dwg_free(&dwg);
return success;}
int main (int argc, char *argv[])
{
int i = 1;
unsigned int opts = 1;
load_dwg (argv[i], opts);
return 0;
}
This print the first value for "index" and then there is a segmentation fault.
210
Erreur de segmentation (core dumped)
but if I change the printf line inside the for to print the addresses like this:
printf("%p\n", &dwg.object_ref[i]->obj->index);
I get all the addresses without any problem.
Can anybody help me to understand the reason of this problem please?
Thanks!