[질문]close()에서 세그멘테이션 오류가 나는 이유는 무엇인가요

dufmadlek의 이미지

void CMainWindowImpl::GenerateMD5()
{
	// Create a file to save md5
	m_pMD5File 			= new CFileBase;
	m_pMD5File->Open( m_pleGenerateFileName->text(), "w+" );
	
	for( uint i = 0; i < m_plbTargetDir->count(); i ++ )
	{
		GetFileName( m_plbTargetDir->text(i) );
	}
	
	delete m_pMD5File;
	m_pMD5File = NULL;
}
QString CMainWindowImpl::GetFileName( QString strPath )
{
	QDir Dir( strPath );
	
	Dir.setSorting( QDir::DirsFirst );
	QString strFileName = "";
	const QFileInfoList* files = Dir.entryInfoList();
	if( files )
	{
		QFileInfoListIterator it( *files );
		QFileInfo* fi;
		while( (fi=it.current() ) != 0 )
		{
			++it;
			if( fi->fileName() == "." || fi->fileName() == ".." )
				; // nothing
			else if( fi->isDir() )
			{
				strFileName = GetFileName( Dir.path() + "/" + fi->fileName() );
			}
			else
			{
				char md5str[128] = {0x00,};
				
				strFileName = Dir.path() + "/" + fi->fileName();
				
				if( _GetMD5( strFileName, md5str ) > 0 )
				{
					strFileName += "=";
					strFileName += md5str;
					strFileName += "\n";
					
					m_pMD5File->Write( strFileName,  strFileName.length() );
				}
				
			} // if-else
		} // while
	} // if
	
	return strFileName;
}

int	 _GetMD5( const char* filename, char* md5str )
{
	MD5_CTX lctx;
	int fd = 0;
	char* data = NULL;
	struct stat status;
	unsigned char digest[16]={0,};
	
	
	fd = open( filename, O_RDONLY );
	if( fd < 0 )
	{
		return fd;
	}
	
	fstat( fd, &status );
	data = new char[ status.st_size];
	read( fd, data, status.st_size );
	
	
	MD5_Init( &lctx );
	MD5_Update( &lctx, data, status.st_size );
	MD5_Final( digest, &lctx );
	
	for( uint i = 0; i < sizeof(digest); ++i )
	{
		sprintf(md5str+(i*2), "%02x", digest[i] );
	}
	
	delete data;
	data = NULL;	
	
	close( fd ); //<---세그멘테이션 오류(_GetMD5함수를 CMainWindowImpl의 멤버함수로 했을경우)
				
	return fd;
}

_GetMD5라는 함수를 CMainWindowImpl 멤버함수로 했더니
_GetMD5() 마지막 부분 close함수에서 세그멘테이션 오류가 납니다.
그래서 전역함수로 만들었더니 오류가 나지 않고 잘 동작은 하는데 이유가 무엇인지 궁금합니다. ㅠ.ㅠ

happyjun의 이미지

우선 delete data; 는 delete[] data; 로 변경하셔야 합니다.

아시겠지만 close()에서 segfault가 나온다고 그곳에 잘못이 있는 것이 아닐 수도 있습니다.

----------------------------------------
http://moim.at
http://mkhq.co.kr

댓글 달기

Filtered HTML

  • 텍스트에 BBCode 태그를 사용할 수 있습니다. URL은 자동으로 링크 됩니다.
  • 사용할 수 있는 HTML 태그: <p><div><span><br><a><em><strong><del><ins><b><i><u><s><pre><code><cite><blockquote><ul><ol><li><dl><dt><dd><table><tr><td><th><thead><tbody><h1><h2><h3><h4><h5><h6><img><embed><object><param><hr>
  • 다음 태그를 이용하여 소스 코드 구문 강조를 할 수 있습니다: <code>, <blockcode>, <apache>, <applescript>, <autoconf>, <awk>, <bash>, <c>, <cpp>, <css>, <diff>, <drupal5>, <drupal6>, <gdb>, <html>, <html5>, <java>, <javascript>, <ldif>, <lua>, <make>, <mysql>, <perl>, <perl6>, <php>, <pgsql>, <proftpd>, <python>, <reg>, <spec>, <ruby>. 지원하는 태그 형식: <foo>, [foo].
  • web 주소와/이메일 주소를 클릭할 수 있는 링크로 자동으로 바꿉니다.

