我在想如何處理函數中的返回語句。我有一個方法分配一些內存,但返回false;當函數出錯時聲明。該語句位於函數的大約一半處,所以如果該函數失敗,我的內存就會泄漏。這不是唯一的回報......;我在這個函數中有聲明。那麼stackoverflow會推薦做什麼來清理函數中的代碼,並在其中包含一些return語句?函數內的返回語句
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"));
對於「發佈代碼」+1。 – bcat 2010-08-31 19:20:23
true ==「發佈代碼」 – TheFuzz 2010-08-31 19:43:44