我在想如何处理函数中的返回语句。我有一个方法分配一些内存,但返回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