Click here to Skip to main content
15,925,255 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
My project use two different function for release and debug. My code is crashing in release version, while allocation of memory.

Release:
WINBASEAPI
_Ret_maybenull_
_Post_writable_byte_size_(dwBytes)
LPVOID
WINAPI
HeapAlloc(_In_ HANDLE hHeap,_In_ DWORD dwFlags,_In_ SIZE_T dwBytes);


Debug:
#ifdef _DEBUG
#define HeapAlloc( a, b, c ) _calloc_dbg( (c), 1, _NORMAL_BLOCK, __FILE__, __LINE__ )
#endif




Code is as per below, it's used for READ xml file in C++\VC++.
Here, I wrote only sample part, which will be read line by line.

HANDLE hHeap = NULL;
SYSTEM_INFO si;
GetSystemInfo( &si );
hHeap = HeapCreate( HGE, 16 * si.dwPageSize, 0L );

int HGE = HEAP_GENERATE_EXCEPTIONS;
TCHAR *xmlTag = _T("nXMLPages");
size_t lenTag = _tcslen( xmlTag );
TCHAR *StartXMLTag = NULL, *EndXMLTag = NULL;
StartXMLTag = (TCHAR *) HeapAlloc( hHeap, HGE, ( lenTag + 2 ) * sizeof( TCHAR ) );
EndXMLTag = (TCHAR *) HeapAlloc( hHeap, HGE, ( lenTag + 4 ) * sizeof( TCHAR ) );	

//Crashing place while call HeapAlloc() for EndXMLTag

What I have tried:

Input file data is like; <nxmlpages>4
Posted
Updated 14-Nov-18 5:25am
v2
Comments
Richard MacCutchan 14-Nov-18 3:34am    
You need to use your debugger to collect more information. Check the values of all variables at the point of failure.

Typical failures in release version happen due to uninitialised variables. Anyway, in the sample code you provided, variables are initialised (with the exception of HGE which is used even before its declaration, a believe the compiler would complain about). I see you didn't check the return value of HeapCreate.
 
Share this answer
 
Comments
Richard MacCutchan 14-Nov-18 11:36am    
I just upvoted this, and a little link popped up next to the stars which said "Undo". But I misread it as "Uno", and thought "how does it know Carlo is Italian? :)
CPallini 14-Nov-18 12:28pm    
Machine learning, Sir. The AI infers that from the bad English used in my sentences.
By the way, thank you very much.
The difference is in debug mode it calls the memory tracking version of calloc and in release it calls HeapAlloc. These two are vastly different and require different, corresponding release functions. Also, using the malloc/calloc family does not require you to create a heap first as HeapAlloc does. Here's what microsoft has to say about them : Comparing Memory Allocation Methods | Microsoft Docs[^]

Personally, unless I had to I would stay with the malloc family of calls (new/delete for c++) so I could take advantage of the memory tracking functionality that is available with them.

If you MUST use HeapAlloc here is a handy little template class that can help you with it. The nice thing is it automatically releases the memory when the object is deleted and provides operators that remove the need for casting of the pointer. It uses the global heap so you don't have to create one. Anyway, here it is:
C++
template <typename T> class CHeapAlloc
{
public:
    CHeapAlloc( size_t count = 1 )
    {
        size_t size = count * sizeof( T );
        m_ptr = (T *)::HeapAlloc( ::GetProcessHeap(), HEAP_ZERO_MEMORY, size );
    }

    virtual ~CHeapAlloc()
    {
        if( m_ptr )
        {
            ::HeapFree( ::GetProcessHeap(), 0, m_ptr );
            m_ptr = nullptr;
        }
    }

    operator T*()         { return m_ptr; }

    T * operator->()      { return m_ptr; }

public:
    T * m_ptr;
};
I can't remember where I found this but I think it came from here.
 
Share this answer
 
v2

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900