0

I limited the lines of code causing a memory leak to the following useless piece of code obtained from the original windows application.

I already know the general issue with timers causing memory leak when the variables inside the callback (or tick event) are not properly disposed, that's why I put the two "using" sections in my function. Still I can not see what I am leaving unmanaged.

I thought about timer bombing but even if I set 1000 ms as interval the memory still increases until I get a memory exception.

Blockquote

class Program
{
    private static bool locked {get; set; }

    static void Main(string[] args)
    {
        System.Threading.Timer timer_log = new System.Threading.Timer(t_callback, null, 0, 100);
        Application.Run();
    }

    private static void t_callback(Object state)
    {
        if (locked == false)
        {
            locked = true;
            Leak.SetIcon();
            locked = false;
        }

    }
}

static class Leak
{
    private static NotifyIcon notifyIcon = new NotifyIcon();

    static public void SetIcon()
    {
        using (Bitmap bitmapIcona = new Bitmap(Properties.Resources.LoggerServiceImg))
        {
            using (Graphics graphicsIcona = Graphics.FromImage(bitmapIcona))
            {
                notifyIcon.Icon = Icon.FromHandle(bitmapIcona.GetHicon());
            }
        }
    }
}

Blockquote

Boltzmann
  • 31
  • 6
  • I do not think https://stackoverflow.com/questions/661561/how-do-i-update-the-gui-from-another-thread is the answer because I do not have a GUI, this is a windows application without a GUI, only a notify icon. I already tried other kinds of timer (also System.Widows.Forms.Timer) but no luck until now. – Boltzmann Sep 18 '20 at 15:35
  • I don't know in fact, hence my question. I searched and it seems there is no problem. https://learn.microsoft.com/dotnet/api/system.windows.forms.notifyicon & http://www.java2s.com/Tutorial/CSharp/0460__GUI-Windows-Forms/UseTimertoupdateNotifyIcon.htm & https://www.codeproject.com/Questions/1017734/Show-balloontip-inside-thread-in-csharp. So it is the recurrent problem with many bitmaps. There are many question about that problem on SO. Thus the problem is not related to a timer and you should have the same problem using a big loop. –  Sep 18 '20 at 15:43
  • You are right, I tried the same thing in just one thread and the problem remains, I will look for an healthier way to update the icon, ignoring the thread in which it is updated. – Boltzmann Sep 18 '20 at 16:03
  • 2
    This is the solution: https://learn.microsoft.com/en-us/dotnet/api/system.drawing.icon.fromhandle?redirectedfrom=MSDN&view=dotnet-plat-ext-3.1#System_Drawing_Icon_FromHandle_System_IntPtr_. Saving the icon created with Icon.FromHandle() in a temp variable and deleting it at the end of the loop by DestroyIcon(newIcon.Handle) solved the problem. My fault, I thought about cross-thread problems at the beginning and searched only that. – Boltzmann Sep 18 '20 at 16:14

0 Answers0