[Vala] Getting corrupted results when returning array of strings



Good morning (where applicable),

I'm trying to return an array of strings (collected from a linked list)
but keep getting corrupted results. I assume it's an ownership issue but
I'm not sure where to begin asking questions, so I'm adding the relevant
parts of my buggy code.

Scope is supposed to hold "variables" (strings) and "arrays of
variables" and provide capability to try and fetch these from parent
scopes if the variable is not defined in the current scope. It doesn't
even have to do that in the error case below.
Dumping "result" before return is reached yields corrupted strings.

public class Scope: Object {

    protected weak Scope    parent;
    protected Variable      local;
(...)
    public string?[]? get_array(string uri) {

        string?[]? result = this.local.get_array(uri);

        if (result == null && this.parent != null) { //! result != null here

            result = this.parent.get_array(uri);
        }

        //! at this point result values appear to be invalid

        return result;
    }
(...)
}

The relevant parts of Variable follow. Dumping nodes.values() inside
get_array() yields to correct results.

public class Variable: TreeNode<string?, string?> {
(...)
    public string?[]? get_array(string uri) {

        string[] parts = uri.split(".");

        var node = this.find(parts);

        if (node != null) {
            return node.values();
        } else {
            return null;
        }

    }
(...)
}

Variable is derived from the generic TreeNode class:

class TreeNode<G, H>: Object {

    protected weak TreeNode<G, H> parent = null;
    protected LinkedList<TreeNode<G, H>> children = null;
    protected G key = null;
    protected H value = null;
(...)
    public virtual H[] values(bool include_leafs = true, bool
include_branches = false) {

        H[] result = { };

        if (this.children != null) {

            foreach (var node in this.children) {

                if ((include_leafs && node.is_leaf()) ||
(include_branches && node.is_branch())) {
                    result += node.value;
                }
            }

        }

        return result;
    }
(...)
}

Any help would be appreciated,
  Dennis



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