_CrtIsValidHeapPointer(pUserData) in dbghelp.c

J

jiing

When I execute my program (it's multithread and has COM and dll),
there is an error message as follows:

Program D:\dongle_1\TestAP\bin\Debug\TestAP.exe
File: dbgheap.c
Line: 1132
Expression: _CrtIsValidHeapPointer(pUserData)
For information on how your program can cause an assertion failure, see
the Visual C++ documentation on asserts.
==================================================
I read the dbgheap.c and find the line 1132, it's as follows:
/*
* If this ASSERT fails, a bad pointer has been passed in. It
may be
* totally bogus, or it may have been allocated from another
heap.
* The pointer MUST come from the 'local' heap.
*/
1132: _ASSERTE(_CrtIsValidHeapPointer(pUserData));


=============================================================I also use
google to find some answers.
They said
(1) If you are developing multithread application, be sure to link it
with
the correct runtime library(/MD, /MT). That what
"The pointer MUST come from the 'local' heap." means.
Check what "ruintime library" you use. It should be "Multi threaded (
debug ) DLL"

Roman

(2)
Not only must they match, they must both be DLL versions. If they're
static versions, each piece (the exe and the dll) will have their own
heap and the OP will still be deleting from mismatched heaps.
I'd also be suspicious of general design issues given the way the OP's
main program is having to clean up for the dll.

You'll get this problem if you use two different run-time libraries,
one to
allocate the memory and one to de-allocate the memory. Open up the
project
settings dialog, switch to C/C++ tab, set Category to Code Generation
and
check the Use run-time library combo box. These must match in any
modules
that share the task of allocating and destroying memory.
--
Randy Charles Morin
Author of Programming Windows Services
http://www.kbcafe.com
=====================================================
If necessary, my dirty code is as follows (Please read from the bottom
up)
http://irc.csie.org:8888/125

Thanks in advance .

-jiing-
 
Joined
Mar 25, 2012
Messages
2
Reaction score
0
Hi,

I am also facing the same problem. What did you do to fix this issue? you said that the point second worked for you. I also did the first point setting up runtime library to /MD or /MDd. not working. second point not able understand clearly. Pls help me to resolve it.
.
Thanks
Rani
 
Last edited:
Joined
Feb 10, 2011
Messages
2
Reaction score
0
Open up the project settings dialog, switch to C/C++ tab, set Category to Code Generation and check the Use run-time library combo box. These must match in any modules.
 
Joined
Mar 25, 2012
Messages
2
Reaction score
0
Hi,

Sorry for late reply. I have checked all modules. it matching.

in my code, i am having below method.

Severity CMDIClass::BuildColumnMap(CMapStringToWord& mIndexMap)
{
int nIndex;
long retcode;
CS_DATAFMT datafmt;
char szColumnName[COLUMN_NAME_LENGTH];

Severity eReturnValue = NO_ERRORS;
TRY
{

retcode = ct_res_info(m_pCommand, CS_NUMDATA, &m_nNumberOfColumns, CS_UNUSED, NULL);


if (retcode != CS_SUCCEED)
{
/* Fix for contract to contract wizard error,
when we have multiple parts and choose part replacement */

//return FATAL_ERROR;
m_nNumberOfColumns = 0;
}

for(nIndex = 1;nIndex <= m_nNumberOfColumns; nIndex++)
{
/*
** Get the column name, then add the entry to the map using the column name
** as the key and storing the nIndex -1 as the value.
*/
retcode = ct_describe(m_pCommand, nIndex, &datafmt);
if (retcode != CS_SUCCEED)
{
return FATAL_ERROR;
}
#ifdef _WIN32
strncpy(szColumnName,datafmt.name,COLUMN_NAME_LENGTH); //for CTLib

#else
_fstrncpy((LPSTR) szColumnName,datafmt.name,COLUMN_NAME_LENGTH); // for CTLib
#endif
szColumnName[COLUMN_NAME_LENGTH] = '\0';
mIndexMap.SetAt(ColumnName, (WORD)(nIndex - 1));
}
}
CATCH(CException, exException)
{
eReturnValue = FATAL_ERROR;
}
END_CATCH
return eReturnValue;
}

it calls
mIndexMap.SetAt(ColumnName, (WORD)(nIndex - 1));

SetAt is inline function of CMapStringToWord class
AFXCOLL_INLINE void CMapStringToWord::SetAt(const char* key, WORD newValue)
{ (*
this)[key] = newValue; }

it calls the below function.

WORD& CMapStringToWord::operator[](const char* key)
{
const CString afxEmptyString;
ASSERT_VALID(
this);
UINT nHash;
CAssoc* pAssoc;
if ((pAssoc = GetAssocAt(key, nHash)) == NULL)
{
if (m_pHashTable == NULL)
InitHashTable(m_nHashTableSize);
// it doesn't exist, add a new Association
pAssoc = NewAssoc();
pAssoc->nHashValue = nHash;
pAssoc->key = key;
// 'pAssoc->value' is a constructed object, nothing more
// put into hash table
pAssoc->pNext = m_pHashTable[nHash];
m_pHashTable[nHash] = pAssoc;
}
return pAssoc->value; // return new reference
}
SetAt is called for 72 items. in that for 31 item SetAt is working fine. after that it goes to dbgheap.c

extern "C" _CRTIMP int __cdecl _CrtIsValidHeapPointer(
const void * pUserData
)
{
if (!pUserData)
return FALSE;
if (!_CrtIsValidPointer(pHdr(pUserData), sizeof(_CrtMemBlockHeader), FALSE))
return FALSE;
return HeapValidate( _crtheap, 0, pHdr(pUserData) );
}

any problem in the code. Pls help me to identify the problem.
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Top