[Last Call] Learn how to a build a cloud-first strategyRegister Now

x
?
Solved

_beginthread() failing

Posted on 2005-04-21
20
Medium Priority
?
534 Views
Last Modified: 2013-12-03
I'm calling:
_beginthread(MyThreadProc, 0, (void*)pData);

_beginthread() is returning -1 which means it failed.  How do I figure out why it's failing?
0
Comment
Question by:lwinkenb
  • 8
  • 6
  • 6
20 Comments
 
LVL 30

Expert Comment

by:Axter
ID: 13837482
Hi lwinkenb,
> >How do I figure out why it's failing?
Check the value of errno and/or _doserrno

These are global variables.

David Maisonave :-)
Cheers!
0
 
LVL 5

Author Comment

by:lwinkenb
ID: 13837494
The value of errno after the call is 0.  What does that mean?
0
 
LVL 30

Expert Comment

by:Axter
ID: 13837512
>>The value of errno after the call is 0.  What does that mean?

That usually means no error.

Are you checking it immediately after calling _beginthread?
It should be the verry first line after calling beginthread.

Also, check the value of _doserrno, to see what's there.
0
NEW Veeam Agent for Microsoft Windows

Backup and recover physical and cloud-based servers and workstations, as well as endpoint devices that belong to remote users. Avoid downtime and data loss quickly and easily for Windows-based physical or public cloud-based workloads!

 
LVL 86

Expert Comment

by:jkr
ID: 13837619
What's you thread's code?
0
 
LVL 5

Author Comment

by:lwinkenb
ID: 13837740
Here is the code where I call _beginthread()

