Checking a valid pointer
-
Hello, I have a vector(CMyData) of an MFC class CMyData in CMyDocumentClass. Because program have to display different sets of Data I usually pass a pointer from a member of this data vector that should be displayed to different view classes. In CMyDataListView, CMyDataPlotView classes before doing anything I first check the validity of pointer with
if(!AfxIsValidAddress(m_pData, sizeof(CMyData), TRUE)) return;
this line usually is passed without problem indication I can read and write to memory location pointed by m_pData. but Next line fails!if(!m_pData->IsKindOf(RUNTIME_CLASS(CMyData))) return;
It says Access violation writing location 0x000000000 checking Call Stack shows that AfxIsValidAddress(...) has been actually executed without problem. Can someone explain what is going on please? How should I check that a memory access failure won't happen. By the way I have to add that in CMyData I have multiple vectors but please note that all this problems occure before I try to access such variables. Actually in the process of checking validity of pointer. Thanks:) -
Hello, I have a vector(CMyData) of an MFC class CMyData in CMyDocumentClass. Because program have to display different sets of Data I usually pass a pointer from a member of this data vector that should be displayed to different view classes. In CMyDataListView, CMyDataPlotView classes before doing anything I first check the validity of pointer with
if(!AfxIsValidAddress(m_pData, sizeof(CMyData), TRUE)) return;
this line usually is passed without problem indication I can read and write to memory location pointed by m_pData. but Next line fails!if(!m_pData->IsKindOf(RUNTIME_CLASS(CMyData))) return;
It says Access violation writing location 0x000000000 checking Call Stack shows that AfxIsValidAddress(...) has been actually executed without problem. Can someone explain what is going on please? How should I check that a memory access failure won't happen. By the way I have to add that in CMyData I have multiple vectors but please note that all this problems occure before I try to access such variables. Actually in the process of checking validity of pointer. Thanks:)If the
CMyData
data object contains pointers itself, it is perfectally reasonable forAfxIsValidAddress(...)
to say that a pointer to the object itself is valid, but it cannot evaluate the object's internals to see if its contained pointers are valid. For example, the object itself may have stepped on its runtime-type information. Peace!-=- James
Please rate this message - let me know if I helped or not! * * *
If you think it costs a lot to do it right, just wait until you find out how much it costs to do it wrong!
Avoid driving a vehicle taller than you and remember that Professional Driver on Closed Course does not mean your Dumb Ass on a Public Road!
See DeleteFXPFiles -
Hello, I have a vector(CMyData) of an MFC class CMyData in CMyDocumentClass. Because program have to display different sets of Data I usually pass a pointer from a member of this data vector that should be displayed to different view classes. In CMyDataListView, CMyDataPlotView classes before doing anything I first check the validity of pointer with
if(!AfxIsValidAddress(m_pData, sizeof(CMyData), TRUE)) return;
this line usually is passed without problem indication I can read and write to memory location pointed by m_pData. but Next line fails!if(!m_pData->IsKindOf(RUNTIME_CLASS(CMyData))) return;
It says Access violation writing location 0x000000000 checking Call Stack shows that AfxIsValidAddress(...) has been actually executed without problem. Can someone explain what is going on please? How should I check that a memory access failure won't happen. By the way I have to add that in CMyData I have multiple vectors but please note that all this problems occure before I try to access such variables. Actually in the process of checking validity of pointer. Thanks:)Electronic75 wrote:
if(!m_pData->IsKindOf(RUNTIME_CLASS(CMyData)))
Set a breakpoint on this statement. Is
m_pData
aCMyData
object? Does it point to the same address as it did when first initialized?
"A good athlete is the result of a good and worthy opponent." - David Crow
"To have a respect for ourselves guides our morals; to have deference for others governs our manners." - Laurence Sterne
-
Hello, I have a vector(CMyData) of an MFC class CMyData in CMyDocumentClass. Because program have to display different sets of Data I usually pass a pointer from a member of this data vector that should be displayed to different view classes. In CMyDataListView, CMyDataPlotView classes before doing anything I first check the validity of pointer with
if(!AfxIsValidAddress(m_pData, sizeof(CMyData), TRUE)) return;
this line usually is passed without problem indication I can read and write to memory location pointed by m_pData. but Next line fails!if(!m_pData->IsKindOf(RUNTIME_CLASS(CMyData))) return;
It says Access violation writing location 0x000000000 checking Call Stack shows that AfxIsValidAddress(...) has been actually executed without problem. Can someone explain what is going on please? How should I check that a memory access failure won't happen. By the way I have to add that in CMyData I have multiple vectors but please note that all this problems occure before I try to access such variables. Actually in the process of checking validity of pointer. Thanks:)AfxIsValidAddress
in VS6 callsIsBadReadPtr
orIsBadWritePtr
depending on whetherbReadWrite
isFALSE
orTRUE
respectively. However, it's been discovered in the last few years that these APIs are seriously flawed. See IsBadXxxPtr should really be called CrashProgramRandomly[^] for details. Visual Studio .NET 2003 still calls IsBadXxxPtr in Debug builds, but in Release builds only checks for NULL. In Visual Studio 2005, the IsBadXxxPtr checks are completely removed, the function only checks for NULL. It does look like you've called through a bad pointer - this could well mean that the virtual function pointer inside CMyData is NULL, which it will be if you usedmemset
, for example.Stability. What an interesting concept. -- Chris Maunder
-
Hello, I have a vector(CMyData) of an MFC class CMyData in CMyDocumentClass. Because program have to display different sets of Data I usually pass a pointer from a member of this data vector that should be displayed to different view classes. In CMyDataListView, CMyDataPlotView classes before doing anything I first check the validity of pointer with
if(!AfxIsValidAddress(m_pData, sizeof(CMyData), TRUE)) return;
this line usually is passed without problem indication I can read and write to memory location pointed by m_pData. but Next line fails!if(!m_pData->IsKindOf(RUNTIME_CLASS(CMyData))) return;
It says Access violation writing location 0x000000000 checking Call Stack shows that AfxIsValidAddress(...) has been actually executed without problem. Can someone explain what is going on please? How should I check that a memory access failure won't happen. By the way I have to add that in CMyData I have multiple vectors but please note that all this problems occure before I try to access such variables. Actually in the process of checking validity of pointer. Thanks:)There is no way to check if an actual non-null pointer is valid! You might want to look at MSDN and read the remake section or learn the limitation of AfxIsValidAddress
Yours Truly, The One and Only!