Commit 527f621
Fix memory leaks
Changed files (4)
modifiedlibbirdfont/Glyph.valamodifiedlibbirdfont/GlyphCollection.valamodifiedlibbirdfont/Path.valamodifiedlibbirdfont/VersionList.vala
Diff
@@ -576,25 +576,40 @@ public class Glyph : FontDisplay { * @return false if no points was deleted */ public bool process_deleted () {- PathList remaining_points;+ Gee.ArrayList<Path> deleted_paths = new Gee.ArrayList<Path> (); foreach (Path p in path_list) { if (p.points.length () > 0) {- remaining_points = p.process_deleted_points ();- foreach (Path path in remaining_points.paths) {- add_path (path);- path.reopen ();- path.create_list ();- MainWindow.get_current_glyph ().add_active_path (path);- }- - if (remaining_points.paths.size > 0) {- delete_path (p);+ if (process_deleted_points_in_path (p)) { return true; } } else {- delete_path (p);+ deleted_paths.add (p); } }+ + foreach (Path p in deleted_paths) {+ delete_path (p);+ }+ + return false;+ }+ + private bool process_deleted_points_in_path (Path p) {+ PathList remaining_points;+ remaining_points = p.process_deleted_points ();+ foreach (Path path in remaining_points.paths) {+ add_path (path);+ path.reopen ();+ path.create_list ();+ + add_active_path (path);+ }+ + if (remaining_points.paths.size > 0) {+ delete_path (p);+ return true;+ }+ return false; } @@ -38,11 +38,11 @@ public class GlyphCollection : GLib.Object { public void insert_glyph (Glyph g, bool selected) { versions.add_glyph (g, selected); - assert (versions.glyphs.length () > 0);+ assert (versions.glyphs.size > 0); } public uint length () {- return versions.glyphs.length ();+ return versions.glyphs.size; } public string get_unicode () {@@ -791,12 +791,13 @@ public class Path { /** Variable precision */ public bool is_over_coordinate_var (double x, double y, double tolerance) {- List<EditPoint> ycoordinates = new List<EditPoint> ();+ Gee.ArrayList<EditPoint> ycoordinates = new Gee.ArrayList<EditPoint> (); double last = 0; bool on_edge = false; double last_x = 0; Path path; PathList pathlist;+ EditPoint next_e, last_e; if (points.length () < 2) { return false;@@ -827,8 +828,8 @@ public class Path { return false; } - if (Math.fabs (cx - x) < tolerance && Math.fabs (last - cy) > 2 * tolerance) {- ycoordinates.append (new EditPoint (cx, cy));+ if (Math.fabs (cx - x) < tolerance && Math.fabs (last - cy) > 10 * tolerance) {+ ycoordinates.add (new EditPoint (cx, cy)); last = cy; } @@ -840,7 +841,7 @@ public class Path { return true; } - if (ycoordinates.length () == 0) {+ if (ycoordinates.size == 0) { warning ("No ycoordinates is empty"); return true; }@@ -849,14 +850,14 @@ public class Path { return (a.y < b.y) ? 1 : -1; }); - for (int i = 0; i < ycoordinates.length () - 1; i++) {- if (Math.fabs (ycoordinates.nth (i).data.y - ycoordinates.nth (i + 1).data.y) < 4 * tolerance) {- ycoordinates.remove_link (ycoordinates.nth (i));+ for (int i = 0; i < ycoordinates.size - 1; i++) {+ if (Math.fabs (ycoordinates.get (i).y - ycoordinates.get (i + 1).y) < 4 * tolerance) {+ ycoordinates.remove_at (i); } } - if (unlikely (ycoordinates.length () % 2 != 0)) {- warning (@"not an even number of coordinates ($(ycoordinates.length ()))");+ if (unlikely (ycoordinates.size % 2 != 0)) {+ warning (@"not an even number of coordinates ($(ycoordinates.size))"); stderr.printf (@"(ymin <= y <= ymax) && (xmin <= x <= xmax);\n"); stderr.printf (@"($ymin <= $y <= $ymax) && ($xmin <= $x <= $xmax);\n"); @@ -867,31 +868,23 @@ public class Path { stderr.printf (@"$(e.y)\n"); } - if (ycoordinates.length () != 0) {- ycoordinates.append (ycoordinates.last ().data.copy ());+ if (ycoordinates.size != 0) {+ ycoordinates.add (ycoordinates.get (ycoordinates.size - 1).copy ());+ } else {+ return true; }- - return true; } - for (unowned List<EditPoint> e = ycoordinates.first (); true; e = e.next) {- return_val_if_fail (!is_null (e) || !is_null (e.data), true);- - if (y <= e.data.y + tolerance) {- return_val_if_fail (!is_null (e.next) || !is_null (e.data), true);- - e = e.next;-- if (y >= e.data.y - tolerance) {+ for (int i = 0; i < ycoordinates.size; i += 2) {+ last_e = ycoordinates.get (i);+ next_e = ycoordinates.get (i + 1);+ if (y <= last_e.y + tolerance) {+ if (y >= next_e.y - tolerance) { return true; } }- - if (e == ycoordinates.last ()) {- break;- } }-+ return false; } @@ -19,11 +19,11 @@ namespace BirdFont { public class VersionList : DropMenu { int current_version_id = -1;- public List<Glyph> glyphs;+ public Gee.ArrayList<Glyph> glyphs; public VersionList (Glyph? g = null) { base ("version");- glyphs = new List<Glyph> ();+ glyphs = new Gee.ArrayList<Glyph> (); set_direction (MenuDirection.POP_UP); @@ -31,22 +31,20 @@ public class VersionList : DropMenu { ma.has_delete_button = false; ma.action = (self) => { return_if_fail (self.parent != null);- return_if_fail (glyphs.length () > 0);+ return_if_fail (glyphs.size > 0); BirdFont.get_current_font ().touch (); add_new_version ();- current_version_id = glyphs.last ().data.version_id;+ current_version_id = glyphs.get (glyphs.size - 1).version_id; }; signal_delete_item.connect ((index) => {- unowned List<Glyph> gl; int current_version; index--; // first item is the add new action- return_if_fail (0 <= index < glyphs.length ());- gl = glyphs.nth (index);- glyphs.remove_link (gl);+ return_if_fail (0 <= index < glyphs.size);+ glyphs.remove_at (index); recreate_index (); @@ -54,8 +52,8 @@ public class VersionList : DropMenu { if (index == current_version) { set_selected_item (get_action_no2 ()); // select the first glyph if the current glyph is deleted } else if (index < current_version) {- return_if_fail (0 <= current_version - 1 < glyphs.length ());- current_version_id = glyphs.nth (current_version - 1).data.version_id;+ return_if_fail (0 <= current_version - 1 < glyphs.size);+ current_version_id = glyphs.get (current_version - 1).version_id; } }); @@ -81,6 +79,8 @@ public class VersionList : DropMenu { } public Glyph get_current () {+ Glyph gl;+ foreach (Glyph g in glyphs) { if (g.version_id == current_version_id) { return g;@@ -89,9 +89,10 @@ public class VersionList : DropMenu { warning (@"Can not find current glyph for id $current_version_id"); - if (glyphs.length () > 0) {- set_selected_version (glyphs.last ().data.version_id);- return glyphs.last ().data;+ if (glyphs.size > 0) {+ gl = glyphs.get (glyphs.size - 1);+ set_selected_version (gl.version_id);+ return gl; } return new Glyph ("");@@ -105,19 +106,19 @@ public class VersionList : DropMenu { } private int get_last_id () {- return_val_if_fail (glyphs.length () > 0, 1);- return glyphs.last ().data.version_id;+ return_val_if_fail (glyphs.size > 0, 1);+ return glyphs.get (glyphs.size - 1).version_id; } private void set_selected_item (MenuAction ma) { int i = ma.index; Glyph current_glyph = MainWindow.get_current_glyph ();- unowned List<Glyph> g;+ Glyph g; - return_if_fail (0 <= i < glyphs.length ());- g = glyphs.nth (i);+ return_if_fail (0 <= i < glyphs.size);+ g = glyphs.get (i); - current_version_id = g.data.version_id;+ current_version_id = g.version_id; return_if_fail (ma.parent != null); @@ -126,12 +127,8 @@ public class VersionList : DropMenu { reload_all_open_glyphs (); - if (unlikely (is_null (g.data))) {- warning ("No data in glyph collection.");- } else {- g.data.set_allocation (current_glyph.allocation);- g.data.default_zoom ();- }+ g.set_allocation (current_glyph.allocation);+ g.default_zoom (); } /** Reload a glyph when a new version is selected. Updates the path@@ -190,10 +187,10 @@ public class VersionList : DropMenu { int v; v = new_version.version_id;- glyphs.append (new_version);+ glyphs.add (new_version); ma = add_item (t_("Version") + @" $v");- ma.index = (int) glyphs.length () - 1;+ ma.index = (int) glyphs.size - 1; ma.action = (self) => { Font font = BirdFont.get_current_font ();