Skip to content
Advertisement

Error in malloc while trying to create a thread

Hi everyone i post only the core of the code which create probleam and which work with threads.

JavaScript

As we can see foo function create new thread and reallocate memory to store the pthread_t. Then it try to create a new thread with pthread_create as NULL as attr and arg and as function pointer a pointer to foo2;

Now the problem is that when i execute the code i have error with memory allocation when pthread_create is called that create this error message:

JavaScript

and if a print backtrace using gdb

JavaScript

How can i solve this problem, where is the problem.

Thanks to all for the patience and sorry for my english

Advertisement

Answer

Your thread allocation code is pointless because you only have 1 thread handle and accessing it threads[HR_OFF] is probably out of bounds. Casting (void) * &foo2 is wrong too, because foo2 has a proper signature like it should. Also retrying failed thread creation inside of loop is not a good idea either. Note that this loop is actually endless because you never increment counter. You should at least check failure reason before deciding to retry.

User contributions licensed under: CC BY-SA
9 People found this is helpful
Advertisement