Is this unsafe usage of a braced initializer list?

谁说胖子不能爱 提交于 2019-12-06 13:32:12

Temporary initializer lists should last until the end of the statement they are constructed, and they lifetime extend the array they wrap.

While make_shared<?>({1,2,3}) is not expected to work (see the imperfections of perfect forwarding), the rest of your code should. If the change you describe actually causes the code to start/stop working, you have some memory corruption.

If this is deterministic with high probability, the problem is likely that someone has a dangling reference to the an object on the stack, and is modifying it or reading it. When you change how you construct the initializer list, the layout of objects on the stack change, and different behaviour happens (some value is non-zero or zero and a branch goes a different way, or the thing being written to doesn't matter in one case and does in another).

If it is a write-based memory corruption, putting a memory breakpoint and tracking all access to that area of the stack (as tedious as it can be) can sometimes spot the location where the danging pointer is being followed.

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!