Re: [Vala] Property getters and memory management



On Fri, 2008-12-19 at 17:22 +0100, Ali Sabil wrote:

On Fri, Dec 19, 2008 at 1:29 PM, Jürg Billeter <j bitron ch> wrote:
        As mentioned in an other thread, I'm considering to change property
        getters to transfer ownership by default. While the current behavior
        works fine in many cases, in some cases it does not work at all and can
        cause bugs in the resulting application or library.
        
        Whenever you want to return a constructed - instead of stored - string
        or object, the property getters need to transfer ownership, which is
        only possible with an unintuitive syntax in Vala 0.5.3 and earlier.
        
        As I understand that Vala libraries, that are used from C, might not
        want to return an owned value from property getters in most cases, we
        will certainly continue to support this without restrictions.


I am not entirely sure about the use of "owned" property getters, I
think the default should remain "unowned", not that it is better, but
to remain consistent with the established conventions in the C/GObject
world, and avoid confusion. Also, from my experience, the use of
"owned" properties is pretty limited. So the syntax I am proposing for
it is:

         public string foo { owned get; set; }

What do you think about it?

My main concern is that the default behavior makes it too easy to break
your application. You can write code in property getters that doesn't
look incorrect but may corrupt your memory. Consider the following
example:

public class Test {
        bool frobnicate;

        public string foo {
                get {
                        string result;
                        if (frobnicate) {
                                result = "foo";
                        } else {
                                result = "bar";
                        }
                        return result;
                }
        }
}

void main () {
        var t = new Test ();
        message ("%s", t.foo);
}

That's why in my opinion, defaulting to owned return values makes a lot
more sense, from the Vala language perspective. However, I understand
that the inconsistency with C ABI is an issue, even though you will
always be able to explicitly specify ownership behavior. I don't see a
way right now how we can address both issues at once, though. Any ideas?

Jürg




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