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

Hi,

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)
{
#ifdef _AFX_NO_DEBUG_CRT
      free(p);
#else
      _free_dbg(p, _CLIENT_BLOCK);
#endif
}


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.  

Thanks
Hendri


hendrivAsked:
Who is Participating?
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.

 
alb66Commented:
CObject::operator delete(void* p) frees the memory allocated with new.
You haven't to worry about its implementation.
0
 
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
0
 
ambienceCommented:
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)
 
 
0
 
hendrivAuthor Commented:
Hi,

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?

Thanks

0
 
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)
{
#ifdef _AFX_NO_DEBUG_CRT
    UNUSED_ALWAYS(nType);
    UNUSED_ALWAYS(lpszFileName);
    UNUSED_ALWAYS(nLine);
    return ::operator new(nSize);
#else
    void* pResult;
#ifdef _AFXDLL
    _PNH pfnNewHandler = _pfnUninitialized;
#endif
    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.
0

Experts Exchange Solution brought to you by ConnectWise

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
Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.

All Courses

From novice to tech pro — start learning today.