Click here to Skip to main content
15,887,334 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more: , +
What I want to do?
I just want to creat a child thread,which counts the child folder numbers in some folder with recursive traversal method. The PATH is parameter of my recursive function
C++
void myclass::FolderNum(LPTSTR path)
{
HANDLE hFile;
...
findfirstfile(...);
...
do
{
if(/*a folder*/)
{
...                        //get new PATH;
FolderNum(path);//recrusion
}
}while(findnextfile(...));
closefile(hFile);
}

Here the problem comes!
If put in main thread, my code runs well. While in child thread, it gets sick!!! When debuging, I found the parameter of my recursive function-PATH-were not been pushed into the thread stack,or at least, there's something wrong when PATH pushed. Because the PATH appears in Garbled characters when my code jump out from first recursion

By the way,
VS2008, WTL, all my functions except thread callback function are member functions of a class(it means, my thread callback function was a friend function of this class.)

So...
1、Any advise? vs2008 debugger problems? Should I use WinDBG? Is there better tools for debugging multithread programs? Or,tips of thread stack especially child thread stack...
2、Thank you in advance.
3、Sorry for my poor English.
ADD:
//these codes are in a member function, creating a thread
C++
TCHAR szCurrentPath[MAX_PATH];       
	GetCurrentDirectory(MAX_PATH,szCurrentPath);	
	dwFolders=1;
	dwFiles=0;
	nPos=0;
	stepLenth=0;


	//Fill the  struct (passed to the thread function)
	threadpara *pa=new threadpara;
	pa->hDll=hDll;
	pa->myself=this;
	pa->szf=szCurrentPath;

	DWORD threadId;
	HANDLE thr;
	thr=CreateThread(NULL,0,(LPTHREAD_START_ROUTINE)ThreadScan,(LPVOID)pa,0,&threadId);


//ThreadScan is the thread function.
C++
void ThreadScan(LPVOID lpv)

{
	Sleep(2000);
	threadpara *p=(threadpara *)lpv;
	//
	myclass* first=p->myself;
	//
	LPTSTR current=p->szf;
	HINSTANCE dll_unload=p->hDll;
	first->FolderNum(current);	
	ExitThread(0);


}
Posted
Updated 1-Apr-12 15:24pm
v2
Comments
Chuck O'Toole 18-Feb-12 22:42pm    
You really need to post the code that calls this "as a thread" or at least the code that creates the thread. If the argument isn't set up correctly, look to the caller, not the callee.
pxdbxq 19-Feb-12 20:57pm    
Thank you...
nv3 19-Feb-12 17:16pm    
From what you are saying, it looks like your initial thread has already terminated or at least deleted your threadpara structure. Hart to tell without seeing the full code, but from your observation it looks that way. Make sure that threadpara lives at least as long as giving your second thread a chance of receiving it.
pxdbxq 19-Feb-12 20:56pm    
When debugging it, I'm sure ThreadScan has got the threadpara structure. local variables like p,first,current, are corrent.
thank you.Now I try to debug it with winDbg. or I'll change the structure of my project.Thank you....

1 solution

Well, here's an educated guess. There isn't enough code posted to be really sure but....

Inside your thread, you are using 'current' as the path. 'current' is a *pointer* to a string of characters.

That pointer is obtained from the 'szf' field of your 'threadpara' object. I'm assuming that 'szf' is also a *pointer* (the definition of threadpara is not shown).

'szf' is a pointer to the 'szCurrentPath' array, which appears to be the only actual place where the string exists, that is, nobody has a *copy* of the string, only copies of *pointers* to the string.

You do not show what happens after the CreateThread() call but presumably the calling thread continues to run.

If the code you posted is inside a function/procedure, then the 'szCurrentPath' is a local variable on the stack. That array will disappear when the function returns. So, you may very well be carrying around pointers to a string that has been returned to the plasma pool, been reused, overwritten, clobbered, etc so by the time your thread runs, it's input data is clobbered.

Rather than carry around a pointer, at some point somebody should make a more permanent copy of the path string or at least define the TCHAR array to be somewhere that has a longer lifetime than the thread that needs it.
 
Share this answer
 
v2
Comments
pxdbxq 20-Feb-12 3:40am    
The analysis is useful and excellent. Maybe that's the point! I'll try to make some experiments.
...
It proves you're right.Thank you!I'm exciting!!! Thank you very very very much!!
Chuck O'Toole 20-Feb-12 8:51am    
Thank you, glad to be of help. Now maybe the person who downvoted my answer will re-consider now that it was proven to be spot on.
[no name] 1-Apr-12 23:16pm    
"Now maybe the person who downvoted my answer will re-consider"
I am not going to put money on that but my vote may help.
Chuck O'Toole 1-Apr-12 23:26pm    
Thanks.

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900