开发者

Strong Pointer causing a heap corruption on application closing when std::vector is present

开发者 https://www.devze.com 2023-03-18 18:38 出处:网络
I am getting a strange heap corruption error during the application close where if a \"std::vector\" is present in my code, AND I am deleted my \"ref_count\" variable. If I don\'t have an std::vector,

I am getting a strange heap corruption error during the application close where if a "std::vector" is present in my code, AND I am deleted my "ref_count" variable. If I don't have an std::vector, there is no crash. If there is a std::vector and I don't delete the ref_count (causing a memory leak), the开发者_开发技巧n there is a heap crash. I am made my test case as small as possible, but unfortunately it is too big for here so it is located at http://codepad.org/USIPjPHJ . Does anyone have any suggestions on what could be the problem?

I will get a crash with the heap exception of this in my output windows.


HEAP[CrashTest.exe]: HEAP: Free Heap block 6d4c30 modified at 6d4c58 after it was freed Windows has triggered a breakpoint in CrashTest.exe.

This may be due to a corruption of the heap, which indicates a bug in CrashTest.exe or any of the DLLs it has loaded.

This may also be due to the user pressing F12 while CrashTest.exe has focus.

The output window may have more diagnostic information.


My IDE is Visual Studio 2010 SP1 (tried on non SP1 also) but that should not be causing the issue.


This line:

strong_ref<int> reg1;

sets *ref_count to 0 (because ptr == NULL), then this line:

strong_ref<int> copy1(reg1);

increments *ref_count to 1.

On exit, the destructor is called twice (once for reg1, once for copy1). The first time, *ref_count is decremented to 0 and ref_count is deallocated, leaving a dangling reference for the second time.

0

精彩评论

暂无评论...
验证码 换一张
取 消

关注公众号