In my CShellFolder::CShellFolder() constructor method, I have the following
code:
if(FAILED(SHGetMalloc(&m_pMalloc)))
{
delete this;
return;
}
I also have similar instances of this code in the constructor as follows:
m_pPidlMgr = new CPidlMgr();
if(!m_pPidlMgr)
{
delete this;
return;
}
The goal, of course, is to cause construction of the object to fail if for
some reason the constructor can't allocate memory for data members that are
objects or if it fails to obtain the shell's memory allocator.
I have other code that then looks like this:
CShellFolder *pSF = new CShellFolder(constructor arguments);
if (!pSF)
return E_OUTOFMEMORY;
What I'm observing happen is that my CShellFolder class constructor method
has actually encountered a reason to fail, and it executes "delete this"
followed by "return", but the "new" operator is *NOT* returning a NULL
pointer. Instead, it appears to be returning the value of the "this"
pointer from the instance of the class that failed to be instantiated. I
would have expected it to return NULL.
This is the 1st time I've seen this class instantiation failure occur, and
the method of handling it is something I inherited from other code and it
looked like a reasonble method to use so I implemented it in my own code.
Is this in fact the wrong way to handle the situation?
Why would "new" return a non-NULL value if the object instance it was
creating fails in the constructor and performs "delete this"?
TIA,
Chuck
--
Chuck Chopp
ChuckChopp (at) rtfmcsi (dot) com http://www.rtfmcsi.com
RTFM Consulting Services Inc. 864 801 2795 voice & voicemail
103 Autumn Hill Road 864 801 2774 fax
Greer, SC 29651
Do not send me unsolicited commercial email.