Re: [Vala] Another interesting List error



Hi Cayle,

On Mit, 2006-08-30 at 22:38 -0500, Cayle Graumann wrote:
    I have another interesting list error for you.   If you use a
function to create a list (e.g. a factory method) and return it back,
when you access the elements of that list a 14633 Critical error
results.  The list is not null, and the elements are not null.  This
happens regardless of the way you access the elements, either by
looping with foreach or using nth_data. 

using GLib;

namespace Error2 {
[...]
        public class ListConstructor {

                public ref List<ListElement> makeList() {

                        List<ListElement> list;

                        list.append(new ListElement(1,"String 1"));
                        list.append(new ListElement(2,"String 2"));
                        list.append(new ListElement(3,"String 3")); 
                        list.append(new ListElement(4,"String 4"));

                        return list;
                }
        }

The problem lies in the makeList() method in combination with the
unfinished work regarding memory management in generic containers. What
currently happens is that ListElement instances get destroyed right
after adding them to the list. The reason is that generic type arguments
default not to be owned by the container in local variables, they
default to be owned by the container in fields, though. I will probably
change it to always default to be owned as this seems to be a more
sensible and not that irritating default.

Due to a bug in the compiler, you can't even explicitly specify that you
want that the list owns the elements, i.e. `ref List<ref ListElement>
list' doesn't work.

As soon as that has been fixed, the preferred way to declare a new list
will be

        var list = new List<ListElement> ();

I hope to get that fixed at the weekend (or before), can't say for sure,
though.

Jürg




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