驗證:多次呼叫WSAStartup會發生什麼?
阿新 • • 發佈:2018-12-14
有時候,總想驗證一些奇怪的想法,乾脆就寫下來,以備後查。
想法:多次呼叫WSAStartup會發生什麼?
void TestCase_WSAStartupAndCleanup() { WORD versionRequested1_1, versionRequested2_2; WSADATA wsaData1_1, wsaData2_2; int err; versionRequested1_1 = MAKEWORD(1, 1); versionRequested2_2 = MAKEWORD(2, 2); err = WSAStartup(versionRequested1_1, &wsaData1_1 ); if ( err != 0 ) { printf_s("wsa startup failed. [%u] \n", WSAGetLastError()); return; } printf_s("wsa startup success. [%u.%u] \n", LOBYTE(wsaData1_1.wVersion), HIBYTE(wsaData1_1.wVersion)); // 模擬中間其他函式又初始化網路的情況 { err = WSAStartup(versionRequested2_2, &wsaData2_2 ); if ( err != 0 ) { printf_s("wsa startup failed. [%u] \n", WSAGetLastError()); return; } printf_s("wsa startup success. [%u.%u] \n", LOBYTE(wsaData2_2.wVersion), HIBYTE(wsaData2_2.wVersion)); // do something ... if ( LOBYTE( wsaData2_2.wVersion ) != 2 || HIBYTE( wsaData2_2.wVersion ) != 2 ) { WSACleanup(); return; } } if ( LOBYTE( wsaData1_1.wVersion ) != 1 || HIBYTE( wsaData1_1.wVersion ) != 1 ) { WSACleanup(); return; } }
執行結果:
Visual Leak Detector Version 2.5.1 installed.
wsa startup success. [1.1]
wsa startup success. [2.2]
No memory leaks detected.