Madz
Madz

Reputation: 1279

Object initialization differences in afxmem.cpp

I have an object in function z which I'm accessing from functions x and y. The problem is that it's initialized through different functions in the afxmem.cpp (VS 11.0 file).

Here's my code;

classA
{
public:
    ADesign *a_d;
};
void classA::functionZ()
{
    a_d = new ADesign;
}

//functionX and functionY both calls same function
void classB::functionX()
{
    ca.functionZ();
}
void classB::functionY()
{
    ca.functionZ();
}

//sample initializer for ADesign
ADesign::ADesign()
:doc_(0)
{
    version_number = 7.82f; 
    d_file = ""; 
    c_file = ""; 
    angle = ID_ANGLE_MER;
    machine_type = MACHINE_COMPRESS;
    //...etc
}

When it's being initialized, it goes through these functions in afxmem.cpp for function x

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

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);
        if (pResult != NULL)
            return pResult;

#ifdef _AFXDLL
        if (pfnNewHandler == _pfnUninitialized)
        {
            AFX_MODULE_THREAD_STATE* pState = AfxGetModuleThreadState();
            pfnNewHandler = pState->m_pfnNewHandler;
        }
        if (pfnNewHandler == NULL || (*pfnNewHandler)(nSize) == 0)
            break;
#else
        if (_afxNewHandler == NULL || (*_afxNewHandler)(nSize) == 0)
            break;
#endif
    }
    return pResult;
#endif
}

And this function in afxmem.cpp for function y;

void* __cdecl operator new(size_t nSize)
{
    void* pResult;
#ifdef _AFXDLL
    _PNH pfnNewHandler = _pfnUninitialized;
#endif
    for (;;)
    {
#if !defined(_AFX_NO_DEBUG_CRT) && defined(_DEBUG)
        pResult = _malloc_dbg(nSize, _NORMAL_BLOCK, NULL, 0);
#else
        pResult = malloc(nSize);
#endif
        if (pResult != NULL)
            return pResult;

#ifdef _AFXDLL
        if (pfnNewHandler == _pfnUninitialized)
        {
            AFX_MODULE_THREAD_STATE* pState = AfxGetModuleThreadState();
            pfnNewHandler = pState->m_pfnNewHandler;
        }
        if (pfnNewHandler == NULL || (*pfnNewHandler)(nSize) == 0)
            break;
#else
        if (_afxNewHandler == NULL || (*_afxNewHandler)(nSize) == 0)
            break;
#endif
    }
    return pResult;
}

When it goes through function y, it doesn't get initialized properly. I'm accessing the program through the command line for function y.

Is there a reason as to why the initialization is happening in 2 different ways for the same object?

Thanks.

Upvotes: 0

Views: 228

Answers (1)

Jabberwocky
Jabberwocky

Reputation: 50882

At the beginning of your .cpp files you may or may not have this line:

#define new DEBUG_NEW

If this line is present, then new will end up invoking this function:

void* __cdecl operator new(size_t nSize, int nType, LPCSTR lpszFileName, int nLine)

if it is not present then new will end up invoking this function:

void* __cdecl operator new(size_t nSize)

That's internal MFC debugging stuff.

But that doesn't explain why something doesn't get initialized properly. You need to elaborate your question, it's not clear enough.

Upvotes: 2

Related Questions