MFC Object allocated with "new", but deallocated with "free"


I am writing an appication in visual studio 6.   I am using MFC (Specifically CRecordset).  I allocate a new instance of CRecordset by using "new".

CModuleRecordSet  * rs = new CModuleRecordSet(&this->m_Database);

Later, when I am done with the object, I call "delete". as in

delete rs;

What I notice is that delete calls into the delete operator defined in afxmem.cpp.

void PASCAL CObject::operator delete(void* p)
      _free_dbg(p, _CLIENT_BLOCK);

The question is, why is it calling free intstead of delete?  And how do I fix this?

Note, I have checked that in later versions of visual studio, the code in afxmem.cpp seems to be the same.  


Who is Participating?

[Product update] Infrastructure Analysis Tool is now available with Business Accounts.Learn More

I wear a lot of hats...

"The solutions and answers provided on Experts Exchange have been extremely helpful to me over the last few years. I wear a lot of hats - Developer, Database Administrator, Help Desk, etc., so I know a lot of things but not a lot about one thing. Experts Exchange gives me answers from people who do know a lot about one thing, in a easy to use platform." -Todd S.

CObject::operator delete(void* p) frees the memory allocated with new.
You haven't to worry about its implementation.
AndyAinscowFreelance programmer / ConsultantCommented:
>>The question is, why is it calling free intstead of  delete?

You call delete, how it is implemented is none of your concern.
ps.  Look at how the memory is allocated with new and you might understand
Because COBject has an overloaded version of delete operator, that gets used.
Conceptually, new is like
- allocate (malloc) + call constructor
And delete is
- Call destructor (p->~T()) + deallocate (free)
hendrivAuthor Commented:

Thanks everyone for your input.  I have checked the allocation call, and it calls into this code in afxmem.cpp.

void* PASCAL
CObject::operator new(size_t nSize, LPCSTR lpszFileName, int nLine)
      return ::operator new(nSize, _CLIENT_BLOCK, lpszFileName, nLine);

Is there any way to step further into new?  To me it looks like "new" is calling into new, and "delete" is using free.  Am I missing something?


AndyAinscowFreelance programmer / ConsultantCommented:
If you step further in you find it is using functionality like malloc to allocate the memory.

void* __cdecl operator new(size_t nSize, int nType, LPCSTR lpszFileName, int nLine)
    return ::operator new(nSize);
    void* pResult;
#ifdef _AFXDLL
    _PNH pfnNewHandler = _pfnUninitialized;
    for (;;)
        pResult = _malloc_dbg(nSize, nType, lpszFileName, nLine);

You match up new with delete, internally those call malloc and free.
Don't mix them yourself.

Experts Exchange Solution brought to you by

Your issues matter to us.

Facing a tech roadblock? Get the help and guidance you need from experienced professionals who care. Ask your question anytime, anywhere, with no hassle.

Start your 7-day free trial
It's more than this solution.Get answers and train to solve all your tech problems - anytime, anywhere.Try it for free Edge Out The Competitionfor your dream job with proven skills and certifications.Get started today Stand Outas the employee with proven skills.Start learning today for free Move Your Career Forwardwith certification training in the latest technologies.Start your trial today
System Programming

From novice to tech pro — start learning today.