Mysterious Segmentation Fault around WSARecv

This topic is 3105 days old which is more than the 365 day threshold we allow for new replies. Please post a new topic.

Recommended Posts

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 ]

Share on other sites
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).

Share on other sites
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?

Share on other sites
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.

Share on other sites
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.

Share on other sites
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?

Share on other sites
Quote:
 Original post by TheUnnamableBtw: 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).

Share on other sites
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.

• What is your GameDev Story?

In 2019 we are celebrating 20 years of GameDev.net! Share your GameDev Story with us.

• 17
• 14
• 10
• 9
• 11
• Forum Statistics

• Total Topics
634094
• Total Posts
3015498
×