[GRASS-dev] d.thematic.area: need for a bit of help in
C-programming/debugging
Glynn Clements
glynn at gclements.plus.com
Thu Jul 24 15:39:06 EDT 2008
Moritz Lennert wrote:
> I'm having a problem with the d.thematic.area module I just committed in
> a modified version.
> It seems to be linked to the class_discont function in
> lib/arraystats/class.c, as this only happens with this algorithm.
> However, when I do the same test with d.thematic.areas -nl flags (i.e.
> don't plot the map, but only output legend info), I don't get the
> segfaults, so there is some problem between this function and the
> display functions.
Or you just get lucky in that case.
> I seem to have tracked it down to the call to Vect_new_line_struct() on
> line 32 in display/d.thematic.area/area.c and from there to (I think)
> Vect__new_line_struct in lib/vector/Vlib/line.c.
Vect_new_line_struct() just malloc()s a block of memory. If it's
crashing there, then it's because the heap has been corrupted, by
overrunning another heap buffer.
I suggest using glibc's mprobe() function:
-- Function: enum mcheck_status mprobe (void *POINTER)
The `mprobe' function lets you explicitly check for inconsistencies
in a particular allocated block. You must have already called
`mcheck' at the beginning of the program, to do its occasional
checks; calling `mprobe' requests an additional consistency check
to be done at the time of the call.
The argument POINTER must be a pointer returned by `malloc' or
`realloc'. `mprobe' returns a value that says what inconsistency,
if any, was found. The values are described below.
-- Data Type: enum mcheck_status
This enumerated type describes what kind of inconsistency was
detected in an allocated block, if any. Here are the possible
values:
`MCHECK_DISABLED'
`mcheck' was not called before the first allocation. No
consistency checking can be done.
`MCHECK_OK'
No inconsistency detected.
`MCHECK_HEAD'
The data immediately before the block was modified. This
commonly happens when an array index or pointer is
decremented too far.
`MCHECK_TAIL'
The data immediately after the block was modified. This
commonly happens when an array index or pointer is
incremented too far.
`MCHECK_FREE'
The block was already freed.
To use it, you need to link the program with -lmcheck (this ensures
that mcheck() is called before the first malloc()), then call mprobe()
on any malloc()d block which you suspect may be getting overrun (those
in class_discont() would seem like an obvious choice).
--
Glynn Clements <glynn at gclements.plus.com>
More information about the grass-dev
mailing list