c++ - Turning on g++ optimization causes segfault - I don't get it -


i've been working on program, , decided turn on optimizations using g++ -o3. suddenly, program started segfaulting. i've hunted problematic code down, , minimized program still segfaults (only when using level 3 optimizations). hoping take quick peek @ code (i tried minimizing as possible):

// src/main.cpp #include "rt/lights/point.hpp" int main(int argc, char **argv) {     rt::light *light = new rt::light::point(alg::vector(.0f, 5.0f, 5.0f), rt::color(1.0f), .5f);     return 0; }   // include/rt/lights/point.hpp #ifndef rt_light_point_hpp_ #define rt_light_point_hpp_  #include "rt/accelerator.hpp" #include "rt/color.hpp" #include "rt/intersection.hpp" #include "rt/light.hpp" // abstract  namespace rt { namespace light {  class point : public light {   public:     point(alg::vector pos, color color, float intensity) : light(intensity * color), pos(pos) {}      color get_contrib(const intersection&, const accelerator&, const alg::vector& toviewer) const;    private:     alg::vector pos; };  } // namespace light } // namespace rt  #endif   // include/rt/light.hpp #ifndef rt_light_hpp_ #define rt_light_hpp_  #include "algebra/vector.hpp" #include "rt/color.hpp"  namespace rt {  class intersection; class accelerator;  class light {   public:     light(color intensity) : intensity(intensity) {}      virtual color get_contrib(const intersection&, const accelerator&, const alg::vector& toviewer) const = 0;      color get_intensity() const {return intensity;}    protected:     color intensity; };  } // namespace rt  #endif 

i love insight on why code segfaults when using optimizations, , how stop doing so. thanks!

$ find src/ -name "*.cpp" | xargs g++ -i include/ -o3 $ ./a.out segmentation fault 

edit: request, constructors alg::vector

 struct vector {     float x, y, z;     vector() : x(.0f), y(.0f), z(.0f) {}     explicit vector(float f) : x(f), y(f), z(f) {}     vector(float x, float y, float z) : x(x), y(y), z(z) {}     // ... 

edit2: adding gdb output when compiling -g

 (gdb) file a.out reading symbols /home/rob/devel/gbug/a.out...done. (gdb) run starting program: /home/rob/devel/gbug/a.out   program received signal sigsegv, segmentation fault. rt::light::point::point (this=0x804b008, pos=..., color=..., intensity=0.5)     @ src/rt/lights/point.cpp:13 13  point::point(alg::vector pos, color color, float intensity) : light(intensity * color), pos(pos) (gdb) bt #0  rt::light::point::point (this=0x804b008, pos=..., color=..., intensity=0.5)     @ src/rt/lights/point.cpp:13 #1  0x08048898 in main (argc=1, argv=0xbffff3e4) @ src/main.cpp:5 

edit3: sources rt::color.

 // include/rt/color.hpp #ifndef rt_color_hpp_ #define rt_color_hpp_  #include "algebra/vector.hpp"  namespace rt {  /*******************************************************************************  * class definition  */  struct color {     float r, g, b;     color() : r(.0f), g(.0f), b(.0f) {}     explicit color(float f) : r(f), g(f), b(f) {}     color(float r, float g, float b) : r(r), g(g), b(b) {}      color& operator+= (const color&);     color& operator*= (const color&);     color& operator*= (float); };  /*******************************************************************************  * member operators  */  inline color& color::operator+= (const color& other) {     r += other.r;     g += other.g;     b += other.b;     return *this; }  inline color& color::operator*= (const color& other) {     r *= other.r;     g *= other.g;     b *= other.b;     return *this; }  inline color& color::operator*= (float f) {     r *= f;     g *= f;     b *= f; }  /*******************************************************************************  * additional operators  */  inline color operator+ (color lhs, const color& rhs) {     return lhs += rhs; }  inline color operator* (color lhs, const color& rhs) {     return lhs *= rhs; }  inline color operator* (color c, float f) {     return c *= f; }  inline color operator* (float f, color c) {     return c *= f; }  } // namespace rt  #endif 

when calculating intensity * color, indirectly operator called:

inline color& color::operator*= (float f) {     r *= f;     g *= f;     b *= f; } 

it claims return reference color, doesn't. should return reference *this, other operators do:

return *this; 

Comments

Popular posts from this blog

java - SNMP4J General Variable Binding Error -

windows - Python Service Installation - "Could not find PythonClass entry" -

Determine if a XmlNode is empty or null in C#? -