Fixed memory leak when scheduling timeout and idle callbacks

This commit is contained in:
nyx 2016-02-16 22:55:05 +03:00
parent 28ba4876e3
commit 114899f46e
2 changed files with 37 additions and 15 deletions

View file

@ -51,7 +51,12 @@ namespace GLib {
bool cont = idle_handler (); bool cont = idle_handler ();
if (!cont) if (!cont)
Remove (); {
lock (this)
{
Remove ();
}
}
return cont; return cont;
} catch (Exception e) { } catch (Exception e) {
ExceptionManager.RaiseUnhandledException (e, false); ExceptionManager.RaiseUnhandledException (e, false);
@ -70,8 +75,11 @@ namespace GLib {
public static uint Add (IdleHandler hndlr) public static uint Add (IdleHandler hndlr)
{ {
IdleProxy p = new IdleProxy (hndlr); IdleProxy p = new IdleProxy (hndlr);
p.ID = g_idle_add ((IdleHandlerInternal) p.proxy_handler, IntPtr.Zero); lock (p)
Source.AddSourceHandler (p.ID, p); {
p.ID = g_idle_add ((IdleHandlerInternal) p.proxy_handler, IntPtr.Zero);
Source.AddSourceHandler (p.ID, p);
}
return p.ID; return p.ID;
} }
@ -82,8 +90,11 @@ namespace GLib {
public static uint Add (IdleHandler hndlr, Priority priority) public static uint Add (IdleHandler hndlr, Priority priority)
{ {
IdleProxy p = new IdleProxy (hndlr); IdleProxy p = new IdleProxy (hndlr);
p.ID = g_idle_add_full ((int)priority, (IdleHandlerInternal)p.proxy_handler, IntPtr.Zero, null); lock (p)
Source.AddSourceHandler (p.ID, p); {
p.ID = g_idle_add_full ((int)priority, (IdleHandlerInternal)p.proxy_handler, IntPtr.Zero, null);
Source.AddSourceHandler (p.ID, p);
}
return p.ID; return p.ID;
} }

View file

@ -49,7 +49,12 @@ namespace GLib {
bool cont = timeout_handler (); bool cont = timeout_handler ();
if (!cont) if (!cont)
Remove (); {
lock (this)
{
Remove ();
}
}
return cont; return cont;
} catch (Exception e) { } catch (Exception e) {
ExceptionManager.RaiseUnhandledException (e, false); ExceptionManager.RaiseUnhandledException (e, false);
@ -65,9 +70,11 @@ namespace GLib {
public static uint Add (uint interval, TimeoutHandler hndlr) public static uint Add (uint interval, TimeoutHandler hndlr)
{ {
TimeoutProxy p = new TimeoutProxy (hndlr); TimeoutProxy p = new TimeoutProxy (hndlr);
lock (p)
p.ID = g_timeout_add (interval, (TimeoutHandlerInternal) p.proxy_handler, IntPtr.Zero); {
Source.AddSourceHandler (p.ID, p); p.ID = g_timeout_add (interval, (TimeoutHandlerInternal) p.proxy_handler, IntPtr.Zero);
Source.AddSourceHandler (p.ID, p);
}
return p.ID; return p.ID;
} }
@ -78,9 +85,11 @@ namespace GLib {
public static uint Add (uint interval, TimeoutHandler hndlr, Priority priority) public static uint Add (uint interval, TimeoutHandler hndlr, Priority priority)
{ {
TimeoutProxy p = new TimeoutProxy (hndlr); TimeoutProxy p = new TimeoutProxy (hndlr);
lock (p)
p.ID = g_timeout_add_full ((int)priority, interval, (TimeoutHandlerInternal) p.proxy_handler, IntPtr.Zero, null); {
Source.AddSourceHandler (p.ID, p); p.ID = g_timeout_add_full ((int)priority, interval, (TimeoutHandlerInternal) p.proxy_handler, IntPtr.Zero, null);
Source.AddSourceHandler (p.ID, p);
}
return p.ID; return p.ID;
} }
@ -91,9 +100,11 @@ namespace GLib {
public static uint AddSeconds (uint interval, TimeoutHandler hndlr) public static uint AddSeconds (uint interval, TimeoutHandler hndlr)
{ {
TimeoutProxy p = new TimeoutProxy (hndlr); TimeoutProxy p = new TimeoutProxy (hndlr);
lock (p)
p.ID = g_timeout_add_seconds (interval, (TimeoutHandlerInternal) p.proxy_handler, IntPtr.Zero); {
Source.AddSourceHandler (p.ID, p); p.ID = g_timeout_add_seconds (interval, (TimeoutHandlerInternal) p.proxy_handler, IntPtr.Zero);
Source.AddSourceHandler (p.ID, p);
}
return p.ID; return p.ID;
} }