• Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 539
  • Last Modified:

_beginthread() failing

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
lwinkenb
Asked:
lwinkenb
  • 8
  • 6
  • 6
2 Solutions
 
AxterCommented:
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
 
lwinkenbAuthor Commented:
The value of errno after the call is 0.  What does that mean?
0
 
AxterCommented:
>>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
Upgrade your Question Security!

Your question, your audience. Choose who sees your identity—and your question—with question security.

 
jkrCommented:
What's you thread's code?
0
 
lwinkenbAuthor Commented:
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
 
jkrCommented:
Have you tried to

DWORD dwTID;

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

if ( !hThread) {

DWORD dwErr = GetLastError();
}

?
0
 
AxterCommented:
Please post the code that actually calls beginthread
0
 
AxterCommented:
What type of project are you using this in?
Is it using the right thread library?
0
 
lwinkenbAuthor Commented:
>>  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
 
AxterCommented:
>>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
 
jkrCommented:
A 'lil' correction - when using 'CreateThread()', the threadshould be declared as

LONG WINAPI ConnectionThreadProc(void *param)

and just 'return 0;'

0
 
lwinkenbAuthor Commented:
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
 
AxterCommented:
What type of project are you using this in?
Is it using the right thread library?
0
 
lwinkenbAuthor Commented:
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
 
jkrCommented:
>>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
 
lwinkenbAuthor Commented:
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
 
jkrCommented:
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
 
lwinkenbAuthor Commented:
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
 
jkrCommented:
Um, actually, since that error also occurs when the thread start fails, it does not necessarily have to originate from the thread itself...
0
 
lwinkenbAuthor Commented:
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
Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.

Join & Write a Comment

Featured Post

Free Tool: Subnet Calculator

The subnet calculator helps you design networks by taking an IP address and network mask and returning information such as network, broadcast address, and host range.

One of a set of tools we're offering as a way of saying thank you for being a part of the community.

  • 8
  • 6
  • 6
Tackle projects and never again get stuck behind a technical roadblock.
Join Now