CArchive BUG | CodeGuru

CArchive BUG

Take a look at the following code. Can you see the problem? The code crashes and burns if the socket ‘socketFile’ is attached to has its terminating socket closed. If the MFC developers would have followed standard guidelines for exception handling, everything would be fine. RULE: Never throw exceptions from destructors. bool sendObject(CObject* pObj) { […]

Written By
CodeGuru Staff
CodeGuru Staff
Feb 13, 1999
1 minute read
CodeGuru content and product recommendations are editorially independent. We may make money when you click on links to our partners. Learn More

Take a look at the following code. Can you see the problem? The code crashes and burns if the
socket ‘socketFile’ is attached to has its terminating socket closed. If the MFC developers
would have followed standard guidelines for exception handling, everything would be fine.

RULE: Never throw exceptions from destructors.

bool sendObject(CObject* pObj)
{
	other code …
	CArchive ar(&socketFile, CArchive::store);
	ar << pObj;
	ar.Flush();
	ar.Close();

	... other code
}

void someOtherFunction()
{
	MyObject obj;

	bool bSuccess = false;
	try
	{
		bSuccess = sendObject(&obj);
	}
	catch (CException* pEx)
	{
		pEx->Delete();
	}
}

The Scenario:

Archiving an object to a CSocketFile that may have had the terminating socket closed.

The Result:

If the terminating socket has been closed, calling Flush on the CArchive will raise
an exception (so will calling Close). In the previous case, before the exception is
handled, CArchive goes out of scope and its destructor calls Close. Close throws
another exception and all hell breaks loose. Never throw exceptions from destructors.

You will need nested try / catch blocks …

bool sendObject(CObject* pObj)
{
	other code …
	CArchive ar(&socketFile, CArchive::store);
	try
	{
		ar << pObj;
		ar.Flush();
		ar.Close();
	}
	catch (CException* pEx)
	{
		pEx->Delete();
		return false;
	}
	… other code
}
someOtherFunction()
{
	MyObject obj;
	bool bSuccess = false;
	try
	{
		bSuccess = sendObject(&obj);
	}
	catch (CException* pEx)
	{
		pEx->Delete();
	}
}

CodeGuru Logo

CodeGuru covers topics related to Microsoft-related software development, mobile development, database management, and web application programming. In addition to tutorials and how-tos that teach programmers how to code in Microsoft-related languages and frameworks like C# and .Net, we also publish articles on software development tools, the latest in developer news, and advice for project managers. Cloud services such as Microsoft Azure and database options including SQL Server and MSSQL are also frequently covered.

Property of TechnologyAdvice. © 2026 TechnologyAdvice. All Rights Reserved

Advertiser Disclosure: Some of the products that appear on this site are from companies from which TechnologyAdvice receives compensation. This compensation may impact how and where products appear on this site including, for example, the order in which they appear. TechnologyAdvice does not include all companies or all types of products available in the marketplace.