Segmentation fault in agread()

I get an segmentation fault in agread() like discribed in the faq (

Is there a workaround without compiling from source?

I am using Qt5.0.2, MinGW4.7 32bit as compiler and the stable Graphviz2.38 binaries. On Ubuntu my code compiles and runs fine.


The simplest way is to read

The simplest way is to read in the entire graph into memory, and pass a pointer to that memory to agmemread().

If that is not possible (e.g., the file is too large), you need to tell agread() to use a reader compatible with the stream you give it. By default, agread assumes you are passing it a FILE* generated by the visual studio version of stdio, and uses that version of fgets to read the stream. To pass in your own reader, use the third argument:

Agdisc_t mydisc;
Agodisc_t myiodisc;

mydisc.mem = NULL;  // use system default = NULL;   // use system default = &myiodisc;
myiodisc.afread = reader; 
myiodisc.putstr = NULL;  // only need to set if calling gvRender()
myiodisc.flush = NULL;   // only need to set if calling gvRender()

The reader function has type

  int (*reader) (void *chan, char *buf, int bufsize);

and should act like the read() system call. That is, it reads from
the stream chan, storing the bytes in buf, whose size is bufsize,
and returns the number of bytes it read. 

For Unix-like stdio, one might use

static int reader(void *chan, char *buf, int bufsize)
    return fread(buf, 1, bufsize, (FILE*)chan); 

Then, to read the graph, use:

FILE* fp = fopen ("mygraph.gv","r");
Agraph_t* g = agread (fp, &mydisc);


Reading the file into memory

Reading the file into memory and passing a pointer to agmemread() works like a charm.

For the second approach to understand i will have to take a deeper look into dokumentation.

kind regards.

I probably made the second

I probably made the second approach sound more complicated than it is. Bascially, you can pass any value you want as the first argument to agread as long as you supply a compatible read function in the afread field of the second argument.

Recent comments