<commit_before>/*
 * Copyright (C)
 *   <NAME> <<EMAIL>>
 *   <NAME> <<EMAIL>>
 *
 * See COPYING.LIB for the License of this software
 *
 * Sample code for :
 * Functions :
 *      Connect.OpenAuth
 *      Domain.GetInfo
 *      Domain.LookupByName
 *      Errors.GetLastError
 *      Errors.SetErrorFunc
 *
 * Types :
 *      DomainInfo
 *      Error
 *
 */

using System;
using Gtk;
using Libvirt;

public partial class MainWindow : Gtk.Window
{
	public MainWindow () : base(Gtk.WindowType.Toplevel)
	{
		Build ();
	}

	protected void OnDeleteEvent (object sender, DeleteEventArgs a)
	{
		Application.Quit ();
		a.RetVal = true;
	}
	protected virtual void OnButton1Clicked (object sender, System.EventArgs e)
	{
		// Connect to the host
		IntPtr conn = Connect.Open(entry1.Text);
		if (conn != IntPtr.Zero)
		{
			// Set error callback. The method "ErrorCallback" will be called when error raised
			Errors.SetErrorFunc(IntPtr.Zero, ErrorCallback);
			// Try to look up the domain by name
			IntPtr domain = Domain.LookupByName(conn, entry2.Text);
            DomainInfo di = new DomainInfo();
            Domain.GetInfo(domain, di);
			entry3.Text = di.State.ToString();
			entry4.Text = di.maxMem.ToString();
			entry5.Text = di.memory.ToString();
			entry6.Text = di.nrVirtCpu.ToString();
			entry7.Text = di.cpuTime.ToString();
		}
		else
		{
			// Get the last error
			Error err = Errors.GetLastError();
			ShowError(err);
		}
	}
	
	private void ErrorCallback(IntPtr userData, Error error)
    {
        ShowError(error);
    }
	
	private void ShowError(Error error)
	{
		MessageDialog errorMsg = new MessageDialog(this, DialogFlags.Modal, MessageType.Error, ButtonsType.Close, error.Message);
		errorMsg.Title = "Error";
		if ((ResponseType) errorMsg.Run() == ResponseType.Close)
		{
			errorMsg.Destroy();
		}
	}
}

<commit_msg>Don't leak domain and connection in virConnectSetErrorFunc example<commit_after>/*
 * Copyright (C)
 *   <NAME> <<EMAIL>>
 *   <NAME> <<EMAIL>>
 *
 * See COPYING.LIB for the License of this software
 *
 * Sample code for :
 * Functions :
 *      Connect.OpenAuth
 *      Domain.GetInfo
 *      Domain.LookupByName
 *      Errors.GetLastError
 *      Errors.SetErrorFunc
 *
 * Types :
 *      DomainInfo
 *      Error
 *
 */

using System;
using Gtk;
using Libvirt;

public partial class MainWindow : Gtk.Window
{
	public MainWindow () : base(Gtk.WindowType.Toplevel)
	{
		Build ();
	}

	protected void OnDeleteEvent (object sender, DeleteEventArgs a)
	{
		Application.Quit ();
		a.RetVal = true;
	}
	protected virtual void OnButton1Clicked (object sender, System.EventArgs e)
	{
		// Connect to the host
		IntPtr conn = Connect.Open(entry1.Text);
		if (conn != IntPtr.Zero)
		{
			// Set error callback. The method "ErrorCallback" will be called when error raised
			Errors.SetErrorFunc(IntPtr.Zero, ErrorCallback);
			// Try to look up the domain by name
			IntPtr domain = Domain.LookupByName(conn, entry2.Text);
			
			if (domain != IntPtr.Zero)
			{
				DomainInfo di = new DomainInfo();
				Domain.GetInfo(domain, di);
				entry3.Text = di.State.ToString();
				entry4.Text = di.maxMem.ToString();
				entry5.Text = di.memory.ToString();
				entry6.Text = di.nrVirtCpu.ToString();
				entry7.Text = di.cpuTime.ToString();
			
				Domain.Free(domain);
			}

			Connect.Close(conn);
		}
		else
		{
			// Get the last error
			Error err = Errors.GetLastError();
			ShowError(err);
		}
	}
	
	private void ErrorCallback(IntPtr userData, Error error)
    {
        ShowError(error);
    }
	
	private void ShowError(Error error)
	{
		MessageDialog errorMsg = new MessageDialog(this, DialogFlags.Modal, MessageType.Error, ButtonsType.Close, error.Message);
		errorMsg.Title = "Error";
		if ((ResponseType) errorMsg.Run() == ResponseType.Close)
		{
			errorMsg.Destroy();
		}
	}
}

{'fraction_non_alphanumeric': 0.1200524246395806, 'fraction_numerical': 0.0041939711664482305, 'mean_word_length': 3.050955414012739, 'pattern_counts': {'":': 0, '<': 15, '<?xml version=': 0, '>': 15, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '4624276', 'n_tokens_mistral': 1369, 'n_tokens_neox': 1230, 'n_words': 306}