[Lemon-user] bug in iteration over nodes of FilterNodes?
Andrew Cunningham
lemon at a-cunningham.com
Thu Aug 11 19:57:53 CEST 2011
FilterNodes<SmartGraph>* foo(SmartGraph& g)
{
SmartGraph::NodeMap<bool> filter(g, true);
return new FilterNodes<SmartGraph>(g, filter);
}
Once the function above returns the object "filter" will have been destroyed
as it goes out-of-scope, so it is not surprising you get segmentation
errors.
You should probably have something like
SmartGraph::NodeMap<bool> *filter=new SmartGraph::NodeMap<bool> (g, true);
return new FilterNodes<SmartGraph>(g, *filter);
But of course you are now responsible for deleting the filter at some point.
On Thu, Aug 11, 2011 at 10:20 AM, Peter Ka <peter_ka at ymail.com> wrote:
> Dear all,
> I am quite new to the lemon community.
>
> I've been trying to create a NodeFilter adapter to filter some of the
> nodes, and then iterate through the remaining nodes. I've come across a bug
> which I simply can't understand and would appreciate your help. I use lemon
> 1.2.2 (stable).
>
> When running the following code:
>
> SmartGraph g;
> for(int i = 0; i < 5; i++) { g.addNode(); }
> SmartGraph::NodeMap<bool> filter(g, true);
> FilterNodes<SmartGraph> *filtered_g = new FilterNodes<SmartGraph>(g,
> filter);
> for(FilterNodes<SmartGraph>::NodeIt n(*filtered_g); n != INVALID; ++n)
> cout << "I am in node " << g.id(n) << endl;
>
> I get the expected result. HOWEVER, when I replace the line that defines
> filtered_g with:
> FilterNodes<SmartGraph> *filtered_g = foo(g);
>
> with:
> FilterNodes<SmartGraph>* foo(SmartGraph& g)
> {
> SmartGraph::NodeMap<bool> filter(g, true);
> return new FilterNodes<SmartGraph>(g, filter);
> }
>
> I get unexpected results (sometimes segmentation errors, sometimes the
> iterator runs only on a subset of the vertices - depends on the input graph
> I generate).
> Can someone please explain to me what's going on here? Shouldn't both
> pieces of code be completely equivalent?
>
> Thanks much,
> Pete
>
> _______________________________________________
> Lemon-user mailing list
> Lemon-user at lemon.cs.elte.hu
> http://lemon.cs.elte.hu/mailman/listinfo/lemon-user
>
>
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://lemon.cs.elte.hu/pipermail/lemon-user/attachments/20110811/ac03ca88/attachment.html>
More information about the Lemon-user
mailing list