views:

323

answers:

2

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?

+1  A: 

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
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
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
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
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
Very strange. I haven't seen anything like this before.
Len Holgate
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
graham.reeds
Did you ever find the cause of this?
Len Holgate
A: 
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
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.