Re: [Vala] Singleton and reference count



Mike Massonnet píše v Út 27. 04. 2010 v 18:11 +0200:
2010/4/27 Sam Wilson <tecywiz121 hotmail com>:
On Tue, 2010-04-27 at 10:46 -0300, Jonh Wendell wrote:
Hi, folks. I wrote a simple singleton:

public class Prefs {
  private static Prefs instance;

  public static Prefs default() {
    if (instance == null)
      instance = new Prefs ();

    return instance;
  }


                public static unowned Preferences get_default () {
                        if (preferences == null) {
                                preferences = new Preferences ();
                                preferences.add_weak_pointer (&preferences);
                        }
                        else
                                preferences.ref ();

                        return preferences;
                }

[...]

[0] http://git.xfce.org/apps/xfmpc/tree/src/preferences.vala

Regards
Mike

I took the liberty of checking out your code (hope you don't mind) and I
believe you're using it quite wrong.
Your 'preferences' variable is still a strong reference, so it is never
freed. 'add_weak_pointer' only makes sense for pure pointer or an
unowned variable. Also, manually using .ref() method only makes sense if
you work with pure pointers, otherwise it messes up Vala's automatic
refcounting.

Check out my reply to the original message to see how it can be done
correctly (unless I made some terrible mistake, which I hope I
didn't). :)

Attachment: signature.asc
Description: Toto je digitálně podepsaná část zprávy



[Date Prev][Date Next]   [Thread Prev][Thread Next]   [Thread Index] [Date Index] [Author Index]