C++ segfault when creating socket

Bucky24

Baseband Member
Messages
65
I've got a very frustrating error to debug right now. I'm using the following code to create a network socket in C++:

Code:
60. int Network::openServerSocket(int portno) {
61.	struct sockaddr_in serv_addr;
62.
63.	int sockfd = socket(PF_INET, SOCK_STREAM, 0);

.....

When I run this code, I get a segfault. I ran the program through gdb and got the following:

Code:
Program received signal SIGSEGV, Segmentation fault.
0x08059210 in socket ()
(gdb) where
#0  0x08059210 in socket ()
#1  0x0805129f in Network::openServerSocket (portno=13546) at Network.cpp:63
(line 63 being the socket call).

I've looked all over google, but seems like everyone else who has a problem like this actually is getting a segfault somewhere else in their code, rather then from the socket function itself.

I've used this code before (though it was a while ago), so I'm fairly sure it should work. Any ideas as to why I'm getting a segfault here?
 
I'm no C++ expert, but I'd guess that when you say this:
I've looked all over google, but seems like everyone else who has a problem like this actually is getting a segfault somewhere else in their code, rather then from the socket function itself.
...this is probably the case with your code also. I see nothing inherently wrong with what you've posted, but it is undoubtedly part of a larger codebase. If you post more of the code in question we may be able to help (though bear in mind it's been a long time since I've touched C++!)
 
Hmm looks like you're right. I wrote a basic C++ program with just that socket line (which is probably what I should have done in the first place) and it worked fine... If it's something else in my code, I can probably track it down and root it out. Was just confusing since the segfault comes directly from the socket command, so I thought there might have been a problem with the socket library. Thanks!
 
There is nothing wrong with that line of code, so your problems are elsewhere.

Is this on windows of Linux, (or Unix) there are slightly different socket creation "issues" on each due to non standard implementation. (so the Winsock.h headers on windows are not quite the same and the socket.h headers on Linux.)

Do you have the rest of your socket creation code?

what are you trying to do? (Are you opening this socket to listen to create a server program, or open a connection to read/write to create a client program?
 
Sorry, forgot to post here that I figured out the issue.

Turns out I'd created a global variable "int socket", and that had overwritten the function declaration. Changed the name of that variable and it worked fine.

So... I guess to anyone else having this problem: Check the global variables you have defined (or class members, if you're calling this within a class).

Note: I'd mark this thread "Resolved" but am not entirely sure how, looks like I can't edit my first post.
 
Glad you got it sorted. I've had similar things to this before in C++ code. The important lesson to remember is that a segfault isn't necessarily caused by the last line of execution your program reaches, but rather because you're attempting to access a corrupted memory region.

A scenario which can cause the same error is given below:

1) At at earlier point in your program (probably very shortly before this) you allocated some memory to an array/object (doesn't really matter)
2) You've written more data into this area of memory than there was space for (i.e. buffer overflow)
3) This corrupted the memory table (where malloc/free aka new/delete stores its information)
4) Your call to socket() attempts to create a new object directly after the previous one, and when it looks in the memory table to find the next area of free memory - reads corrupted data and bang goes your program, aka segfault.

This is one of those scenarios that catches even the best programmers - but once you see a segfault from a line of code which is perfectly fine, you'll go straight to this scenario and should be up a running again in no time.

Hope that helps.
 
Yeah, that's a really good series of steps to remember.

I think the main problem is that I have just come back to C++ after working with Java for a year or so, so I was treating the segfault like an exception-something caused inside the function that threw it. Which obviously is not the case with C++... It's going to be an interesting transition again.
 
Back
Top Bottom