Merge pull request #228 from lytico/lytico/issues/226

add test for https://github.com/GtkSharp/GtkSharp/issues/226
This commit is contained in:
lytico 2021-03-18 19:07:50 +01:00 committed by GitHub
commit 9bec86e406
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23

View file

@ -0,0 +1,93 @@
using System;
using System.Threading;
using Gdk;
using Gtk;
namespace Samples
{
[Section (ContentType = typeof(PixbufDemo), Category = Category.Miscellaneous)]
class PixbufSection : ListSection
{
public PixbufSection ()
{
AddItem ($"Press button to run / stop {nameof(PixbufDemo)} :", new PixbufDemo ("Press me"));
}
}
class PixbufDemo : Button
{
public PixbufDemo (string text) : base (text) { }
private bool running = false;
public void DispatchPendingEvents ()
{
// The loop is limited to 1000 iterations as a workaround for an issue that some users
// have experienced. Sometimes EventsPending starts return 'true' for all iterations,
// causing the loop to never end.
int n = 1000;
Gdk.Threads.Enter ();
while (Gtk.Application.EventsPending () && --n > 0) {
Gtk.Application.RunIteration (false);
}
Gdk.Threads.Leave ();
}
protected override void OnPressed ()
{
base.OnPressed ();
var count = 0;
if (running) {
running = false;
return;
}
var startmem = GC.GetTotalMemory (true);
var testfile = "Textpic.png";
using (var writeTestFile = new Pixbuf (typeof(ImageSection).Assembly, "Testpic")) {
writeTestFile.Save (testfile, "png");
}
using (var heatup = new Pixbuf (testfile)) {
ApplicationOutput.WriteLine ($"{nameof(heatup)}.{nameof(Pixbuf.ByteLength)}\t{heatup.ByteLength:N0}");
}
startmem = GC.GetTotalMemory (true);
ApplicationOutput.WriteLine ($"{nameof(GC.GetTotalMemory)} at start: {startmem:N}");
running = true;
var memAllocated = 0UL;
while (running) {
using (var source = new Pixbuf (typeof(ImageSection).Assembly, "Testpic")) {
memAllocated += source.ByteLength;
count++;
}
DispatchPendingEvents ();
if (!running)
break;
}
var endmem = GC.GetTotalMemory (true);
ApplicationOutput.WriteLine ($"Leak:\t{(endmem - startmem):N0}\t{nameof(memAllocated)}");
ApplicationOutput.WriteLine ($"{nameof(GC.GetTotalMemory)} at start: {startmem:N0}\tat end: {endmem:N0}\t{nameof(Pixbuf)} created: {count}");
}
}
}