Re: Do TreeIters persist after foreach search ?




Dave Howorth wrote:
I think there must be something I'm overlooking about iters. I have code
which for testing I've simplified to the following:

    my $match_iter;
    $tree_store->foreach(
        sub {
            my ($tree_store, $path, $iter) = @_;

            my $level = $tree_store->get($iter, LEVEL);
            warn "  tree_store=$tree_store, iter=$iter;\n";

            $match_iter = $iter;
            return TRUE;
        });

    warn "search $tree_store => $match_iter;\n";
    my $level = $tree_store->get($match_iter, LEVEL);


The iters are intended to be used on the stack in C, so for a foreach(), they
would be destroyed before returning from the foreach() call.

Instead, either store the $path or, better yet, the data from the row that you
found, like this:

    my $row_data;
    $tree_store->foreach(
        sub {
            my ($tree_store, $path, $iter) = @_;
            my $this_row = [ $tree_store->get ($iter) ];
            if (this_row_is_the_one_i_want ($this_row)) {
                $row_data = $this_row;
                return TRUE;
            }
            return FALSE;
        });
    if (defined $row_data) {
        ...
    }

Alternatively, you could just call the i-found-it-action in the foreach
callback when you find the row you want, and avoid the issue.




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