LinuxQuestions.org
Review your favorite Linux distribution.
Home Forums Tutorials Articles Register
Go Back   LinuxQuestions.org > Forums > Non-*NIX Forums > Programming
User Name
Password
Programming This forum is for all programming questions.
The question does not have to be directly related to Linux and any language is fair game.

Notices


Reply
  Search this Thread
Old 12-26-2007, 03:14 AM   #1
nc3b
Member
 
Registered: Aug 2005
Posts: 330

Rep: Reputation: 32
Question fdopen causes segmentation fault


Hello. I have the following problem. Everything wen fine, and all of the sudden, the program began crashing (with segmentation fault)

this is what gdb complains about

Code:
*** glibc detected *** malloc(): memory corruption: 0x0805b3a8 ***

Program received signal SIGABRT, Aborted.
[Switching to Thread 16384 (LWP 794)]
0x401adc81 in kill () from /lib/libc.so.6
(gdb) bt
#0  0x401adc81 in kill () from /lib/libc.so.6
#1  0x400274a1 in pthread_kill () from /lib/libpthread.so.0
#2  0x4002787b in raise () from /lib/libpthread.so.0
#3  0x401ad8f8 in raise () from /lib/libc.so.6
#4  0x401aef00 in abort () from /lib/libc.so.6
#5  0x401e16ce in __libc_message () from /lib/libc.so.6
#6  0x401e8781 in _int_malloc () from /lib/libc.so.6
#7  0x401ea2f1 in malloc () from /lib/libc.so.6
#8  0x401d8912 in fdopen@@GLIBC_2.1 () from /lib/libc.so.6
#9  0x0804c609 in client (this=0x805b338, _sock=8, _pin=
      {sin_family = 2, sin_port = 60164, sin_addr = {s_addr = 83994816}, sin_zero = "\000\000\000\000\000\000\000"}) at client.cpp:28
#10 0x08049760 in main (argc=1, argv=0xbffff794) at main.cpp:101
(gdb) quit
client.cpp:28
Code:
this->rx=fdopen(_sock,"r");
I double checked, and _sock is greater than 0 and so on (you can see right from the output, here _sock=8). Can you please tell me what I am doing wrong? What really bugs me is that it doesn't consistently crash, but only at times. And after linking it with -lefence it stopped crashing:| . Now I removed the -lefence and it doesn't crash anymore. Don't know what to say.. Should I leave the -lefence?
 
Old 12-26-2007, 04:24 AM   #2
SciYro
Senior Member
 
Registered: Oct 2003
Location: hopefully not here
Distribution: Gentoo
Posts: 2,038

Rep: Reputation: 51
It doesn't appear to be a segfault, but a abort, libc detected corrupted memory. This type of error is quite random, as its caused by another part of the program writing to a memory location within the program (so it doesn't cause a segfault), the valid write only gets noticed latter, at a diffrent place in the program. The time and conditions that this type of error generate a abort (or segfault) are effected by the layout of the program. Linking against another library, or even changing one like (say, adding a 'printf("hi\n")', can change the location of where the error manifests, or even if it does. These errors are hard to track down by hand, all the code is valid, and the logic might appear quite valid as well, especially since you don't know where in your program the memory gets corrupted, only when it gets noticed. Your best chance is to use a tool like valgrind to locate code thats writing to memory locations it should not write to.

Last edited by SciYro; 12-26-2007 at 04:27 AM.
 
Old 12-26-2007, 04:33 AM   #3
nc3b
Member
 
Registered: Aug 2005
Posts: 330

Original Poster
Rep: Reputation: 32
Wow. Thanks This really sounds quite sensible. In the past I have often been confused by the fact that the program crashed if I commented out a cout or something.. Now I finally understand
I will try to use valgrind, thank you very much for your help. Cheers
 
  


Reply



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 Off
HTML code is Off



Similar Threads
Thread Thread Starter Forum Replies Last Post
yast segmentation fault, system freezing - nvidia driver at fault? BaltikaTroika SUSE / openSUSE 2 12-02-2005 09:34 AM
Segmentation fault santhosh_o Programming 3 10-26-2004 05:45 AM
What is segmentation fault? koyi Programming 3 08-30-2003 12:03 AM
segmentation fault... perdesiz Linux - Software 1 08-18-2003 01:55 AM
Segmentation fault ifconfig Linux - Software 2 05-19-2003 09:26 PM

LinuxQuestions.org > Forums > Non-*NIX Forums > Programming

All times are GMT -5. The time now is 09:36 PM.

Main Menu
Advertisement
My LQ
Write for LQ
LinuxQuestions.org is looking for people interested in writing Editorials, Articles, Reviews, and more. If you'd like to contribute content, let us know.
Main Menu
Syndicate
RSS1  Latest Threads
RSS1  LQ News
Twitter: @linuxquestions
Open Source Consulting | Domain Registration