Article ID: 132893
Article Last Modified on 7/5/2005
/* Compile options needed: /MT /GX
*/
// Change the following line into a comment to show the problem:
#define TWO_PHASED_CONSTRUCTION
// Include the MFC debug memory allocation functions. You will see the
// memory leaks reported by MFC when the application terminates when you
// run the program in the Visual C++ debugger.
#include "afx.h"
#define new DEBUG_NEW
class A
{
char *x;
public:
#ifndef TWO_PHASED_CONSTRUCTION
A() // This code doesn[ASCII 146]t clean up.
{
x = new char[10]; // x will be orphaned.
throw int(1);
}
#else
A() // This code cleans up fine.
{
// Initialize automatic variables here.
}
void Create()
{
// Initialize dynamic members here.
x = new char[10];
throw int(1);
}
#endif
~A()
{
delete [] x;
}
};
void main()
{
A *a;
try
{
a=0;
a=new A;
// Do memory allocation in Create() when doing two-phased construction.
#ifdef TWO_PHASED_CONSTRUCTION
a-<Create();
#endif
}
catch(int)
{
delete a;
}
}
Additional query words: 9.0 9.1 9.00 9.10
Keywords: kbprb kbcodegen kbcpponly kbcode kbcompiler KB132893