为什么我的 PictureBox 加载例程会泄漏内存?

发布于 2024-08-07 05:26:34 字数 1329 浏览 1 评论 0原文

我一直尝试在 C++/CLI 应用程序中交换 PictureBox 中的图像,但我的解决方案似乎存在内存泄漏:

System::Void button1_Click(System::Object^  sender, System::EventArgs^  e)
{
    // Pick a new bitmap
    static int resource = IDB_BITMAP1;
    if( resource == IDB_BITMAP2)
    {
        resource = IDB_BITMAP1;
    }
    else
    {
        resource = IDB_BITMAP2;
    }

    // Get the primary module
    Module^ mod = Assembly::GetExecutingAssembly()->GetModules()[0];

    // Get the instance handle 
    IntPtr hinst = Marshal::GetHINSTANCE(mod);

    // Get the bitmap as unmanaged
    HANDLE hbi = LoadImage((HINSTANCE) hinst.ToPointer(),MAKEINTRESOURCE(resource),IMAGE_BITMAP,0,0,LR_DEFAULTCOLOR); 

    // Import the unmanaged bitmap into the managed side 
    Bitmap^ bi = Bitmap::FromHbitmap(IntPtr(hbi));

    // Remove any previously stored images
    if(m_pictureBox1->Image != nullptr)
    {
        delete m_pictureBox1->Image;
        m_pictureBox1->Image = nullptr;
    }

    // Insert the bitmap into the picture box
    m_pictureBox1->Image = bi;

    // Free up the unmanaged bitmap
    DeleteObject(hbi);
}

据我所知,我明确释放了内存,那么为什么任务管理器会这样做每次单击按钮时都会报告内存增加约 24k?

I've been trying to swap images in a PictureBox in a C++/CLI application but my solution appears to have a memory leak:

System::Void button1_Click(System::Object^  sender, System::EventArgs^  e)
{
    // Pick a new bitmap
    static int resource = IDB_BITMAP1;
    if( resource == IDB_BITMAP2)
    {
        resource = IDB_BITMAP1;
    }
    else
    {
        resource = IDB_BITMAP2;
    }

    // Get the primary module
    Module^ mod = Assembly::GetExecutingAssembly()->GetModules()[0];

    // Get the instance handle 
    IntPtr hinst = Marshal::GetHINSTANCE(mod);

    // Get the bitmap as unmanaged
    HANDLE hbi = LoadImage((HINSTANCE) hinst.ToPointer(),MAKEINTRESOURCE(resource),IMAGE_BITMAP,0,0,LR_DEFAULTCOLOR); 

    // Import the unmanaged bitmap into the managed side 
    Bitmap^ bi = Bitmap::FromHbitmap(IntPtr(hbi));

    // Remove any previously stored images
    if(m_pictureBox1->Image != nullptr)
    {
        delete m_pictureBox1->Image;
        m_pictureBox1->Image = nullptr;
    }

    // Insert the bitmap into the picture box
    m_pictureBox1->Image = bi;

    // Free up the unmanaged bitmap
    DeleteObject(hbi);
}

As far as I can see, I'm explicitely releasing the memory so why does task manager report an ~24k increase in memory each time the button is clicked?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

决绝 2024-08-14 05:26:34

两个词:垃圾收集

two words: garbage collection

情定在深秋 2024-08-14 05:26:34

奇怪的是,这实际上看起来是当您将鼠标悬停在按钮上时引起的。每次执行此操作时,内存都会跳跃,但经过足够多的鼠标悬停后,内存使用量就会稳定下来。实际单击按钮(即调用我的例程)不会导致任何泄漏。

Bizarrely, this actually looks to be caused when you mouse-over the button. Each time you do that the memory jumps but after enough mouse-overs that memory usage stabilises. The actual clicks on the button (i.e. calls to my routine) don't cause any leakage.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文