I have the following piece of test code:
int main(int argc, char *argv[])
{
int sock = 7; /* to simplify test case */
struct event e0;
struct event *e1;
event_init();
// works fine: ---------------
printf("Starting test 0...\n");
event_set(&e0, sock, EV_READ, handle_new_connection, &e0);
event_add(&e0, NULL);
//----------------------------
// doesn't work: -------------
printf("Starting test 1...\n");
e1 = (struct event*) malloc(sizeof(struct event)); // <<<<<<
if (e1==NULL)
{
printf("malloc failed\n");
exit(1);
}
event_set(e1, sock, EV_READ, handle_new_connection, e1);
event_add(e1, NULL);
//----------------------------
return 0;
}
The first test (event structure on the stack) works correctly, however the
malloc'ed structure seems to cause problems in event_set. Output from
valgrind:
Starting test 0...
Starting test 1...
==11398== Invalid write of size 4
==11398== at 0x402CA99: event_set (event.c:516)
==11398== by 0x8048668: main (test6.c:33)
==11398== Address 0x4172110 is 0 bytes after a block of size 72 alloc'd
==11398== at 0x401C405: malloc (vg_replace_malloc.c:149)
==11398== by 0x804862A: main (test6.c:26)
==11398==
...
"0 bytes after a block of size 72 alloc'd" apparently means that something
is accessing one byte after the allocated memory.
Can anyone see what I'm doing wrong?
I am running libevent-1.4.8-stable, and sizeof(struct event) is 72 (i386
system).
thanks,
Tom
_______________________________________________
Libevent-users mailing list
[email protected]
http://monkeymail.org/mailman/listinfo/libevent-users