Remotion of exit (-1) leads to segfault BEFORE exit (-1)

Hello, I recently implemented new code into my program that made him end into a segfault. Started to print messages on the terminal to be able to locate the precise line. I located such line, it is:

1
2
3
4
5
cout << "TEST1" << endl;
            //final_color = diffuse_color * mat.ks + specular_color * mat.kd;
            final_color = diffuse_color * mat.ks + specular_color * mat.kd;

            cout << "TEST2" << endl;


I know it shall be this line because the "TEST2" is not being printed.

However if I add an "exit(-1)" soon after the "TEST2" cout, before anything else, the program stop crashing.

How is that possible? The fact that isn't crashing after the insertion of "exit (-1)" would only make sense if problematic line was another one AFTER the exit (-1) insertion.

Can it be related to multithreading? It is a multithreaded program, but the issue happens even if I send only of the threads to return reaching this point of the code.

No idea of what to do, so any help might prove useful.
use a debugger, run through valgrind.
¿why do you think that that line may cause a segfault? ¿are those variables `valarray' or something?

you probably invoked undefined behaviour before
ne55, I putted a cout << "final_color: " << final_color.red << ' ' << final_color.green << ' ' << final_color.blue << endl; between cout << "final_color: " << final_color.red << ' ' << final_color.green << ' ' << final_color.blue << endl; and cout << "TEST2" << endl;
with the intention of allowing gdb to print its values.

Now I am receiving the segfault even with a exit (-1) after cout << "TEST2" << endl;

What do you mean by
are those variables `valarray' or something?


If I change "final_color" to final_color = diffuse_color * mat.ks; it seems to run fine (at least no segfault

To change it to final_color = /*diffuse_color * mat.ks +*/ specular_color * mat.kd; also stops it from segfault.

So is this related to something I am doing with operator overloading (everything in the line are "Colors")
I have three operator overloading operations that works with two colors:

1
2
3
4
5
6
7
8
9
10
11
12
class Color {
   public:
      inline Color operator += (const Color& c) {
         //cout << "c: " << c.red << ' ' << c.green << ' ' << c.blue << endl;
         //cout << "this: " << this->red << ' ' << this->green << ' ' << this->blue << endl;
         this->red = this->red + c.red;
         this->green = this->green + c.green;
         this->blue = this->blue + c.blue;
         //return *this;
      }
      float red,green,blue;
};


1
2
3
4
5
6
7
8
9
10
inline Color& operator * (const Color& c, const Color &d) {
   Color color;
   //cout << "c :" << c.red << ' ' << c.green << ' ' << c.blue << endl;
   //cout << "d :" << d.red << ' ' << d.green << ' ' << d.blue << endl;
   color.red = c.red * d.red;
   color.green = c.green * d.green;
   color.blue = c.blue * d.blue;
   //cout << "color :" << color.red << ' ' << color.green << ' ' << color.blue << endl;
   return color;
}

1
2
3
4
5
6
7

[code]inline Color& operator + (const Color& c, const Color &d) {
   Color color;
   color.red = c.red + d.red;
   color.green = c.green + d.green;
   color.blue = c.blue + d.blue;
}


So I overload twice the '+' operator when it works with two parameters. Might be that that makes the compiler confused?

You should review functions in your favorite textbook.

Color Color::operator+= (const Color& c)
Returns nothing, despite claiming to return a Color; flowing off the end of a function results in a program whose behavior is undefined.

Color& Color::operator* (const Color& c, const Color &d)
Returns a dangling reference. Attempts to access the result yields a program whose behavior is undefined.

Color& Color::operator+ (const Color& c, const Color &d)
Returns nothing, despite claiming to return a Color. Flowing off the end of a function results in a program whose behavior is undefined.
Last edited on
Topic archived. No new replies allowed.