BBCode

  • 텍스트에 BBCode 태그를 사용할 수 있습니다. URL은 자동으로 링크 됩니다.
  • 다음 태그를 이용하여 소스 코드 구문 강조를 할 수 있습니다: <code>, <blockcode>, <apache>, <applescript>, <autoconf>, <awk>, <bash>, <c>, <cpp>, <css>, <diff>, <drupal5>, <drupal6>, <gdb>, <html>, <html5>, <java>, <javascript>, <ldif>, <lua>, <make>, <mysql>, <perl>, <perl6>, <php>, <pgsql>, <proftpd>, <python>, <reg>, <spec>, <ruby>. 지원하는 태그 형식: <foo>, [foo].
  • 사용할 수 있는 HTML 태그: <p><div><span><br><a><em><strong><del><ins><b><i><u><s><pre><code><cite><blockquote><ul><ol><li><dl><dt><dd><table><tr><td><th><thead><tbody><h1><h2><h3><h4><h5><h6><img><embed><object><param>
  • web 주소와/이메일 주소를 클릭할 수 있는 링크로 자동으로 바꿉니다.

Textile

  • 다음 태그를 이용하여 소스 코드 구문 강조를 할 수 있습니다: <code>, <blockcode>, <apache>, <applescript>, <autoconf>, <awk>, <bash>, <c>, <cpp>, <css>, <diff>, <drupal5>, <drupal6>, <gdb>, <html>, <html5>, <java>, <javascript>, <ldif>, <lua>, <make>, <mysql>, <perl>, <perl6>, <php>, <pgsql>, <proftpd>, <python>, <reg>, <spec>, <ruby>. 지원하는 태그 형식: <foo>, [foo].
  • You can use Textile markup to format text.
  • 사용할 수 있는 HTML 태그: <p><div><span><br><a><em><strong><del><ins><b><i><u><s><pre><code><cite><blockquote><ul><ol><li><dl><dt><dd><table><tr><td><th><thead><tbody><h1><h2><h3><h4><h5><h6><img><embed><object><param><hr>

Markdown

  • 다음 태그를 이용하여 소스 코드 구문 강조를 할 수 있습니다: <code>, <blockcode>, <apache>, <applescript>, <autoconf>, <awk>, <bash>, <c>, <cpp>, <css>, <diff>, <drupal5>, <drupal6>, <gdb>, <html>, <html5>, <java>, <javascript>, <ldif>, <lua>, <make>, <mysql>, <perl>, <perl6>, <php>, <pgsql>, <proftpd>, <python>, <reg>, <spec>, <ruby>. 지원하는 태그 형식: <foo>, [foo].
  • Quick Tips:
    • Two or more spaces at a line's end = Line break
    • Double returns = Paragraph
    • *Single asterisks* or _single underscores_ = Emphasis
    • **Double** or __double__ = Strong
    • This is [a link](http://the.link.example.com "The optional title text")
    For complete details on the Markdown syntax, see the Markdown documentation and Markdown Extra documentation for tables, footnotes, and more.
  • web 주소와/이메일 주소를 클릭할 수 있는 링크로 자동으로 바꿉니다.
  • 사용할 수 있는 HTML 태그: <p><div><span><br><a><em><strong><del><ins><b><i><u><s><pre><code><cite><blockquote><ul><ol><li><dl><dt><dd><table><tr><td><th><thead><tbody><h1><h2><h3><h4><h5><h6><img><embed><object><param><hr>

Plain text

  • HTML 태그를 사용할 수 없습니다.
  • web 주소와/이메일 주소를 클릭할 수 있는 링크로 자동으로 바꿉니다.
  • 줄과 단락은 자동으로 분리됩니다.
댓글 첨부 파일
이 댓글에 이미지나 파일을 업로드 합니다.
파일 크기는 8 MB보다 작아야 합니다.
허용할 파일 형식: txt pdf doc xls gif jpg jpeg mp3 png rar zip.
CAPTCHA
이것은 자동으로 스팸을 올리는 것을 막기 위해서 제공됩니다.