0

I have a function with the prototype

DWORD WINAPI blah(LPVOID arg);

Which was meant to be used with CreateThread for a threaded app.

I call it with CreateThread with no problem. But then somewhere else in the code, I call it normally, just by blah(NULL). When it gets to this part, it crashes. Is this because the WINAPI part makes it __stdcall and you can't just call __stdcall functions like that?

tfkl
  • 71
  • 1
  • 2
  • Hard to say without the definition of `blah`. It could be a variety of things. – GManNickG Aug 08 '10 at 19:06
  • What is in the function? Does it dereference the null pointer you pass? – James McNellis Aug 08 '10 at 19:06
  • No, it doesn't use the argument. I was just wondering if the program would crash because it's __stdcall but I'm calling it not from CreateThread. – tfkl Aug 08 '10 at 19:08
  • You're asking about a step when you should be asking about the problem. Instead of the subproblem "will this crash because of `__stdcall`", you should just ask about the full problem (allowing us to see the function) "why does this crash when called without `CreateThread` like `...`", which may result in "oh, you can't call it with `__stdcall`." Never ask subproblems, only real problems. On a side note, calling it should be fine. – GManNickG Aug 08 '10 at 19:12
  • Are you calling it directly or through something like a callback? – Collin Dauphinee Aug 08 '10 at 19:52

2 Answers2

2

It is not because of __stdcall. Start your program in the debugger and check which line of the code gives you a crash.

Kirill V. Lyadvinsky
  • 97,037
  • 24
  • 136
  • 212
0

The only problem would be if blah() specifically calls TerminateThread(self) to end, instead of just returning off the bottom. The CreateThread call sets up the return address such that when blah() returns, it calls TerminateThread.

If blah() doesn't have any code like that, then an examination of the code is needed to see if it somehow does something thread specific which makes it fail. Offhand, I can't think of anything else (besides TerminateThread()) which might cause code written to be a thread which would prevent it from being called directly.

wallyk
  • 56,922
  • 16
  • 83
  • 148