Clicking the text tool for the first time sometimes causes an exception

Bug #890589 reported by Robert Nordan on 2011-11-15
6
This bug affects 1 person
Affects Status Importance Assigned to Milestone
Pinta
Medium
Robert Nordan

Bug Description

Sometimes when you click the text tool for the first time an exception will be raised and caught with this text (in debug mode):

System.Reflection.TargetInvocationException: Exception has been thrown by the target of an invocation. ---> System.NullReferenceException: Object reference not set to an instance of an object
  at Pinta.Core.FontManager.GetSizes (Pango.FontFace fontFace) [0x00024] in /home/rpvn/vcs/Pinta/Pinta.Core/Managers/FontManager.cs:81
  at Pinta.Core.FontManager.GetSizes (Pango.FontFamily family) [0x00000] in /home/rpvn/vcs/Pinta/Pinta.Core/Managers/FontManager.cs:64
  at Pinta.Tools.TextTool.UpdateFontSizes () [0x0002d] in /home/rpvn/vcs/Pinta/Pinta.Tools/Tools/TextTool/TextTool.cs:252
  at Pinta.Tools.TextTool.OnBuildToolBar (Gtk.Toolbar tb) [0x00652] in /home/rpvn/vcs/Pinta/Pinta.Tools/Tools/TextTool/TextTool.cs:209
  at Pinta.Core.BaseTool.DoBuildToolBar (Gtk.Toolbar tb) [0x00000] in /home/rpvn/vcs/Pinta/Pinta.Core/Classes/BaseTool.cs:95
  at Pinta.Core.ToolManager.SetCurrentTool (Pinta.Core.BaseTool tool) [0x0009d] in /home/rpvn/vcs/Pinta/Pinta.Core/Managers/ToolManager.cs:124
  at Pinta.Core.ToolManager.HandlePbToolItemClicked (System.Object sender, System.EventArgs e) [0x00053] in /home/rpvn/vcs/Pinta/Pinta.Core/Managers/ToolManager.cs:75
  at (wrapper managed-to-native) System.Reflection.MonoMethod:InternalInvoke (System.Reflection.MonoMethod*,object,object[],System.Exception&)
  at System.Reflection.MonoMethod.Invoke (System.Object obj, BindingFlags invokeAttr, System.Reflection.Binder binder, System.Object[] parameters, System.Globalization.CultureInfo culture) [0x000d5] in /usr/src/packages/BUILD/mono-2.8.2/mcs/class/corlib/System.Reflection/MonoMethod.cs:223
  --- End of inner exception stack trace ---
  at System.Reflection.MonoMethod.Invoke (System.Object obj, BindingFlags invokeAttr, System.Reflection.Binder binder, System.Object[] parameters, System.Globalization.CultureInfo culture) [0x000f0] in /usr/src/packages/BUILD/mono-2.8.2/mcs/class/corlib/System.Reflection/MonoMethod.cs:231
  at System.Reflection.MethodBase.Invoke (System.Object obj, System.Object[] parameters) [0x00000] in /usr/src/packages/BUILD/mono-2.8.2/mcs/class/corlib/System.Reflection/MethodBase.cs:96
  at System.Delegate.DynamicInvokeImpl (System.Object[] args) [0x000bf] in /usr/src/packages/BUILD/mono-2.8.2/mcs/class/corlib/System/Delegate.cs:401
  at System.MulticastDelegate.DynamicInvokeImpl (System.Object[] args) [0x00018] in /usr/src/packages/BUILD/mono-2.8.2/mcs/class/corlib/System/MulticastDelegate.cs:69
  at System.Delegate.DynamicInvoke (System.Object[] args) [0x00000] in /usr/src/packages/BUILD/mono-2.8.2/mcs/class/corlib/System/Delegate.cs:375
  at GLib.Signal.ClosureInvokedCB (System.Object o, GLib.ClosureInvokedArgs args) [0x0004f] in /usr/src/packages/BUILD/gtk-sharp-2.12.10/glib/Signal.cs:195
  at GLib.SignalClosure.Invoke (GLib.ClosureInvokedArgs args) [0x0000c] in /usr/src/packages/BUILD/gtk-sharp-2.12.10/glib/SignalClosure.cs:118
  at GLib.SignalClosure.MarshalCallback (IntPtr raw_closure, IntPtr return_val, UInt32 n_param_vals, IntPtr param_values, IntPtr invocation_hint, IntPtr marshal_data) [0x00075] in /usr/src/packages/BUILD/gtk-sharp-2.12.10/glib/SignalClosure.cs:146

This would appear to be because the fonts aren't finished initializing or something similar. After closing the exception window you can type text but not change font size. If you change to a different font type and then back again, you can change font size. (Probably initialized by then.)

The fix is probably to catch the exception earlier and substitute the default font size.

Robert Nordan (rpvn) on 2011-11-15
description: updated
Robert Nordan (rpvn) wrote :

OK, there is something wrong with the underlying Pango library (or pango-sharp which is the gobetween).

When you call this method in FontManager.cs, it's supposedto return nsizes = 0 if the font is not a bitmap font. (Most fonts are vector fonts these days, so they will return 0)

// Query for supported sizes for this font
fontFace.ListSizes (out sizes, out nsizes);

if (nsizes == 0)
 return default_font_sizes;

List<int> result = new List<int> ();

for (int i = 0; i < nsizes; i++)
 result.Add (*(&sizes + 4 * i)); //Here's where things go wrong....

However, sometimes it returns garbage like 32767 which means it's returning uninitialized values, but that will still pass the check. Now, putting a try/catch around the function trying to recover the sizes will work in debug mode and catch the nullreference exception, but not when you run without debug because such things aren't checked then. (And then the font menu is filled with gibberish numbers.)

There is a real ugly hack that can fix this, but I'm not sure I want to do it:

//Ugly hack to deal with pango returning garbage when not finished initiliazing
if (nsizes <= 0 || nsizes > 200)
 return default_font_sizes;

Is it worth it to try and chase this down further or should we just do it the easy way?

grofaty (grofaty) wrote :

I have never had any crash with Text object. Are you using some exsotic font? Does this happen with any font or just some of them?

Robert Nordan (rpvn) wrote :

It's always trying to look up Adobe Courier (probably comes from installing Flash) when it happens, but that's only because Adobe Courier is the alphabetically first font and is therefore selected when I click the text tool. I'm fairly sure it's Pango or pango-sharp messing up, because even if there was some exotic font problem they should take care of it rather than pass it on to us.

Cameron White (cameronwhite91) wrote :

It might be worth mentioning upstream, since this seems like a very odd issue.

I've never seen any crashes with the text tool, though.

Robert Nordan (rpvn) wrote :

Yeah, I don't know if I have some strange setup or something. Probably need to investigate further before making any rash reports. :P

Robert Nordan (rpvn) wrote :

I recently ran into this issue again, and after a bit of experimenting I noticed something: It only happens (sometimes) when I run Pinta through the MonoDevelop debug command. This would explain why this error hasn't been spotted by anyone else! :P

So, it could be that it is caused by the debug enviroment slowing things down so that the font manager is starved of input somehow. Cameron, try running in debug a couple of times and clicking the text tool, to see if it happens for you too?

Cameron White (cameronwhite91) wrote :

I tried a few times and didn't get a crash. However, I'm on a fresh install of 12.04 and don't have any fonts installed beyond the default ones.

tags: added: text-tool
To post a comment you must log in.
This report contains Public information  Edit
Everyone can see this information.

Other bug subscribers