指针指向不正确

Pointer Not Pointing Correctly?

本文关键字:不正确 指针      更新时间:2023-10-16

我在一个线程中运行这段代码,假设receivedPosts和td->window是有效的:

std::vector<Post> *receivedPosts_n = new std::vector<Post>;
*receivedPosts_n = receivedPosts;
SendMessage(td->window, WM_TIMER, IDT_TIMER_FIND_NEW_POSTS_CALLBACK, 
    (LPARAM) receivedPosts_n);

我在IDT_TIMER_FIND_NEW_POSTS_CALLBACK (hwnd是td->窗口)运行这段代码:

case IDT_TIMER_FIND_NEW_POSTS_CALLBACK:
    {
        std::vector<Post> *currentPosts_ptr = (std::vector<Post> *)lParam;
        //This vector turns up as undefined
        std::vector<Post> currentPosts = *currentPosts_ptr;
    }
    break;

但是问题是*currentPosts_ptr变成了一个无效的指针,即它指向随机内存。

指针有什么问题?

谢谢。

MSDN文档说对于WM_TIMER消息lParam的值是

一个指向应用程序定义的回调函数的指针,在安装计时器时传递给SetTimer函数。

如果您需要发送自定义消息,那么使用专门为这种情况设计的WM_USER通过0x7FFF是更好的主意。