The UNIX and Linux Forums  
Hello and Welcome from United States to the UNIX and Linux Forums! Thank You for Visiting and Joining Our Global Community.

Go Back   The UNIX and Linux Forums > Top Forums > High Level Programming
.
google unix.com



High Level Programming Post questions about C, C++, Java, SQL, and other programming languages here.

More UNIX and Linux Forum Topics You Might Find Helpful
Thread Thread Starter Forum Replies Last Post
Segmentation fault big123456 Linux 0 07-20-2007 05:01 AM
Segmentation fault rshaikh AIX 2 04-16-2007 08:12 AM
Segmentation Fault compbug UNIX for Dummies Questions & Answers 3 04-21-2006 10:43 AM
segmentation fault wojtyla High Level Programming 3 02-19-2005 02:53 PM
Segmentation fault jshaulis AIX 1 06-01-2004 04:16 PM

Closed Thread
English Japanese Spanish French German Portuguese Italian Dutch Swedish Russian Norwegian Hungarian Hebrew Danish Powered by Powered by Google
 
LinkBack Thread Tools Search this Thread Rate Thread Display Modes
  #1 (permalink)  
Old 08-25-2005
fermisoft's Avatar
fermisoft fermisoft is offline
Registered User
  
 

Join Date: Jun 2005
Location: Mumbai
Posts: 16
fwrite throws segmentation fault

Code : function sSaveTFFile
Code:
             .......................
	iRetCode = link (caCurrentFilename, caBackupFilename);
	if (iRetCode == -1)
	{
		ERR_MSG2(LOG_ALERT, "Can't move %s to %s", caCurrentFilename, caBackupFilename);
		return(FAILURE);
	}
	
	iRetCode = unlink (caCurrentFilename);
	if (iRetCode == -1)
	{
		ERR_MSG2(LOG_ALERT, "Can't move %s to %s", caCurrentFilename, caBackupFilename);
		return(FAILURE);
	}


	fpTFFile = fopen (caCurrentFilename, "wb"); 
	if (fpTFFile == NULL)
	{
		ERR_MSG1 (LOG_ALERT, "Can't open %s\n", caCurrentFilename);
		return (FAILURE);
	}

/*
the program crashes here while calling the fwrite function. 
I have verified all the parameters of fwrite. They are valid pointers.  I’ve also tried putting another fwrite function writing a hard coded value into a file. That function also is crashing.
I've replaced fwrite with fprintf, but still getting the same error.
*/
	if (fwrite (pCurSectData->pucData, sizeof (Uchar_t), pCurSectData->ulDataSize, fpTFFile)
		!= pCurSectData->ulDataSize)
	{
		ERR_MSG1 (LOG_ALERT, "Can't save modified TF data to %s\n", caCurrentFilename);
		return (FAILURE);
	}

             ................................
please find the snapshot of the call stack (while the crash has occurred) in the attachement


Here our function is sSaveTFFile, which calls the fwrite and it is throwing the segmentation fault.

While searching in Google Groups, I found some articles telling the reason to be memory mismanagement. Could any one figure out the exact problem?
Attached Thumbnails
fwrite-throws-segmentation-fault-untitled.jpg  
  #2 (permalink)  
Old 08-25-2005
andryk's Avatar
andryk andryk is offline Forum Advisor  
Registered User
  
 

Join Date: Sep 2003
Posts: 448
Maybe some of those ptrs point to illegal memories ...
try writing with open/write/close only and see if sigsev occur again on writing data, if so, some ptrs may be erronous
  #3 (permalink)  
Old 08-25-2005
blowtorch's Avatar
blowtorch blowtorch is offline Forum Advisor  
Supporter
  
 

Join Date: Dec 2004
Location: Singapore
Posts: 2,350
Are you looping anywhere? Any segmentation faults I have faced were mainly due to accidental infinite loops.
  #4 (permalink)  
Old 08-26-2005
fermisoft's Avatar
fermisoft fermisoft is offline
Registered User
  
 

Join Date: Jun 2005
Location: Mumbai
Posts: 16
Quote:
Originally Posted by andryk
Maybe some of those ptrs point to illegal memories ...
try writing with open/write/close only and see if sigsev occur again on writing data, if so, some ptrs may be erronous
Those pointers are valid. I attempted to call fwrite with a hardcoded string. It too crashed.
If u see the attachemnt (in my prev message), the seg. fault has occured in calloc, which is being called by fwrite. So i suspect memory leak/corruption.

You got any idea abt the impact of memory issues on fwrite function?
  #5 (permalink)  
Old 08-26-2005
fermisoft's Avatar
fermisoft fermisoft is offline
Registered User
  
 

Join Date: Jun 2005
Location: Mumbai
Posts: 16
Quote:
Originally Posted by blowtorch
Are you looping anywhere? Any segmentation faults I have faced were mainly due to accidental infinite loops.

No... there are no infinite loops.
  #6 (permalink)  
Old 08-26-2005
jim mcnamara jim mcnamara is offline Forum Staff  
...@...
  
 

Join Date: Feb 2004
Location: NM
Posts: 5,715
It means memory allocated to the
Code:
pCurSectData->pucData
object is less than
Code:
sizeof (Uchar_t) * pCurSectData->ulDataSize
  #7 (permalink)  
Old 09-13-2005
fermisoft's Avatar
fermisoft fermisoft is offline
Registered User
  
 

Join Date: Jun 2005
Location: Mumbai
Posts: 16
The above problem has been solved. Actually memory overrun was happening in a memcpy operation. This memory overrun caused the fwrite call to crash.

I detected that culprit memcpy code by placing fwrite randomly through the program flow. Then i picked up the first fail of fwrite function. After correcting the memory overrun in the memcpy call, the program proceeds smoothly.

Thanks all.
Sponsored Links
Closed Thread

Bookmarks

Thread Tools Search this Thread
Search this Thread:

Advanced Search
Display Modes Rate This Thread
Rate This Thread:

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 On




All times are GMT -4. The time now is 11:37 PM.


Powered by: vBulletin, Copyright ©2000 - 2006, Jelsoft Enterprises Limited. Language Translations Powered by .
vBCredits v1.4 Copyright ©2007 - 2008, PixelFX Studios
The UNIX and Linux Forums Content Copyright ©1993-2009. All Rights Reserved.Ad Management by RedTyger

Content Relevant URLs by vBSEO 3.2.0