last day (15 days later) » 

11:10 PM
9
Q: Possible MSVC compiler bug

Dmitry TeslenkoGiven shared_ptr variable declared in condition clause of for loop and for loop body contains if/continue statement, microsoft compiler (as of version 2015) generates extra destructor call (two total) per loop iteration. This causes destruction of Item objects that should be out of reach of Holde...

 
can you explain what you expected instead and why you think the behavior you see points at a compiler bug?
 
@largest_prime_is_463035818 Shouldn't destruction of all items happen after loop? (Though, there are no "visible side-effects" for Item(1) and Item(3) after end of loop. Hence, I'm a bit uncertain whether this just is a kind of clever and valid optimization nor have I an idea for what it could be good for.)
 
@Scheff I am wondering the same, but I want to be sure why OP concludes a compiler bug. For now they only showed what happens and is not expected, but nothing is really obvious.
 
I just tested it in VS2019 and it works as expected postimg.cc/S2ssm0zX
 
@anastaciu You compiled in Debug mode. This could change things. Please, try in Release... (In Debug mode, there should be no "clever optimization" usually.)
 
11:10 PM
fwiw, clang does the same: godbolt.org/z/8rMW8r while with gcc output is the expected godbolt.org/z/e9W5eb
@anastaciu here you can see it online godbolt.org/z/vss485
 
@largest_prime_is_463035818, the fact of the matter is, atfter the iteration, the objects are no longer needed, so I wouldn't call it a bug, in fact I think it's arguably better.
 
@anastaciu but destructors should only be called when the vector goes out of scope, not before. If the destructor had no side effects I would agree (though then we could not observe it)
@anastaciu it looks like shared pointers deletes when the ref count goes back to 1 (and that last ref is known to be not used anymore).
 
@largest_prime_is_463035818, yes, it's puzzling, though clang has proven to be a very smart compiler, it's doing its magic again ;)
 
@largest_prime_is_463035818 Good catch. I completely overlooked that a print output is a visible side-effect... :-)
 
@anastaciu hard to argue in favor of gcc when the others dont agree ;). As it seems like more digging into details is need to understand why clang/msvc are right (or not) I added the langauge-lawyer tag
 
11:10 PM
A slight modification shows that this isn't just a smart optimization. The same thing happens even if the items are used again after the loop; leading to erroneous results. Either this is a bug or it's some corner-case of the language I'm not aware of.
 
@largest_prime_is_463035818 when the ref count goes back to 1 (and that last ref is known to be not used anymore) yes it does look like it.
@MilesBudnek, well spotted, the fact of the matter is, if the OP is using MSVC, it shouldn't do it, we need to know more details about the compilation/test process, but you're right, now it seems more like a bug again. clang is now being too smart for its own good.
@Scheff, it's the same both in release and debug, it would be nice to know the compilation details and/or optimizations.
 
It's MSVC 2015. Both debug and release build gives same result. I'm running 64bit Win10.
 
@DmitryTeslenko can you tell us what version of MSVC are you using, and what optimizations/options are you enabling?
 
@anastaciu updated post with compiler id and options. Also added reference to "optimized" items to show that's not an optimization.
 
Perhaps someone like @NicolBolas can know what is the reason for this behavior.
 
11:10 PM
You may wish to report a minimized case of this as a bug in the MSVC bug tracker.
 
MSVC 2019 doesn't have this behaviour, with neither debug/release/32bit/64bit
 

last day (15 days later) »