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)
{
	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();
	}
}



Comments

  • CArchive and CsocketFile

    Posted by Legacy on 02/17/2004 12:00am

    Originally posted by: guzhenghong

    i an using CArchive with CSockFile.
    
    At client:
    Send 100 CObject 's to server
    At Server:
    Recieve the first CObject is TRUE;
    But there is error result next.
    vc++6.0 windows 2000 pro

    Reply
  • CArchive

    Posted by Legacy on 09/04/2002 12:00am

    Originally posted by: Sarah

    Do you know if there is an alternative to a CArchive in C#??

    Reply
  • CSocket in Debug vs. Release

    Posted by Legacy on 01/08/2002 12:00am

    Originally posted by: Kevin Goodman

    I've got a different problem. Debug ver. works fine. Release version accepts in WINOCC.CPP COleControlContainer::AttachControlSite call to pSite = (COleControlSite*)m_siteMap.GetValueAt(pWnd->m_hWnd);

    The odd thing is I place a break point here in the Debug ver. and it nevers gets hit. The other really strange thing is I don't have any AFX or OLE controls in the app.

    Any thoughts?

    Reply
  • Use correct techniques !

    Posted by Legacy on 06/14/2001 12:00am

    Originally posted by: Maxo

    Why did you catch exceptions from archive sooner than you've created CArchive object ?

    Reply
  • avoiding the problem by using abort ??

    Posted by Legacy on 01/08/2001 12:00am

    Originally posted by: matthias metzner

    the command abort doesn't throw an exception at closing the archive (but also doesn't flush the archive) - perhaps this is the MS response to the error?

    have nice days ...

    Reply
  • Thanks for the clue...

    Posted by Legacy on 02/15/1999 12:00am

    Originally posted by: Mike Smith

    I've been having this somewhat intermittent problem with MFC sockets, got a feeling that what you're saying could be the cause.

    Reply
  • If you are sure it's a bug

    Posted by Legacy on 02/13/1999 12:00am

    Originally posted by: Hans Wedemeyer

    If you are sure it's a bug why not submit it to MS, they used to have a
    reward program for the first person to submit a true bug. The reward used to be
    a free compiler upgrade at next release..

    regards
    Hans Wedemeyer

    Reply
Leave a Comment
  • Your email address will not be published. All fields are required.

Top White Papers and Webcasts

  • Live Event Date: August 20, 2014 @ 1:00 p.m. ET / 10:00 a.m. PT When you look at natural user interfaces as a developer, it isn't just fun and games. There are some very serious, real-world usage models of how things can help make the world a better place – things like Intel® RealSense™ technology. Check out this upcoming eSeminar and join the panel of experts, both from inside and outside of Intel, as they discuss how natural user interfaces will likely be getting adopted in a wide variety …

  • Savvy enterprises are discovering that the cloud holds the power to transform IT processes and support business objectives. IT departments can use the cloud to redefine the continuum of development and operations—a process that is becoming known as DevOps. Download the Executive Brief DevOps: Why IT Operations Managers Should Care About the Cloud—prepared by Frost & Sullivan and sponsored by IBM—to learn how IBM SmartCloud Application services provide a robust platform that streamlines …

Most Popular Programming Stories

More for Developers

Latest Developer Headlines

RSS Feeds