Boost logo

Boost Users :

Subject: Re: [Boost-users] 1.36.0 boost/random/uniform_01 Infinite[?] Loop
From: Steven Watanabe (watanabesj_at_[hidden])
Date: 2008-10-16 19:39:09


Dave Steenburgh wrote:
> > Or better, a minimal example to try?
> I'd like to think I know what the problem is now, but I'm still a
> little mystified, and I'm not sure how best to correct it. What I
> think I want is a correction to or replacement for my wrapper class,
> but I definitely want a second opinion. (Maybe a third and a fourth,
> too...)
> I've got a minimal-ish program that exhibits strange behavior, but it
> doesn't get stuck. I suspect the same phenomenon is at work here,
> though, since it still only appears when compiling without
> optimization. The source is attached, and here are some of my
> observations: If I use ng instead of vg, there is no problem. (Try
> it yourself; just move the comment slashes.) If I omit the call to
> problemFunction, everything works. If I decrease the number of
> arguments problemFunction requires, everything works. Based on that
> info, my guess is that something the random generator needs is created
> on the stack, and later overwritten. However, I think if that were
> really the problem, it would still be a problem on another platform,
> or with optimizations enabled.

The error is in this class:

class normalGenerator : public randomGenerator
  typedef boost::normal_distribution<> normalDistribution;
  boost::variate_generator<baseGenerator&, normalDistribution> _gen;
  explicit normalGenerator(baseGenerator gen = baseGenerator(time(NULL)))
    : _gen(gen, normalDistribution()) { }
  inline virtual double operator()() { return _gen(); }

a) _gen stores a reference to a baseGenerator.
b) The constructor takes a baseGenerator by value, which is then
destroyed when
  the constructor returns.

In Christ,
Steven Watanabe

Boost-users list run by williamkempf at, kalb at, bjorn.karlsson at, gregod at, wekempf at