heap corruption when using multithreaded std::cout in TEST_CASE
- Dominant language
- C++
- Stars
- 21.5k
- Forks
- 3.5k
- Avg merge
- 3d 16h
- Merged PRs (30d)
- 2
Description
Heap corruption occurs when using std::cout from multiple threads in TEST_CASE
**Reproduction steps**
```c++
#include
#include
TEST_CASE("cout test") {
auto thread1 = std::thread([] {
for (size_t i = 0; i < 100; ++i) {
std::cout << "thread 1 iteration " << i << std::endl;
}
});
auto thread2 = std::thread([] {
for (size_t i = 0; i < 100; ++i) {
std::cout << "thread 2 iteration " << i << std::endl;
}
});
thread1.join();
thread2.join();
}
```
running under Debug causes Assertion:
```
Debug Assertion Failed!
Program: ...make-build-debug-visual-studio-2022\examples\010-TestCase.exe
File: minkernel\crts\ucrt\src\appcrt\heap\debug_heap.cpp
Line: 904
Expression: _CrtIsValidHeapPointer(block)
```
and call stack:
```
RtlRegisterSecureMemoryCacheCallback 0x00007ffbee50a313
memset 0x00007ffbee4ccd24
RtlValidateHeap 0x00007ffbee46e115
HeapValidate 0x00007ffbec20ebcb
_CrtIsValidHeapPointer 0x00007ffb77162fd1
_calloc_base 0x00007ffb77161385
_free_dbg 0x00007ffb771649c5
operator delete(void *) 0x00007ff6ad894258
operator delete(void *,unsigned long long) 0x00007ff6ad892ff8
std::_Deallocate<16,0>(void *,unsigned long long) xmemory:255
std::allocator::deallocate(char *const,const unsigned long long) xmemory:827
std::basic_stringbuf,std::allocator >::overflow(int) sstream:278
std::basic_streambuf >::xsputn(char const *,__int64) 0x00007ffb77a94159
std::basic_streambuf >::sputn(char const *,__int64) 0x00007ffb77b073e8
std::operator<< >(std::basic_ostream > &,const char *) ostream:780
::operator()() 010-TestCase.cpp:15
std::invoke< >( &&) type_traits:1480
std::thread::_Invoke >,0>(void *) thread:55
_register_onexit_function 0x00007ffb77184c7c
BaseThreadInitThunk 0x00007ffbecb47034
RtlUserThreadStart 0x00007ffbee462651
```
**Platform information:**
- OS: **Windows 10 Pro 20H2**
- Compiler+version: **Visual Studio 2022**
- Catch version: **v3.0.1**
Contributor guide
Research direction
Start with the reproduction in examples/010-TestCase.cpp and run it under Windows 10 with Visual Studio 2022 in Debug mode. Trace how the TEST_CASE handles concurrent std::cout output, then verify the completed behavior by rerunning the two-thread example without heap-corruption assertions.
Written by the indexing model from the issue text.
Assessment
- Tech stack
- cpp
- Domain
- testing-qa
- Issue type
- Bug
- Difficulty
- 4/5
- Estimated time
- 3-5 days
- Activity status
- Stale
- Clarity
- Mostly clear
- Newbie friendliness
- 35/100