liamzdenek / go-pthreads

Lightweight binding of pthreads to Google Go
BSD 2-Clause "Simplified" License
36 stars 8 forks source link

Possibly returning a function local variable address ? #5

Open kpdev74 opened 6 years ago

kpdev74 commented 6 years ago

I could be mistaken but I feel that func Create (), can return address of a local variable which gets destroyed after the end func Create(). At line 55 in func Create (), pidptr is defined to point to address of function local variable "pid" which stores the pthread_t returned by pthread_create() library call. pthread_create() requires the address of a variable (as is normal for all kernel calls) to return the pthread_t value. Now, since we return the address of local variable "pid" after converting it to a unsafe.Pointer, the garbage collector of 'go' will not add an extra reference to the local variable "pid" and when we return the said value, it may get overwritten. Any subsequent attempt to kill the thread represented by 'Thread' may now work or may not depending on whether the local variable value was overwritten or otherwise. It's probably best to ask the user to supply a type 'Thread' as an argument to func Create() and return the pid in the passed caller variable, than allocating one on stack of func Create(). Makes sense ?

This might also perhaps explain the error seen by kryptodev.