Warning: file_get_contents(/data/phpspider/zhask/data//catemap/4/wpf/12.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Wpf 为什么我的PictureBox加载例程会泄漏内存?_Wpf_Memory Leaks_C++ Cli_Picturebox - Fatal编程技术网

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

Wpf 为什么我的PictureBox加载例程会泄漏内存?,wpf,memory-leaks,c++-cli,picturebox,Wpf,Memory Leaks,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

我一直在尝试,但我的解决方案似乎存在内存泄漏:

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?

两个词:垃圾收集

奇怪的是,这看起来实际上是鼠标悬停在按钮上造成的。每次这样做时,内存都会跳跃,但在足够多的鼠标移动之后,内存使用就会稳定下来。实际点击按钮(即调用我的例程)不会导致任何泄漏。

这是我最初的反应,但明确删除的对象应该可以避免这些影响,不是吗?我认为清理只会清理非托管的一面。其余的将在稍后由GC清理。。。