Re: [Vala] Some basic questions
- From: Mikael Hermansson <mike tielie gmail com>
- To: Vala ML <vala-list gnome org>
- Subject: Re: [Vala] Some basic questions
- Date: Sun, 23 Mar 2008 13:13:25 +0100
On Sat, 2008-03-22 at 19:23 +0000, Phil Housley wrote:
On 22/03/2008, Mikael Hermansson <mike tielie gmail com> wrote:
1:
If I want a copy of Gtk.TreePath I have to use weak but the problem
is
howto free it after use?
TreePath a;
weak TreePath b;
b=a.copy();
b.[free/unref] <- does not work.
You should never have to free anything in Vala, except where you
specifically choose not to use the automatic memory management (by
using pointers.) When you get a weak reference to an object, you are
accepting that the object might be free'd even though you still have a
reference to it - this usually implies that the actual management of
that object is totally hidden away inside a library.
valac --pkg gtk+-2.0
If I take a copy of TreeView it will not free the object.
Example code here:
using Gtk;
public class Tree
{
TreeView tw;
construct
{
TreeIter iter;
TreePath path;
weak TreePath path2;
tw = new TreeView.with_model(new ListStore(1, typeof(string)));
var model =(ListStore) tw.get_model();
model.append(out iter);
model.get_iter_first(out iter);
path = model.get_path(iter);
path2=path.copy();
}
}
Created C code last line in the tree_constructur:
path2 = gtk_tree_path_copy (path);
(path == NULL ? NULL : (path = (gtk_tree_path_free
(path), NULL)));
}
This means app will leak the gtk.TreePath....
List selection = tw.get_tree_selection().get_selected_rows();
Again, you never need to free. Probably the weak ref is used because
the library might decide to free the object as soon as it is no longer
valid.
Well then the binding is wrong because Gtk reference API says returned
selection list should be free after use and if I take a close look in
the C source created it will not free the list and its objects.
I dont know howto fix the binding maybe I should send a bugreport about
this...
3: Also the model parameter should not be unrefed
TreeModel model;
selections=treeviewPlaylist.get_selection().get_selected_rows( out
model);
if (selection)
return ;
Should it definitely not be unref'd? Normally and out parameter will
result in a strong reference being made, and so an unref is needed
when the local variable goes out of scope. You could well be right in
this case though, I haven't looked at what this code actually does.
If i not pass weak TreeModel to the .get_selected_rows(model) it will
unref the object and the result is a corrupt TreeModel.
Greets
Mikael Hermansson
[
Date Prev][
Date Next] [
Thread Prev][
Thread Next]
[
Thread Index]
[
Date Index]
[
Author Index]