This is the mail archive of the
libc-help@sourceware.org
mailing list for the glibc project.
Malloc struct in one thread, free in another => SEGV
- From: "Christopher Taylor" <chtaylo3 at gmail dot com>
- To: libc-help at sourceware dot org
- Date: Fri, 18 Jul 2008 18:17:58 -0400
- Subject: Malloc struct in one thread, free in another => SEGV
- Dkim-signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=gmail.com; s=gamma; h=domainkey-signature:received:received:message-id:date:from:to :subject:mime-version:content-type:content-transfer-encoding :content-disposition; bh=afn4vRuVVipFsJn8swdnTeUcQFu4yOZWOMe+TnXwQzs=; b=jYSq9mjLLbu8B2fVZ+gDOOnzHp5j1f8CPK5m4XNEjBcEsfdYk82eMCjpkAmTLJKnc1 uMzlG7xK0kTIDRclDKyWvRiK9bshu3/62OatFNZitEtHHEmnVyuYpgNCkabisEzvuzc9 u96DweEZJ+6WanL7mgOHbJt8LPQdZxjFrUTM4=
- Domainkey-signature: a=rsa-sha1; c=nofws; d=gmail.com; s=gamma; h=message-id:date:from:to:subject:mime-version:content-type :content-transfer-encoding:content-disposition; b=DyCRliQmu8pR8qJAHhEvnUGbjMOiJUKUdWrZJ5lxR1r9De1OtBxe74QJ6n3tMOOIP+ lySPdWAPFlC9B/Qy93T0wQj8kg6Vf/PpvhJbUBdqONMAkzAGgu+cs0LjA5dfhM18oj4t SpmA+ZQcQDChvEG67vTo9byC4YKWCPrKgUJT0=
This is probably a pebkac issue and the workaround is obvious and
works but I'm curious why this doesn't?
The code below segv's at the indicated free.
Seems to me that malloc would keep track of an address and a size and
what I do with those and what structures that I populate that space
?should be? irrelvant to malloc/free. Either that's not the case or
(more likely) I'm doing something that's out in left field.
The code below is a simplicicaton of what I've built on my debian etch machine:
gcc version 4.1.2 20061115 (prerelease) (Debian 4.1.1-21)
libc6-dev 2.3.6.ds1-13etch5 GNU C Library: Development Libraries and
Header Files
NPTL 2.3.6
If I free the memory in the thread it was created everything works.
If instead of passing a prt to a struct, I pass an int .. everything works.
If I malloc the memory in chunks instead of all at once (the obvious
workaround mentioned above) ... everything works
Does anyone have any ideas?
Thanx for any assistance!
-Chris
typedef struct {
char *buffers;
int *amoutUsedPerBuffer;
int numBuffers;
} Thread_Response_t;
void*
workerThread(void* arg)
{
Thread_Response_t *tr = (Thread_Response_t*)malloc(
sizeof(Thread_Response_t) + //space for the struct
(sizeof(char) * MAX_NUM_BUFFERS * MAX_BUFFER_SIZE) + //space for the buffers
(sizeof(int) * MAX_NUM_BUFFERS)); //space for the int array
//do some stuff inclusing filling up the buffers
pthread_exit((void*)tr);
}
int
main (int argc, char **argv)
{
Thread_Response_t *tr;
pthread_t wtid;
pthread_create(&wtid, NULL);
pthread_join(wtid, &tr);
//printf some stuff out
free((void*)tr); //<=== segv here
return 0;
}