Disclaimer: This is a personal web page. Contents written here do not represent the position of my employer.

Monday, March 24, 2008

 

Unmanaged documentation hard to manage

I'm not supposed to deal much with low level technologies (unmanaged code, C language...), but in order to test things from the beginning to allow myself understand things better, I'm dealing with Atk for later translating code to C#+Atk#.

One of the things I hate more is not having the excellent refactoring options that the modern IDE's nowadays can provide. For example, how do I know where is some element defined (in my code? in a reference? in which reference?)? Sometimes I find myself copy-pasting a function into another empty test project and start adding references to it until it compiles. Someone would say: hey but there's a lot of documentation out there! Really? Take this example:

Function: g_type_class_ref
- It smells like a Glib/Gobject function.
- Surprisingly, calling it in a test project that has a reference to glib doesn't work.
- More surprisingly, calling it in a test project that has a reference to glib and atk works.
- Mmmm, but it doesn't smell like an ATK component, right? Maybe atk ref already depends on the lib I'm looking for...
- Let's google it...
- WTF! the majority of links provided are forums, not docs.
- Finally got one from the LinuxFoundation, which tells me something about a libgobject-2.0 thingy... but that lib is not in my system.
- Anyway, the page redirects me to the "Gobject 2.6.2 Reference Manual"...
- But! it's a broken link which ends on main gtk.org doc page.
- Well, then, let's look for "site:gtk.org g_type_class_ref"... No results!

Ok ok, this is not C language fault, but how do you guys get to know these type of things? I hope you don't download all the source code of all these libs and grep for the word you're looking for... :D

Please make me feel dumb if you want to reply, and I will learn one more interesting thing today :)

In managed code with an IDE, you just have to right-click on the method and select "Go to definition" and if the method is found in a binary reference, there are different behaviours depending on the IDE you use, if you use VS2005 or later, you get an Object Browser that shows you the API of the library, and with MonoDevelop it tries to use the debugging symbols for showing the source code or otherwise it shows a minibug.

SOLUTION of the QUIZ: I had to look for the library 'gobject-2.0', not libgobject-2.0. Of course, Mike Gorse was the unmanaged expert that made me feel dumb! :D

SOLUTION to avoid the QUIZ to other people in the future: bug 369927. Mmmm, it has been closed as WONTFIX, maybe the unmanaged side of things should not get easier :)

Another mini-quiz that I solved also recently comes from the managed world. Do you know how static constructors work? They get called automatically whenever the code flow comes in the type where they lay. Example:


public static class MainClass
{
  public static void Main(string[] args)
  {
    Console.WriteLine("begin");
    new StaticCtorTest(1);
    new StaticCtorTest(2);
    Console.WriteLine("end");
  }
}

public class StaticCtorTest
{
  public StaticCtorTest(int initValue)
  {
    Console.WriteLine("Normal ctor");
    this.initValue = initValue;
  }

  static StaticCtorTest()
  {
    Console.WriteLine("Static ctor");
  }

  private int initValue;
}


What's the output of this test? This:


begin
Static ctor
Normal ctor
Normal ctor
end


Now, what happens if we drop the custom constructor? We can still create objects from our class, but using the implicit empty constructor:


public static class MainClass
{
  public static void Main(string[] args)
  {
    Console.WriteLine("begin");
    new StaticCtorTest();
    new StaticCtorTest();
    Console.WriteLine("end");
  }
}

public class StaticCtorTest
{
  static StaticCtorTest()
  {
    Console.WriteLine("Static ctor");
  }

  private int initValue;
}


Output:


begin
Static ctor
end


This constructor can only be called when no other constructors have been defined. Example: the following code will give a compiler error:


public static class MainClass
{
  public static void Main(string[] args)
  {
    Console.WriteLine("begin");
    new StaticCtorTest(); //The type `StaticCtorTest' does not contain a constructor that takes `0' arguments(CS1729)
    Console.WriteLine("end");
  }
}

public class StaticCtorTest
{
  public StaticCtorTest(int initValue)
  {
    Console.WriteLine("Normal ctor");
    this.initValue = initValue;
  }

  static StaticCtorTest()
  {
    Console.WriteLine("Static ctor");
  }

  private int initValue;
}


Ok, but here comes the surprise. What happens if, instead of using a class, you use a struct?


public static class MainClass
{
  public static void Main(string[] args)
  {
    Console.WriteLine("begin");
    new StaticCtorTest();
    Console.WriteLine("end");
  }
}

public struct StaticCtorTest
{
  public StaticCtorTest(int initValue)
  {
    Console.WriteLine("Normal ctor");
    this.initValue = initValue;
  }

  static StaticCtorTest()
  {
    Console.WriteLine("Static ctor");
  }

  private int initValue;
}


What happens is that you don't receive any compiler error, and thus you may think that the static constructor is being called, however, the output:


begin
end


WTF! This stupid thing made mkestner and me feel like crazy, wondering if any JIT optimization would kill our call in the GLibSharp bindings.

Fortunately, with this little research and his help, all was solved and I can keep on concentrating on how to complete Atk# in order to get my toplevel-with-n-children sample to work, and finishing the documentation that is still in a draft stage.

Labels: , , , ,


Comments:
Hey, coincidentally i wrote a blog entry a while back that might address your code navigation woes.

http://bgmerrell.blogspot.com/2008/02/first-of-all-let-me-introduce-ctags-and.html
discusses using exuberant ctags and vim to gain some IDE-like features.

Specifically item 10 talks about the "go to definition" feature. However, I don't know if/how it handles a binary reference.

There's likely a lot more functionality and power than I talk about...
 
Post a Comment

Links to this post:

Create a Link



<< Home

This page is powered by Blogger. Isn't yours?

Categories

RSS of the category Gnome
RSS of the category Mono
RSS of the category C#
RSS of the category Programming
RSS of the category Mozilla
RSS of the category Web Development
RSS of the category Security
RSS of the category Open Source
RSS of the category Engineering
RSS of the category Misc
RSS of the category Politics

Contact with me:
aaragonesNOSPAMes@gnNOSPAMome.org

Archive
My Photo
Name:
Location: London, United Kingdom
Follow me on Twitter