Go Back   Computer Forums > General Computing > Programming
Click Here to Login
Join Computer forums Today


Reply
 
Thread Tools Search this Thread Display Modes
 
Old 03-10-2013, 12:57 AM   #1
Baseband Member
 
Join Date: Jun 2005
Posts: 65
Default C++ segfault when creating socket

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?
__________________

__________________
All the world's a computer, and all the men and women merely AI agents...
Bucky24 is offline   Reply With Quote
Old 03-10-2013, 08:02 AM   #2
Site Team
 
berry120's Avatar
 
Join Date: Jul 2009
Location: England, UK
Posts: 3,425
Default Re: C++ segfault when creating socket

I'm no C++ expert, but I'd guess that when you say this:
Quote:
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++!)
__________________

__________________
Save the whales, feed the hungry, free the mallocs.
berry120 is offline   Reply With Quote
Old 03-10-2013, 04:08 PM   #3
Baseband Member
 
Join Date: Jun 2005
Posts: 65
Default Re: C++ segfault when creating socket

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!
__________________
All the world's a computer, and all the men and women merely AI agents...
Bucky24 is offline   Reply With Quote
Old 03-12-2013, 08:31 AM   #4
Site Team
 
root's Avatar
 
Join Date: Mar 2004
Posts: 8,004
Default Re: C++ segfault when creating socket

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?
__________________
I didn’t fight my way to the top of the food chain to be a vegetarian…
Im sick of people saying 'dont waste paper'. If trees wanted to live, they'd all carry guns.
"The inherent vice of capitalism is the unequal sharing of blessings; The inherent vice of socialism is the equal sharing of miseries."
root is online now   Reply With Quote
Old 03-12-2013, 11:43 AM   #5
Baseband Member
 
Join Date: Jun 2005
Posts: 65
Default Re: C++ segfault when creating socket

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.
__________________
All the world's a computer, and all the men and women merely AI agents...
Bucky24 is offline   Reply With Quote
Old 03-13-2013, 02:56 PM   #6
In Runtime
 
Join Date: Feb 2013
Location: UK
Posts: 156
Default Re: C++ segfault when creating socket

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.
_michaelm is offline   Reply With Quote
Old 03-13-2013, 03:01 PM   #7
Baseband Member
 
Join Date: Jun 2005
Posts: 65
Default Re: C++ segfault when creating socket

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.
__________________

__________________
All the world's a computer, and all the men and women merely AI agents...
Bucky24 is offline   Reply With Quote
Reply

Tags
c++, networking, segfault, sockets

Thread Tools Search this Thread
Search this Thread:

Advanced Search
Display Modes

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are Off



All times are GMT -5. The time now is 02:57 AM.


Powered by vBulletin® Version 3.8.8 Beta 4
Copyright ©2000 - 2017, vBulletin Solutions, Inc.
Search Engine Friendly URLs by vBSEO 3.6.0