C++控制台多线程动画程序出现0xC0000005错误求助
Hey there! That 0x0000005 error is an Access Violation—it means your program’s trying to read or write memory it doesn’t own or has no permission to touch. Since it works on the first run but fails later, here are the most likely culprits to check, tailored to your Borland C++ setup:
Common Causes & Fixes
1. Unsynced Shared Resources in Multithreading
First off, multithreading’s biggest pitfall is unprotected shared data. If multiple threads are modifying the same global variable, writing to the console (like cout or printf), or accessing a shared buffer without locking, you’ll get race conditions. On the first run, memory might line up "luckily," but subsequent runs will hit conflicts that trigger access violations.
Fix: Use Win32 Critical Sections to guard shared resources:
// Declare a critical section globally or in a shared scope CRITICAL_SECTION consoleLock; // Initialize it in your main thread before starting other threads InitializeCriticalSection(&consoleLock); // In each thread, wrap shared operations like this: EnterCriticalSection(&consoleLock); cout << "Thread-safe console output" << endl; // Or modify shared global variables here LeaveCriticalSection(&consoleLock); // Don't forget to clean up when done DeleteCriticalSection(&consoleLock);
2. DOS/Non-Thread-Safe Functions
You’re including dos.h—those are legacy DOS-era functions, which are not thread-safe. Functions from dos.h (like low-level hardware calls or old delay routines) were designed for single-tasking systems. When used in multithreaded Win32 apps, they can corrupt system state or overwrite memory used by other threads.
Fix: Replace DOS functions with Win32 equivalents. For example, instead of a custom delay using DOS calls, use Sleep(mseconds) from windows.h—it’s thread-safe and designed for Win32.
3. Borland Compiler Thread API Quirks
Borland C++ 5.x (especially the older 5.02) has strict requirements for Win32 thread functions. If your thread function’s signature isn’t exactly right, the stack will get corrupted, leading to access violations.
Make sure your thread function matches this exact signature:
DWORD WINAPI YourThreadFunction(LPVOID lpParam) { // Your thread logic here return 0; }
Also, when creating threads with CreateThread, always store the thread handle and close it when you’re done (with CloseHandle) to avoid resource leaks that can cause unexpected memory issues over time.
4. Stack Overflow in Threads
Each thread in Win32 has its own stack, and Borland’s default stack size might be small for your use case. If a thread has large local variables or deep recursion, the first run might not hit the stack limit, but later runs (as memory fragments) will overflow.
Fix: When creating a thread, explicitly set a larger stack size:
HANDLE hThread = CreateThread( NULL, 1024 * 1024, // 1MB stack size (adjust as needed) YourThreadFunction, NULL, 0, NULL );
You can also adjust the default stack size in Code::Blocks’ compiler settings for Borland C++.
5. Uninitialized Pointers or Dangling Memory
If you’re passing pointers between threads, or using uninitialized pointers, the first run might coincidentally point to valid memory, but subsequent runs will hit garbage or freed memory. Double-check:
- All pointers are initialized before use
- Memory passed to threads isn’t freed before the thread finishes using it
- Global arrays aren’t being accessed beyond their bounds
Migration Tips for Borland C++ 5.02
Since you’re moving to 5.02 (even older than 5.5), keep these in mind:
- 5.02 has poorer support for early C++ standards—avoid modern features (like templates with complex logic) that might not compile or have buggy implementations.
- Stick to
iostream.hinstead of the standardiostream(5.02’s support for the latter is spotty), but remember oldiostreamimplementations aren’t thread-safe—use the critical section trick for console output. - Enable maximum compiler warnings (look for Borland’s
-Wflags in Code::Blocks) to catch uninitialized variables or implicit conversions that could lead to memory issues.
Next Steps If You’re Still Stuck
If you can narrow down the problem:
- Test your code in single-threaded mode first—if it works, the issue is definitely multithreading-related.
- Comment out parts of your thread code one by one to find exactly which line triggers the access violation.
- Use Borland’s debugger to break on the error and inspect the call stack—this will show you exactly which thread and function is causing the memory access issue.
内容的提问来源于stack exchange,提问作者Zombie Chibi XD

