Advertisement

Runtime crash with non-ref copy constructor

Started by June 03, 2020 12:15 PM
7 comments, last by WitchLord 4 years, 5 months ago

Hi, one of my users encountered a crash and I managed to size it down into a very small reproducable script:

class Foo
{
	Foo(){}
	Foo(Foo f){}
}
void Test(Foo f){}

void Main()
{
	auto f = Foo();
	Test(f);
}

Seems like it's due to the constructor passing by value. If I do “const Foo &in f” it doesn't crash.

I mean, this crashing makes a lot of sense if you think about it ? but perhaps it should throw a compiler error instead?

There are some issues with this. A few things are missing here, so it won't compile unless perhaps you're using a very forgiving compiler.

Having a copy-ctor taking the original object by value would invoke the copy-ctor recursively. Please don't do that.
A copy constructor takes a const reference.

When the little issues have been ironed out, you're left with a compiler error, as you would expect.
Gcc does this, and I would think other compilers would also do it. Have you tried a proper, clean build, and are both your header and implementation files saying the same thing?

Advertisement

You've definitely got a very lenient compiler, there. Visual C++ throws a “illegal copy constructor: first parameter must not be a 'Foo'” compiler error too.

Direct3D has need of instancing, but we do not. We have plenty of glVertexAttrib calls.

You guys are aware that this is posted in AngelCode-forum, so its probably not C++ but AngelScript, right? :D

Juliean said:

You guys are aware that this is posted in AngelCode-forum, so its probably not C++ but AngelScript, right? :D

I wasn't. ?

I wish I could have caught that upon compile…

Haha thanks Juliean. This is indeed Angelscript. This seems to happen sometimes when I post Angelscript bugs that could be interpreted also as C++ questions. ? Sorry for any confusion!

Advertisement

It is a common mistake due to AngelScript syntax beeing so similar to C++ ?

Thanks for reporting this Miss. It should indeed give a compiler error, or at least not identify this constructor as the copy constructor.

I'll have it fixed as soon as possible.

AngelCode.com - game development and more - Reference DB - game developer references
AngelScript - free scripting library - BMFont - free bitmap font generator - Tower - free puzzle game

I've fixed this in revision 2648.

The scenario won't give a compiler error, but it won't use the constructor as a copy constructor. Instead it will use the default constructor and then do a copy of members as is the default behaviour in AngelScript, when no copy constructor is available.

AngelCode.com - game development and more - Reference DB - game developer references
AngelScript - free scripting library - BMFont - free bitmap font generator - Tower - free puzzle game

This topic is closed to new replies.

Advertisement