لماذا يتم تحميل ذاكرة التسرب الروتينية لـ Picturebox؟

StackOverflow https://stackoverflow.com/questions/1566045

سؤال

لقد كنت محاولة تبديل الصور في مربع الصور في تطبيق C ++/CLI لكن يبدو أن حلي يحتوي على تسرب ذاكرة:

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 في كل مرة يتم فيها النقر فوق الزر؟

هل كانت مفيدة؟

المحلول

كلمتين: مجموعة القمامة

نصائح أخرى

بشكل غريب ، يبدو أن هذا يحدث في الواقع عند الفأر فوق الزر. في كل مرة تقوم فيها ، تقفز الذاكرة ولكن بعد ما يكفي من عمليات الاستخدام التي يستقرها استخدام الذاكرة. لا تسبب النقرات الفعلية على الزر (أي مكالمات إلى روتيني) أي تسرب.

مرخصة بموجب: CC-BY-SA مع الإسناد
لا تنتمي إلى StackOverflow
scroll top