Hi there,
I have used beginthread() in my driver to create a thread and I allocate the
stack in my program instead of letting the system to allocate the stack. The
reason is that I want to be able to free the stack evertime the thread end
so I had the program like the folllowing:
main()
{
char *args[2];
args[0]=(char *)malloc(STACK_SIZE);
_beginthrad(child,args[0],STACK_SIZE,args);
…
…
}
child(void far *parm)
{
char * stack =parm[0];
…
…
free(stack);
_endthread();
}
So my question is that can I free the stack before calling _endthared()? I
have run the program for some stress test, and so far, it doesn’t seem has
any problem, but I am still wondering is there any good suggestion for the
stack free up.
Thanks in advance!
Teresa
Teresa Tao <ttao@neontech.com> wrote:
Hi there,
I have used beginthread() in my driver to create a thread and I allocate the
stack in my program instead of letting the system to allocate the stack. The
reason is that I want to be able to free the stack evertime the thread end
so I had the program like the folllowing:
main()
{
char *args[2];
args[0]=(char *)malloc(STACK_SIZE);
_beginthrad(child,args[0],STACK_SIZE,args);
.
.
}
child(void far *parm)
{
char * stack =parm[0];
.
.
free(stack);
_endthread();
}
So my question is that can I free the stack before calling _endthared()? I
have run the program for some stress test, and so far, it doesn’t seem has
any problem, but I am still wondering is there any good suggestion for the
stack free up.
The question is, does _endthread(), or the act of calling _endthread()
write anything to the stack? If so, this is dangerous in that if you
get pre-empted after free() by another thread that calls malloc(), it
could get the memory you just freed, and then you would over-write its
new memory with whatever hit the stack in calling/inside _endthread().
I’d say the answer is that no, this isn’t safe.
-David