I was wondering how people handle return statements in a function. I have a method that allocates some memory but has a return false; statement when a function goes wrong. this statement is located about half way through the function so my memory is leaked if that function fails. This isn't the only return ...; statement I have in this function. What would stackoverflow recommend doing to clean up the code in a function with a few return statements in it?
if( true == OpenFtpConnection() )
{
AfxMessageBox( _T( "Connection to internet and ftp server found" ) );
// set the directory to where the xml file lives
if( false == FtpSetCurrentDirectory( m_ftpHandle, _T(_FTP_XML_LOCATION) ) )
return false;
HINTERNET xmlHandle = NULL;
WIN32_FIND_DATA fileData;
SYSTEMTIME fileWriteTime;
xmlHandle = FtpFindFirstFile( m_ftpHandle, _T("TPCFeed.xml"), &fileData, INTERNET_FLAG_RELOAD, 0 );
if( NULL == xmlHandle )
return false;
else
{
// get the write time of the ftp file
FileTimeToSystemTime( &fileData.ftLastWriteTime, &fileWriteTime );
// get the write time of the local file
HANDLE localFileHandle = NULL;
localFileHandle = CreateFile( _T(_XML_FILENAME_PATH), FILE_READ_ATTRIBUTES,
FILE_SHARE_READ, NULL, OPEN_EXISTING,
NULL, NULL );
if( INVALID_HANDLE_VALUE == localFileHandle )
{
AfxMessageBox( _T( "opening file failed, file not found" ) );
return false;
}
else
{
CloseHandle( localFileHandle );
}
// close the FtpFindFirstFile() handle
InternetCloseHandle( xmlHandle );
}
// download xml file to disk
//if( false == FtpGetFile( m_ftpHandle, _T("TPCFeed.xml"), _T(_XML_FILENAME_PATH), FALSE,
// FILE_ATTRIBUTE_NORMAL, FTP_TRANSFER_TYPE_BINARY, 0 ) )
// return false;
}
else
{
AfxMessageBox( _T( "No Connection to internet or ftp server found" ) );
return false;
}
if( true == CloseFtpConnection() )
AfxMessageBox( _T( "Connection to internet closed" ) );
else
AfxMessageBox( _T( "Connection to internet not closed" ) );