vendredi 20 juillet 2018

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;

In the header file;

ADesign *a_d;

In the .cpp where it's being initialized;

a_d = new ADesign;

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.

Aucun commentaire:

Enregistrer un commentaire