My thread procedure looks like:
void ConnectionThreadProc(void *param)
{
      PIRCDATA                  ircData = (PIRCDATA)param;
      struct sockaddr_in      sockaddr;
      struct hostent            *h;
      int                              ret;
      char                        buff[256];
      char                        *message;
      int                              message_size;

      ircData->s = socket(AF_INET,SOCK_STREAM,0);
      if(ircData->s == INVALID_SOCKET) return;

      #ifdef UNICODE
      {
            char asciihost[128];
            wcstombs(asciihost,ircData->szServer,sizeof(asciihost));
            h = gethostbyname(asciihost);
      }
      #else
            h = gethostbyname(ircData->szServer);
      #endif
      if(!h)
      {
            return;
      }
      // etc

Using the debugger, it doesn't look like the thread procedure is ever entered.
0
 
LVL 86

Expert Comment

by:jkr
ID: 13837784
Have you tried to

DWORD dwTID;

HANDLE hThread = CreateThread(NULL,0,ConnectionThreadProc,(LPVOID)pData,0,&dwTID);

if ( !hThread) {

DWORD dwErr = GetLastError();
}

?
0
 
LVL 30

Expert Comment

by:Axter
ID: 13837854
Please post the code that actually calls beginthread
0
 
LVL 30

Expert Comment

by:Axter
ID: 13837874
What type of project are you using this in?
Is it using the right thread library?
0
 
LVL 5

Author Comment

by:lwinkenb
ID: 13837892
>>  Please post the code that actually calls beginthread
oops, forgot to paste that in my last comment:

int NewConnection(PIRCDATA ircData)
{
      int ret;
      TCHAR      buff[128];
      ret = _beginthread(ConnectionThreadProc,0,(void*)ircData);      
      if(ret == -1)
      {
            sprintf(buff,"Error in _beginthread().  errno = %d\n",errno);
            MessageBox(NULL,buff,"ERROR",MB_OK);
      }
      return 0;
}

jkr, I'm not doing it that way because of the following from MSDN:
"A thread in an executable that calls the C run-time library (CRT) should use the _beginthread and _endthread functions for thread management rather than CreateThread and ExitThread;"
0
 
LVL 30

Expert Comment

by:Axter
ID: 13837973
>>sprintf(buff,"Error in _beginthread().  errno = %d\n",errno);

You shouldn't use above method to dispay the error.

Try the following instead:

     int ret;
     TCHAR     buff[128];
     int tmperr;
     ret = _beginthread(ConnectionThreadProc,0,(void*)ircData);    
     if(ret == -1)
     {
          tmperr = errno;
          sprintf(buff,"Error in _beginthread().  errno = %d\n",tmperr);
          MessageBox(NULL,buff,"ERROR",MB_OK);
     }
0
 
LVL 86

Expert Comment

by:jkr
ID: 13837992
A 'lil' correction - when using 'CreateThread()', the threadshould be declared as

LONG WINAPI ConnectionThreadProc(void *param)

and just 'return 0;'

0
 
LVL 5

Author Comment

by:lwinkenb
ID: 13838113
I got the same results with your code Axter.  

Here is another note, when I run in debug mode I get the following error after I close the program:

Debug Error
R6017
- unexpected multithread lock error
0
 
LVL 30

Assisted Solution

by:Axter
Axter earned 1000 total points
ID: 13838409
What type of project are you using this in?
Is it using the right thread library?
0
 
LVL 5

Author Comment

by:lwinkenb
ID: 13838512
The project is a win32 application.  
>> Is it using the right thread library?
I don't know, how would I tell?  I did select for it to use the multithreaded version of the CRT in the project settings.
0
 
LVL 86

Expert Comment

by:jkr
ID: 13838540
>>I don't know, how would I tell?

You would get linking errors if not. Have you tried 'CreateThread()'? That might help to shed in some more light about what is going wrong...
0
 
LVL 5

Author Comment

by:lwinkenb
ID: 13838598
When I use CreateThread() it works (new thread starts executing), but I still get the following error after I close the application.
Debug Error
R6017
- unexpected multithread lock error
0
 
LVL 86

Accepted Solution

by:
jkr earned 1000 total points
ID: 13838624
That might be caused by wht your thread code does. Since the main portion is missing, it's hard to diagnose that. The error description is:

"This error usually occurs if the process inadvertently alters the run-time heap data. However, it can also be caused by an internal error in the run-time or operating-system code."
0
 
LVL 5

Author Comment

by:lwinkenb
ID: 13838640
I finally ended up figuring out the problem.  It was a buffer overflow somewhere else in my program which involved the parameter I was passing to _beginthread(). Thanks for taking the time to help,  I'll split up the points between you.
0
 
LVL 86

Expert Comment

by:jkr
ID: 13838651
Um, actually, since that error also occurs when the thread start fails, it does not necessarily have to originate from the thread itself...
0
 
LVL 5

Author Comment

by:lwinkenb
ID: 13839397
Well in this case I basically did:

SOMESTRUCT *param = malloc(sizeof(param));
param->values = something;
_beginthread(ThreadProc,0,(LPVOID)param);

I didn't allocate enough memory for my parameter (I only allocated sizeof(void*) bytes), so that's why _beginthread() was crashing.
0

Featured Post

Free Backup Tool for VMware and Hyper-V

Restore full virtual machine or individual guest files from 19 common file systems directly from the backup file. Schedule VM backups with PowerShell scripts. Set desired time, lean back and let the script to notify you via email upon completion.  

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

This article surveys and compares options for encoding and decoding base64 data.  It includes source code in C++ as well as examples of how to use standard Windows API functions for these tasks. We'll look at the algorithms — how encoding and decodi…
With most software applications trying to cater to multiple user needs nowadays, the focus is to make them as configurable as possible. For e.g., when creating Silverlight applications which will connect to WCF services, the service end point usuall…
This is Part 3 in a 3-part series on Experts Exchange to discuss error handling in VBA code written for Excel. Part 1 of this series discussed basic error handling code using VBA. http://www.experts-exchange.com/videos/1478/Excel-Error-Handlin…
This lesson discusses how to use a Mainform + Subforms in Microsoft Access to find and enter data for payments on orders. The sample data comes from a custom shop that builds and sells movable storage structures that are delivered to your property. …

829 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question