Ticket #6006: fd_leak_test.cpp

File fd_leak_test.cpp, 1.1 KB (added by yafei.zhang@…, 11 years ago)
Line 
1/*
2* On Linux
3* g++ -Wall -g fd_leak_test.cpp -lboost_system -lboost_thread -lpthread
4* This program may cause fd leaks
5*
6* */
7#include <stdlib.h>
8#include <stdio.h>
9#include <boost/asio.hpp>
10#include <boost/thread.hpp>
11
12static void test()
13{
14#define N 1024
15 boost::asio::io_service * ios[N];
16 boost::asio::ip::tcp::socket * socks[N];
17
18 for (size_t i=0; i<N; i++)
19 {
20 try
21 {
22 ios[i] = new boost::asio::io_service();
23 }
24 catch (std::exception& e)
25 {
26 ios[i] = NULL;
27 continue;
28 }
29
30 try
31 {
32 socks[i] = new boost::asio::ip::tcp::socket(*ios[i]);
33 }
34 catch (std::exception& e)
35 {
36 socks[i] = NULL;
37 }
38 }
39
40
41 for (size_t i=0; i<N; i++)
42 {
43 delete socks[i];
44 delete ios[i];
45 }
46}
47
48int main(int argc, char ** argv)
49{
50 char cmd[128];
51
52 snprintf(cmd, sizeof(cmd), "ls -al /proc/self/fd");
53
54 printf("press any key to begin\n");
55 getchar();
56 system(cmd);
57
58
59 boost::thread_group tg;
60 for (int j=0; j<20; j++)
61 {
62 tg.create_thread(test);
63 }
64 tg.join_all();
65
66 printf("press any key to continue\n");
67 getchar();
68 system(cmd);
69 return 0;
70}