[Gtk-sharp-list] Problem with Gtk.Clipboard copy/paste

alf alf82@freemail.gr
Fri, 29 Oct 2004 17:43:08 +0300


I have been experimenting with the copy/paste clipboard features of Gtk#
and I have run into something that may be a bug. I am using Gtk# 1.0.2
and Mono 1.1.1.

For example:

using System;
using Gtk;

class CopyPasteTest:Gtk.Window {

Gtk.Clipboard clipboard;

static TargetEntry[] targets= new TargetEntry[]{
new TargetEntry("UTF8_STRING", 0, 0)};

public static void Main(string[] args)
	Application.Init ();
	new CopyPasteTest ();
	Application.Run ();

public CopyPasteTest () : base ("CopyPasteTest")
	this.SetDefaultSize (400, 300);
	this.DeleteEvent += new DeleteEventHandler (OnDelete);

	clipboard=Gtk.Clipboard.Get(Gdk.Atom.Intern("CLIPBOARD", true));

	Button btn = new Button ("Copy");
	btn.Clicked += new EventHandler (OnCopyClicked);

	this.ShowAll ();

void OnClipboardGet(Clipboard cb, ref SelectionData sd, uint i)
	Gdk.Atom atom=Gdk.Atom.Intern("UTF8_STRING",false);
	sd.Set(atom, 8, new byte[]{0x31,0x32,0x33});

void OnClipboardClear(Clipboard cb)


void OnCopyClicked(object o, EventArgs args)
	clipboard.SetWithData(targets, new ClipboardGetFunc(OnClipboardGet),
new ClipboardClearFunc(OnClipboardClear));

	// this works fine 

void OnDelete (object o, DeleteEventArgs args)
	Application.Quit ();


When I click on the copy button and try to paste in some other
application that accepts UTF8_STRING (eg gedit) my program crashes with:

Unhandled Exception: System.NullReferenceException: Object reference not
set to an instance of an object
in <0xffa6e8e0> (wrapper managed-to-native)
Gtk.SelectionData:gtk_selection_data_set (intptr,intptr,int,byte[],int)
in <0x00004> (wrapper managed-to-native)
Gtk.SelectionData:gtk_selection_data_set (intptr,intptr,int,byte[],int)
in <0x0003e> Gtk.SelectionData:Set (Gdk.Atom,int,byte[],int)
in <0x0001d> Gtk.SelectionData:Set (Gdk.Atom,int,byte[])
in [0x00025] (at /home/alf/Projects/CopyPasteTest/Main.cs:37)
CopyPasteTest:OnClipboardGet (Gtk.Clipboard,Gtk.SelectionData&,uint)
in <0x00076> (wrapper delegate-invoke)
in <0x0007b> GtkSharp.ClipboardGetFuncWrapper:NativeCallback
in <0x0009f> (wrapper native-to-managed)
in <0xff97bec1> (wrapper managed-to-native) Gtk.Application:gtk_main ()
in <0x00004> (wrapper managed-to-native) Gtk.Application:gtk_main ()
in <0x00007> Gtk.Application:Run ()
in [0x0000b] (at /home/alf/Projects/CopyPasteTest/Main.cs:15)
CopyPasteTest:Main (string[])

If in the method OnCopyClicked() I use clipboard.SetText() instead of
clipboard.SetWithData() everything runs normally. Unfortunately, I need
to use other types besides the string type.

I would post this to bugzilla but I first wanted to make sure that 
this is not a problem with my code.

Any thoughts?


http://www.freemail.gr - δωρεάν υπηρεσία ηλεκτρονικού ταχυδρομείου.
http://www.freemail.gr - free email service for the Greek-speaking.