如何判断指针是否有效的问题。

王楚铁 2017-04-18 10:02:55
我有一个工作线程,需要接受外部消息,消息分为两种,一种是带结构体参数的,第二种带的参数是常数,
我每次收到消息后会解析结构体,如果是第二种没有带结构体参数的消息,会崩溃。


UINT Class::Thread(LPVOID lpParam)
{
CMsgStruct ReceiveMsg; //接受到的信息 暂存,这个CMsgStruct 就是结构体
PeekMessage(&msg, NULL, WM_USER, WM_USER, PM_NOREMOVE);
while(GetMessage(&msg, 0, MIN_CMD, MAX_CMD))
{
//强行转换
pReceiveMsgCache = (CleanerMsgStruct * )msg.lParam;
//这里,如果发来的消息带参数是结构体,则能解析,指针有效。否则,pReceiveMsgCache 里虽然有值,但是后续pReceiveMsgCache->iCasID 这样使用时,会崩溃。因为指针无效。
//现在的问题是,如何在这里判断一下pReceiveMsgCache或者是msg.lParam,如果有效再解析,无效的话就不解析。

//以下代码解析参数
int a = pReceiveMsgCache->iMyID ;
int b = pReceiveMsgCache->iMyNumber ;
int c = pReceiveMsgCache->iMyPos ;
}
}


问题是,在解析之前如何判断一下pReceiveMsgCache或者是msg.lParam,如果指针有效我就解析里面的值,无效的话就不解析了。

我在一开始收消息时有对消息类型进行判断,但是保险起见还是想在解析之前再判断一下,以免第一道防线做的不到位。
...全文
1170 9 打赏 收藏 转发到动态 举报
写回复
用AI写文章
9 条回复
切换为时间正序
请发表友善的回复…
发表回复
worldy 2017-04-21
  • 打赏
  • 举报
回复
你必须在发送端保证,使得msg.lParam要么是一个有效的指针,要么是NULL,然后你在线程解释的地方,判断msg.lParam是否为NULL
用户 昵称 2017-04-21
  • 打赏
  • 举报
回复
assert不好使,出错直接就弹对话框
kakabulusi 2017-04-21
  • 打赏
  • 举报
回复
必须有个传入位置作合法性判断。再进行消息传递,防止出现你所描述的情况。或者对传入的内容进行合法性验证在做数据处理。
dong364 2017-04-18
  • 打赏
  • 举报
回复
发送消息时,可让wParam参数携带消息类型,根据类型进行不同的解析
赵4老师 2017-04-18
  • 打赏
  • 举报
回复
崩溃的时候在弹出的对话框按相应按钮进入调试,按Alt+7键查看Call Stack即“调用堆栈”里面从上到下列出的对应从里层到外层的函数调用历史。双击某一行可将光标定位到此次调用的源代码或汇编指令处,看不懂时双击下一行,直到能看懂为止。 Access Validation Functions The Win32 API provides a set of functions that a process can use to verify whether it has a specified type of access to a given memory address or range of addresses. The following access validation functions are available. Function Description IsBadCodePtr Determines whether the calling process has read access to the memory at the specified address. IsBadReadPtr Determines whether the calling process has read access to the memory at a specified range of addresses. IsBadStringPtr Determines whether the calling process has read access to the memory pointed to by a null-terminated string pointer. The function validates access for a specified number of characters or until it encounters the string's terminating null character. IsBadWritePtr Determines whether the calling process has write access to the memory at a specified range of addresses. The IsBadHugeReadPtr and IsBadHugeWritePtr functions are also available for compatibility with 16-bit versions of Windows that distinguished between normal memory allocations and huge allocations occupying multiple segments. In 32-bit versions of Windows, these functions are equivalent to IsBadReadPtr and IsBadWritePtr. In a preemptive multitasking environment, it is possible for some other thread to change the process's access to the memory being tested. Even when an access validation function indicates that the process has the desired access to the specified memory, you should use structured exception handling when attempting to access the memory. Use of structured exception handling enables the system to notify the process if an access violation exception occurs, giving the process an opportunity to handle the exception. For more information, see Structured Exception Handling.
三岁、就很帅 2017-04-18
  • 打赏
  • 举报
回复
ASSERT断言 结构体信息的话你看下传入的结构体的大小不为0就可以了吧
rered2010 2017-04-18
  • 打赏
  • 举报
回复
4楼正解. 只能在一开始的时候把类型定义严格一些.
zgl7903 2017-04-18
  • 打赏
  • 举报
回复
AfxIsValidAddress IsBadReadPtr IsBadWritePtr
向立天 2017-04-18
  • 打赏
  • 举报
回复
无法直接判断一个指针是否有效 因为指针就是一个地址变量 只要那个地址存在指针就是对的 至于地址里的数据是不是有效数据其实和地址也就是指针本身无关 你这个需求只能做约定 比如判断lParam是不是NULL 如果不是结构体传这个参数就用NULL来传递

16,551

社区成员

发帖
与我相关
我的任务
社区描述
VC/MFC相关问题讨论
社区管理员
  • 基础类社区
  • Creator Browser
  • encoderlee
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告

        VC/MFC社区版块或许是CSDN最“古老”的版块了,记忆之中,与CSDN的年龄几乎差不多。随着时间的推移,MFC技术渐渐的偏离了开发主流,若干年之后的今天,当我们面对着微软的这个经典之笔,内心充满着敬意,那些曾经的记忆,可以说代表着二十年前曾经的辉煌……
        向经典致敬,或许是老一代程序员内心里面难以释怀的感受。互联网大行其道的今天,我们期待着MFC技术能够恢复其曾经的辉煌,或许这个期待会永远成为一种“梦想”,或许一切皆有可能……
        我们希望这个版块可以很好的适配Web时代,期待更好的互联网技术能够使得MFC技术框架得以重现活力,……

试试用AI创作助手写篇文章吧