void GoalInterfaceTest::testGoalInterfaceAssign() { // Just create a goal with a SmartPtr and let it get deleted // NOTE: For some reason we can't copy SmartPtr<>s and still // have the benefit of reference counting - it will // throw an exception when it comes to deleting the copied pointer... SmartPtr<GoalInterface> sg(new StoneCountGoal()); // These lines throw an exception at the end of the function // Goal sgGoal(sg); // Goal sgGoal((GoalInterface*)sg); GoalInterface* sgToadPtr = new StoneCountGoal(); Goal* toadPtr = new Goal( sgToadPtr ); // This copies the GoalInterface* sgToadPtr into a 2nd SmartPtr in toadPtr2 // causing two reference counts of sgToadPtr and so a crash! // Goal* badToadPtr2 = new Goal( sgToadPtr ); // This line throws an exception at the end of the function // Goal badToad( toadPtr->getGoals().begin()->goal ); // This line throws an exception at the end of the function // Goal badToad2( sgToadPtr ); // Try copying toad and its SmartPtrs Goal copyToad( *toadPtr ); Goal copyToad2( NULL ); #ifdef _DEBUG _ASSERTE( _CrtCheckMemory() ); _ASSERTE( _CrtIsValidHeapPointer( sgToadPtr ) ); _ASSERTE( _CrtIsValidHeapPointer( toadPtr ) ); #endif // No need to delete sgToadPtr since GoalInterface implements // reference counting and has been copied into a SmartPtr in Goal // SmartPtr will delete it when done // mustDeletePtr on the otherhand is _not_ copied into a SmartPtr // therefore we must delete it explicitly GoalInterface* mustDeletePtr = new StoneCountGoal(); delete mustDeletePtr; delete toadPtr; // delete badToadPtr2; }
LRESULT CRecordSound::OnStopRecording(WPARAM wParam, LPARAM lParam) { MMRESULT mmReturn = 0; if(!m_bRecording) return FALSE; if(m_bRecording) { mmReturn = ::waveInStop(m_hRecord); if(!mmReturn) mmReturn = ::waveInReset(m_hRecord); Sleep(500); if(!mmReturn) mmReturn = ::waveInClose(m_hRecord); if(!mmReturn) m_bRecording = FALSE; if(m_Player) m_Player->PostThreadMessage(WM_PLAYSOUND_STOPPLAYING,0,0); if(m_Writer) m_Writer->PostThreadMessage(WM_WRITESOUNDFILE_CLOSEFILE,0,0); if (_CrtIsValidHeapPointer(pwsf)) { delete pwsf; } pwsf = NULL; return mmReturn; } return TRUE; }
static int CrtAllocHook(int nAllocType, void * pvData, size_t nSize, int nBlockUse, long lRequest, const unsigned char * szFileName, int nLine) { if (nBlockUse == _CRT_BLOCK) { return TRUE; } if (!g_inAllocHook) { g_inAllocHook = true; if (nAllocType == _HOOK_ALLOC) { StoreAlloc(pvData, nSize, lRequest, (g_newFile[0]) ? g_newFile : (const char*)szFileName, "", (g_newFile[0]) ? g_newLine : nLine); } else if (nAllocType == _HOOK_FREE) { if (_CrtIsValidHeapPointer(pvData)) { _CrtMemBlockHeader* pHead = pHdr(pvData); lRequest = pHead->lRequest; } RemoveAlloc(pvData, nSize, lRequest); } // clear the new caller g_newFile = ""; g_inAllocHook = false; } return TRUE; }
int CDbgMemAlloc::CrtIsValidHeapPointer( const void *pMem ) { return _CrtIsValidHeapPointer( pMem ); }
int catchMemoryAllocHook(int allocType, void *userData, size_t size, int blockType, long requestNumber, const unsigned char *filename, // Can't be UNICODE int lineNumber) { _CrtMemBlockHeader *pCrtHead; long prevRequestNumber; #ifdef UNICODE wchar_t Wname[1024] ; Wname[0] = '\0' ; #endif // internal C library internal allocations if ( blockType == _CRT_BLOCK ) { return( TRUE ); } // check if someone has turned off mem tracing // CRT memory tracking can be turned off by the app. MFC sometimes does this temporarily. if ((( _CRTDBG_ALLOC_MEM_DF & _crtDbgFlag) == 0) && ( ( allocType == _HOOK_ALLOC) || ( allocType == _HOOK_REALLOC))) { if (pfnOldCrtAllocHook) { pfnOldCrtAllocHook(allocType, userData, size, blockType, requestNumber, filename, lineNumber); } return TRUE; } // protect if mem trace is not initialized if (g_pMemTrace == NULL) { if (pfnOldCrtAllocHook) { pfnOldCrtAllocHook(allocType, userData, size, blockType, requestNumber, filename, lineNumber); } return TRUE; } // protect internal mem trace allocs if (g_pMemTrace->isLocked) { if (pfnOldCrtAllocHook) { pfnOldCrtAllocHook(allocType, userData, size, blockType, requestNumber, filename, lineNumber); } return( TRUE); } // lock the function g_pMemTrace->isLocked = true; // #ifdef UNICODE int len ; if (NULL != filename) { len = strlen((char *)filename) + 1 ; MultiByteToWideChar(CP_ACP, 0, (char *)filename, len, Wname, len) ; } else len = 0 ; #else #define Wname (char*)filename #endif if (allocType == _HOOK_ALLOC) { g_pMemTrace->addMemoryTrace((void *) requestNumber, size, Wname, lineNumber); } else if (allocType == _HOOK_REALLOC) { if (_CrtIsValidHeapPointer(userData)) { pCrtHead = pHdr(userData); prevRequestNumber = pCrtHead->lRequest; // //if (pCrtHead->nBlockUse == _IGNORE_BLOCK) //{ // if (pfnOldCrtAllocHook) // { // pfnOldCrtAllocHook(allocType, userData, size, blockType, requestNumber, filename, lineNumber); // } // goto END; //} g_pMemTrace->redoMemoryTrace((void *) requestNumber, (void *) prevRequestNumber, size, Wname, lineNumber); } } else if (allocType == _HOOK_FREE) { if (_CrtIsValidHeapPointer(userData)) { pCrtHead = pHdr(userData); requestNumber = pCrtHead->lRequest; // //if (pCrtHead->nBlockUse == _IGNORE_BLOCK) //{ // if (pfnOldCrtAllocHook) // { // pfnOldCrtAllocHook(allocType, userData, size, blockType, requestNumber, filename, lineNumber); // } // goto END; //} g_pMemTrace->removeMemoryTrace((void *) requestNumber, userData); } } //END: // unlock the function g_pMemTrace->isLocked = false; return TRUE; }
bool is_valid (void* ptr) { return !!_CrtIsValidHeapPointer(ptr); }