[Lemon-user] bug in iteration over nodes of FilterNodes?

Andrew Cunningham lemon at a-cunningham.com
Fri Aug 12 17:37:38 CEST 2011


Hi Peter,
That was my best 'guess' without actually trying it...did it fix the seg
fault?

Andrew

On Fri, Aug 12, 2011 at 5:36 AM, Peter Ka <peter_ka at ymail.com> wrote:

> Thank you for the response
> I did not realize that the filter object (given to the constructor of
> FilterNodes) should remain valid throughout the lifetime of the FilterNodes
> object
>
> ------------------------------
> *From:* Andrew Cunningham <lemon at a-cunningham.com>
> *To:* Peter Ka <peter_ka at ymail.com>
> *Cc:* "lemon-user at lemon.cs.elte.hu" <lemon-user at lemon.cs.elte.hu>
> *Sent:* Thursday, August 11, 2011 8:57 PM
> *Subject:* Re: [Lemon-user] bug in iteration over nodes of FilterNodes?
>
>
> 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/20110812/e0635222/attachment.html>


More information about the Lemon-user mailing list