|
From: Russ F. <rus...@ho...> - 2003-10-25 18:21:32
|
> > I'm getting some strange message (aren't we all) that I'm having trouble
> > with understanding. I'm doing a memset(0) on a pointer prior to calling
> > free(). Valgrind complains it's an invalid write of size 4. However,
>if I
> > reduce the memset by ONE byte, I don't get any invalid write at all.
>Here
> > is the message:
> >
> > ==28655== Invalid write of size 4
> > ==28655== at 0x402E426D: memset (../sysdeps/i386/memset.c:65)
> > ==28655== by 0x804C9F9: delete_foo (foo.c:80)
> > ==28655== by 0x804B1ED: process_bar (bar.c:606)
> > ==28655== Address 0x40D6DA00 is 0 bytes after a block of size 96
>alloc'd
> > ==28655== at 0x4002B905: malloc (vg_replace_malloc.c:153)
> > ==28655== by 0x804C99F: new_foo (foo.c:66)
> > ==28655==
> >
> > If I change the memset from memset(foo, 0, sizeof(foo_struct)) to
> > memset(foo, 0, sizeof(foo_struct)-1), I get no error. I would expect to
>get
> > "invalid write of size 3" if in fact this memset is the problem.
>
>When you subtract one from the size memset will have to write the
>last three bytes a byte at a time. Without the subtraction it will
>write the whole of the last word in one go. When valgrind reports
>an invalid write of size 4 it means the instruction was trying to
>write four bytes, but it doesn't mean all four bytes are invalid.
>
>My guess is that you have malloced one byte to little, so the memset
>is overrunning the end of the block by one byte.
>
>Tom
That sounds like a reasonable explanation. I'm sure I'm allocing the
correct amount, because I'm a stickler for doing sizeof's and such. Unless
I'm mistaken, my struct foo has a bunch of pointers:
struct foo {
char* this;
char* that;
char* theother;
int a;
int b;
...
} and so on.
To malloc it, I'm doing "struct foo* f = (struct foo*)malloc(sizeof(struct
foo));" Of course, when I need to use the char*'s, I malloc them
separately. I even tried aligning the malloc on a 32 bit boundary by
maintaining a separate counter and padding by up to 3 bytes, but that didn't
work. I wonder if there's some other portion of the code making illicit
changes and is causing "referred pain" by valgrind. Has anyone noticed such
a thing before?
I'll keep pecking away, but in the meantime, thanks to all for the
suggestions.
Russ
_________________________________________________________________
Cheer a special someone with a fun Halloween eCard from American Greetings!
Go to http://www.msn.americangreetings.com/index_msn.pd?source=msne134
|