Destructor in vector called too often

Started by
26 comments, last by Juliean 9 years, 8 months ago

I just tested your code with GCC 4.7.2 (Windows)


AttributeDeclartion() called.
AttributeDeclartion() called.
AttributeDeclartion(const AttributeDeclaration& decl) called with this: 8595680
and decl: 2686698.
AttributeDeclartion(const AttributeDeclaration& decl) called with this: 8595681
and decl: 2686699.
~AttributeDeclartion() called with this: 2686699.
~AttributeDeclartion() called with this: 2686698.
Initializing vector done.
~AttributeDeclartion() called with this: 8595680.
~AttributeDeclartion() called with this: 8595681.
Press <RETURN> to close this window...

So no duplicate destruction of the same object in there.

So perhaps this is a bug with initializer lists in VC, I dunno. Are you seeing the destructor message with the same value for "this" more than once in your output?

Advertisement


So no duplicate destruction of the same object in there.

So perhaps this is a bug with initializer lists in VC, I dunno. Are you seeing the destructor message with the same value for "this" more than once in your output?

Huh, thats weird. For me it actually completly looks like:


AttributeDeclartion() called.
AttributeDeclartion() called.
AttributeDeclartion(const AttributeDeclaration& decl) called with this: 8595680
and decl: 2686698.
AttributeDeclartion(const AttributeDeclaration& decl) called with this: 8595681
and decl: 2686699.
~AttributeDeclartion() called with this: 2686699.
~AttributeDeclartion() called with this: 2686698.
~AttributeDeclartion() called with this: 2686698. // thats the "faulty" line
Initializing vector done.
~AttributeDeclartion() called with this: 8595680.
~AttributeDeclartion() called with this: 8595681.
Press <RETURN> to close this window...

I actually found a ticket that states something about a memory leak in that initializer list in VC that should have been fixed some time ago. So maybe some over-enthusiastic programmer fixed the leak but also caused this double deletion. Don't know, based on that it doesn't happens in GCC I might as well post it to the VC-people, maybe someone there has greater insight to the actual problem.

Two things I notice about your example code (I don't have MSVC2013 on hand to test initializer lists at the moment):
1. By adding the string to the constructor you disabled the default constructor. Does the bug go away if you have a parameter of a different type? (Try with a POD like int, and a non-POD like, I dunno, std::shared_ptr<int>)
2. You don't have an assignment operator, so the compiler probably generated one for you. Add it in with matching debug output to see if an extra object is being generated and assigned to to match your destructor call. Or add it in as =delete and see if the compiler errors.

1. Yes! As I (at least I though I) mentioned it only happens with the string-type argument. Int, float bool, Class* all work. I didn't test it with other classes references which are implicitely created, might be worth a check though.

2. As I already stated multiple times, none of those operators are called in the code in question here. It would be a legit query, but doesn't mean anything here really due to this fact ;) Also, in my minimal test case, the class doesn't have any state at all, so there would be no point of a copy/move-operator in the first place... neigther is there for a copy-ctor, its just there to assist in debug-ouput. ;)

Not sure. Either:

a) There is a bug in VC

b) This program is relying on some undefined behaviour I'm not spotting that just happens to work with GCC

I suspect the former since its such a simple program fragment.


Not sure. Either:



a) There is a bug in VC

b) This program is relying on some undefined behaviour I'm not spotting that just happens to work with GCC



I suspect the former since its such a simple program fragment.

Probably the former, since every working online-compiler I could find, most of them based on GCC or similar, worked as intented. I'll report the appearent bug to Microsoft and post here again with the answer. Would still be nice though if anybody with visual studio 2013 or the 2012 November compiler CTP could also test the code and confirm that this is happening for him too.


Would still be nice though if anybody with visual studio 2013 or the 2012 November compiler CTP could also test the code and confirm that this is happening for him too.

I'm sorry to say that I can't reproduce the problem with Visual Studio 2013 Update 2.

current project: Roa


I'm sorry to say that I can't reproduce the problem with Visual Studio 2013 Update 2.

Dafuq? Ok, now I'm totally cursious. I'll double-check to see if I have the latest updates. I'll also see if I get to test it on another PC. Thanks for testing!

EDIT: So on this PC with a fresh installation of Visual Studio 2013 Express (Ultimate at my regular PC) from the online-installer about 3 weeks ago, it also happens. Don't really see if Update 2 is installed, but I can install update 3, which I will do to see if this is really related to this.

Just a thought, but try changing the optimization settings. At work we've found some bugs in GCC (reported) using -O3 and had to manually add the flags -O3 adds minus the one that was causing the issue.


Just a thought, but try changing the optimization settings. At work we've found some bugs in GCC (reported) using -O3 and had to manually add the flags -O3 adds minus the one that was causing the issue.

You where right. Totally doesn't happen in release mode. So now all I have to do is find the relevant setting and report it to microsoft. I quess I'll just #ifdef the nullptr-set for debug mode until they fix it, but its really something that shouldn't be happending.

EDIT: Ok, now it isn't happening at all anymore. Appears he just didn't compile after starting VS with the update 3, quess it was fixed in one of the updates. Going to confirm this on my home-PC, but I'm optimistic (even though I swear I was on the most recent update, weird). Thanks all for the help though!

This topic is closed to new replies.

Advertisement