0

My app can switch between Serial and Socket connections, but neither can be open at the same time. So when I try to switch between a Socket to Serial, I delete the Socket connection.

The socket based object uses a private class called UsesWinsock (a big shout out to Len Holgate here as it is basically his code (it is RAII actually but I never applied it to WSA until I saw him do it)). This simply calls WSAStart and and WSACleanUp.

When WSACleanUp is called it tails off into the NTDLL and assembler. @err,hr gives me "The data area passed to a system call is too small" as the HRESULT.

I've tried moving the code around: Moving the class in the order it is inherited causes the destruction sequence to change and actually broke the functions out so they were the last things to be called in the destruct sequence. Neither worked.

I'm pretty sure I've freed everything that is allocated (there's only 1 socket and a couple of events) and I am stuck - this is a fairly important problem for me.

Google is unhelpful returning 4 results of which none solve my problem.

Any suggestions?

Community
  • 1
  • 1
graham.reeds
  • 16,230
  • 17
  • 74
  • 137

2 Answers2

1

So, are you using my UsesWinsock class as is? That is, are you saying that it's broken? If so an email directly to me might be a more efficient way of solving the problem ;)

I notice that my code doesn't actually check the return code from WSACleanUp() in the dtor, so, I'm assuming you are checking this, it's SOCKET-ERROR and WSAGetLastError() is reporting ERROR-INSUFFICIENT-BUFFER ?

Len Holgate
  • 21,282
  • 4
  • 45
  • 92
  • I am not using your class as-is, but it does look suspiciously like it. There is a phobia about using outside code here so I had to rewrite it. But once the idea is seeded how can you un-seed it? The actual HRESULT error is in the watch window as @err,hr. CleanUp never actually gets to return. – graham.reeds Feb 16 '09 at 12:04
  • Strange. I've never seen that happen. Is something else unloading the winsock dlls before this call happens? Do you have other calls to WSAStartup and WSACleanUp? What's the smallest example program that demonstrates the problem; etc... – Len Holgate Feb 16 '09 at 12:44
  • Nothing else uses WinSock in the code so it shouldn't get unloaded. Nothing appears in the debug window suggesting that it is unloading. As for smallest example, I'd have to look into it. – graham.reeds Feb 16 '09 at 13:23
  • I've done a bit more digging and rearranging. I placed the code at the outermost edge of the code as part of the WinApp. This still causes the same error, though I do get a flash of the exception message of invalid handle at 0xc0000008. – graham.reeds Feb 16 '09 at 13:39
  • Very strange. I haven't seen anything like this before. – Len Holgate Feb 16 '09 at 14:09
  • The kind of random guess that I tend to go with in these situations is that I'm screwing with memory that I don't own somewhere else and this is causing the problem. Is that a possibility? (if you listen carefully you'll hear some straws being clutched at...). – Len Holgate Feb 16 '09 at 23:46
  • Yeah. I am clutching at straws here too. I've moved it to the edge so I can continue working. Hopefully when I get it working I can then start to tidy & refactor and that will fix whatever I broke... – graham.reeds Feb 18 '09 at 10:24
  • Did you ever find the cause of this? – Len Holgate Jul 17 '09 at 16:12
0

I'd be interested to know what you'd find if you used the wt "trace and watch data" command in windows debugger:

Set a break point at the beginning of the WSACleanup:

bp ws2_32!wsacleanup

Once hit, issue the trace command:

wt -oa -oR @$ra

and watch out for calls to ntdll!RtlSetLastWin32Error

You could also post the findings here, it'd interesting to look at them.

deemok
  • 2,735
  • 19
  • 11
  • Hmmm. Never used the windows debugger. When I get this module working I will do some refactoring. If that doesn't fix it then I will have a try at the windows debugger. There's an article on codeproject iirc on it. – graham.reeds Feb 18 '09 at 10:26
  • it could pinpoint your problem right away though. not knowing the architecture of your software it is hard to judge, but knowing which internal call (invoked by WSACleanup) fails would be a definite step forward. – deemok Feb 18 '09 at 10:34