0

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!

  • Are there *any* warnings when compiling (with all warning flags turned on (e.g., `-Wall -Wextra` at least for gcc, `-Weverything` for clang, etc)? Have you tried running it through valgrind? Have you tried stepping through your program with a debugger? – 9769953 Jul 27 '18 at 11:12
  • Hello, thanks for replying! I tried with the warning flags turned on and no warnings at all. Then I tried with Valgrind and it shows one error which is: Address 0x0 is not stack'd, malloc'd or (recently) free'd. Then I malloc'd the variable obj to store it in a temporal memory space but the problem still the same. After the first reading I get the segmentation fault. – Diego Zúñiga Jul 27 '18 at 11:59
  • Have you looked at the values of `dwg`, `dwg.object_ref[i]`, `dwg.object_ref[i]->obj` etc in a debugger, and see if they make sense. Just printing an address, as you do now, doesn't mean anything if that address doesn't contain what you think it contains. – 9769953 Jul 27 '18 at 12:22
  • Please update your code with the allocation you're using. – 9769953 Jul 27 '18 at 12:23
  • I just found the problem using a debugger. Thanks for the recommendation! Just for record, the problem was in the analysed .dwg file because in the second element of the list it had a wrong address (0x00) and that's why it was stopping all the time in the same place. I was assuming that the file is correctly made. Thanks again. – Diego Zúñiga Jul 27 '18 at 14:54

1 Answers1

0

Finally I found the problem. It was in the analysed .dwg file because in the second element of the list it had a wrong address (0x00) and that's why it was stopping all the time in the same place. I was assuming that the file is correctly made and it seems to be a problem in some other files because I also tried with different dwg files so I'll add a condition on the address value before trying to have access to it.