Mysterious Segmentation Fault around WSARecv

Started by
6 comments, last by hplus0603 13 years, 9 months ago
I'm using C++ and ( currently ) targeting Windows.
I wrote a socket handler class for easier use ( it uses select btw ), and I want to port it to Windows, to use Overlapped I/O, because I've read it's much better, especially for handling many connections.
Receiving is built around a loop, which is started by StartReceive(), then continued by checkRecv() and Recv().

The class may have more problems, but I want to solve this one first: at succesfull connecting, StartReceive() is called, which crashes the app by calling WSARecv. It gets a Segmentation Fault, and can't figure out why.

Here's a pastebin: http://pastebin.com/P9mxDR0d
I tried to make it well-commented.

[ Sorry, English is not my native language, if I didn't give some needed information, just ask ]
Advertisement
You don't allocate the data for the buffers, only the buffer structures themselves.

Btw: class names that start with "_" are reserved for the standard library, and class names that start with "__" are reserved for the compiler. Your class name is not legal C++ (although your compiler probably doesn't complain).
enum Bool { True, False, FileNotFound };
Thanks for your reply, but I think I misunderstood what you say.
In the constructor I allocate some data, and set the struct's buf member to the pointer which the allocation gave. ( and set the size of course )
In the loop, I reallocate some data, and update the struct's members.
( And my eCBuffer class works as intended, sorry, I didn't mention it )
Could you point a bit closer to the source of my problem, please?
Sorry, your mysterious naming convention confused me.
I still don't see why you deallocate and re-allocate the buffer, though.
I suggest you find a reproducible case without randomness, and use data (memory) breakpoints to see what's going on.
enum Bool { True, False, FileNotFound };
The size of the packets are not constant, and I want to receive only one packet at once. So at first I wait for the size of the packet, then deallocate a buffer, and allocate one with the size of the incoming packet.

I'll try breakpoints and then edit this post.
Uff, I feel myself a bit dumb now. The problem was with WSARecv, that I gave a value for flags instead of a pointer to a DWORD (" __inout LPDWORD lpFlags" in the msdn library).
I'll have to polish the class a bit, but it looks like it will work.
And thank you for your help and effort to understand my code :D

Btw: So underscores before names are reserved, but are underscores *after* the name reserved? For example, is "foo_" valid?
Quote:Original post by TheUnnamable
Btw: So underscores before names are reserved, but are underscores *after* the name reserved? For example, is "foo_" valid?
Yes, however it doesn't add anything to the variable name which makes it a questionable choice. (Prefixes and suffixes aren't normally used in programming except for say Windows Forms where Hungarian notation is used for controls).

One convention is to use a trailing underscore for member variables.

class foo {    public void func(int arg) {      arg_ = arg;    }    int arg_;};


This is more concise than "m_foo" while still being distinctive.
enum Bool { True, False, FileNotFound };

This topic is closed to new replies.

Advertisement