In the WndProc callback of my program I'm doing this to store a mouse click in a vector:
case WM_LBUTTONDOWN:
point = new POINT();
point->x = LOWORD (lParam);
point->y = HIWORD (lParam);
point_vector.push_back(point);
InvalidateRect(hWnd, NULL, TRUE);
break;
It compiles fine but when this runs, I get an access violation on the "push_back". Both "point" and "point_vector" are declared globally and seem valid in the debugger. If I declare them locally, no access violation. Why would this happen?
This is on VS10.
@Martyn Lovell Here is the call stack
msvcr100d.dll!operator delete(void * pUserData=0xfefefefe) Line 52 + 0x3 bytes C++ my_app.exe!std::allocator::deallocate(tagPOINT * * _Ptr=0xfefefefe, unsigned int formal=0) Line 182 + 0x9 bytes C++ my_app.exe!std::vector >::reserve(unsigned int _Count=1) Line 768 C++ my_app.exe!std::vector >::_Reserve(unsigned int _Count=1) Line 1298 C++ my_app.exe!std::vector >::push_back(tagPOINT * const & _Val=0x008e9d58) Line 992 C++ my_app.exe!WndProc(HWND * hWnd=0x000b060a, unsigned int message=513, unsigned int wParam=1, long lParam=19857987) Line 241 C++ user32.dll!774662fa()
[Frames below may be incorrect and/or missing, no symbols loaded for user32.dll]
user32.dll!77466d3a()
user32.dll!77466ce9()
user32.dll!77466e44()
user32.dll!774677c4()
user32.dll!7746788a()
my_app.exe!wWinMain(HINSTANCE__ * hInstance=0x00c80000, HINSTANCE__ * hPrevInstance=0x00000000, wchar_t * lpCmdLine=0x006c35d2, int nCmdShow=1) Line 62 + 0xc bytes C++ my_app.exe!__tmainCRTStartup() Line 547 + 0x2c bytes C my_app.exe!wWinMainCRTStartup() Line 371 C kernel32.dll!764c33ca()
ntdll.dll!77e79ed2()
ntdll.dll!77e79ea5()
And this is the line where it crashes in dbgdel.cpp (not sure if this is helpful) /* verify block type */ _ASSERTE(_BLOCK_TYPE_IS_VALID(pHead->nBlockUse));
@CoreyStup Doesn't seem to make any difference if the vars are local or global
Also this happens if I call other vector functions like resize(), clear() or reserve(), but not size().
POINT
? Copying is probably faster than passing a pointer. – Ben Voigt