[AGENT++] Hi, all, why these code lead to memry leak?

cheng wan wancheng82 at gmail.com
Mon Mar 1 08:48:25 CET 2010


Snmpwalk test result,
1: tcpConnLocalPort.0.0.0.0.22.0.0.0.0.0 (integer) 22
2: tcpConnLocalPort.0.0.0.0.111.0.0.0.0.0 (integer) 111
3: tcpConnLocalPort.0.0.0.0.901.0.0.0.0.0 (integer) 901
4: tcpConnLocalPort.0.0.0.0.2049.0.0.0.0.0 (integer) 2049
5: tcpConnLocalPort.0.0.0.0.48477.0.0.0.0.0 (integer) 48477
6: tcpConnLocalPort.0.0.0.0.49871.0.0.0.0.0 (integer) 49871
7: tcpConnLocalPort.0.0.0.0.57097.0.0.0.0.0 (integer) 57097
8: tcpConnLocalPort.10.10.50.161.22.10.10.50.112.1788 (integer) 22
9: tcpConnLocalPort.10.10.50.161.22.10.10.50.112.2302 (integer) 22
10: tcpConnLocalPort.10.10.50.161.22.10.10.50.112.2632 (integer) 22
11: tcpConnLocalPort.10.10.50.161.22.10.10.50.112.2910 (integer) 22
12: tcpConnLocalPort.10.10.50.161.22.10.10.50.112.3168 (integer) 22
13: tcpConnLocalPort.10.10.50.161.22.10.10.50.112.3202 (integer) 22
14: tcpConnLocalPort.10.10.50.161.22.10.10.50.112.4276 (integer) 22
15: tcpConnLocalPort.10.10.50.161.22.10.10.50.112.4294 (integer) 22
16: tcpConnLocalPort.10.10.50.161.34331.0.0.0.0.0 (integer) 34331
17: tcpConnLocalPort.10.10.50.161.37293.0.0.0.0.0 (integer) 37293
18: tcpConnLocalPort.10.10.50.161.38027.10.10.50.161.52188 (integer) 38027
19: tcpConnLocalPort.10.10.50.161.38028.10.10.50.161.52188 (integer) 38028
20: tcpConnLocalPort.10.10.50.161.38034.10.10.50.161.52188 (integer) 38034
21: tcpConnLocalPort.10.10.50.161.38045.10.10.50.161.52188 (integer) 38045
22: tcpConnLocalPort.10.10.50.161.38052.10.10.50.161.52188 (integer) 38052
23: tcpConnLocalPort.10.10.50.161.38060.10.10.50.161.52188 (integer) 38060
24: tcpConnLocalPort.10.10.50.161.38065.10.10.50.161.52188 (integer) 38065
25: tcpConnLocalPort.10.10.50.161.38786.0.0.0.0.0 (integer) 38786
26: tcpConnLocalPort.10.10.50.161.38786.10.10.50.161.39270 (integer) 38786
27: tcpConnLocalPort.10.10.50.161.39270.10.10.50.161.38786 (integer) 39270
28: tcpConnLocalPort.10.10.50.161.39636.10.10.50.161.50430 (integer) 39636
29: tcpConnLocalPort.10.10.50.161.39639.10.10.50.161.50430 (integer) 39639
30: tcpConnLocalPort.10.10.50.161.40599.0.0.0.0.0 (integer) 40599
31: tcpConnLocalPort.10.10.50.161.40599.10.10.50.161.45822 (integer) 40599
32: tcpConnLocalPort.10.10.50.161.41288.0.0.0.0.0 (integer) 41288
33: tcpConnLocalPort.10.10.50.161.41288.10.10.50.161.58601 (integer) 41288
34: tcpConnLocalPort.10.10.50.161.41494.0.0.0.0.0 (integer) 41494
35: tcpConnLocalPort.10.10.50.161.41494.10.10.50.161.57009 (integer) 41494
36: tcpConnLocalPort.10.10.50.161.41494.10.10.50.161.57014 (integer) 41494
37: tcpConnLocalPort.10.10.50.161.42199.0.0.0.0.0 (integer) 42199
38: tcpConnLocalPort.10.10.50.161.42199.10.10.50.161.45490 (integer) 42199
39: tcpConnLocalPort.10.10.50.161.42916.0.0.0.0.0 (integer) 42916
40: tcpConnLocalPort.10.10.50.161.42916.10.10.50.161.47449 (integer) 42916
41: tcpConnLocalPort.10.10.50.161.43456.10.10.50.161.60689 (integer) 43456
42: tcpConnLocalPort.10.10.50.161.43459.10.10.50.161.60689 (integer) 43459
43: tcpConnLocalPort.10.10.50.161.43465.10.10.50.161.60689 (integer) 43465
44: tcpConnLocalPort.10.10.50.161.43476.10.10.50.161.60689 (integer) 43476
45: tcpConnLocalPort.10.10.50.161.43483.10.10.50.161.60689 (integer) 43483
46: tcpConnLocalPort.10.10.50.161.43491.10.10.50.161.60689 (integer) 43491
47: tcpConnLocalPort.10.10.50.161.44530.0.0.0.0.0 (integer) 44530
48: tcpConnLocalPort.10.10.50.161.44530.10.10.50.161.58882 (integer) 44530
49: tcpConnLocalPort.10.10.50.161.44530.10.10.50.161.58885 (integer) 44530
50: tcpConnLocalPort.10.10.50.161.44530.10.10.50.161.58898 (integer) 44530
51: tcpConnLocalPort.10.10.50.161.45490.10.10.50.161.42199 (integer) 45490
52: tcpConnLocalPort.10.10.50.161.45782.0.0.0.0.0 (integer) 45782
53: tcpConnLocalPort.10.10.50.161.45782.10.10.50.161.51185 (integer) 45782
54: tcpConnLocalPort.10.10.50.161.45822.10.10.50.161.40599 (integer) 45822
55: tcpConnLocalPort.10.10.50.161.45823.10.10.50.161.50465 (integer) 45823
56: tcpConnLocalPort.10.10.50.161.47017.0.0.0.0.0 (integer) 47017
57: tcpConnLocalPort.10.10.50.161.47017.10.10.50.161.56806 (integer) 47017
58: tcpConnLocalPort.10.10.50.161.47449.10.10.50.161.42916 (integer) 47449
59: tcpConnLocalPort.10.10.50.161.50430.0.0.0.0.0 (integer) 50430
60: tcpConnLocalPort.10.10.50.161.50430.10.10.50.161.39636 (integer) 50430
61: tcpConnLocalPort.10.10.50.161.50430.10.10.50.161.39639 (integer) 50430
62: tcpConnLocalPort.10.10.50.161.50465.0.0.0.0.0 (integer) 50465
63: tcpConnLocalPort.10.10.50.161.50465.10.10.50.161.45823 (integer) 50465
64: tcpConnLocalPort.10.10.50.161.51185.10.10.50.161.45782 (integer) 51185
65: tcpConnLocalPort.10.10.50.161.52188.0.0.0.0.0 (integer) 52188
66: tcpConnLocalPort.10.10.50.161.52188.10.10.50.161.38027 (integer) 52188
67: tcpConnLocalPort.10.10.50.161.52188.10.10.50.161.38028 (integer) 52188
68: tcpConnLocalPort.10.10.50.161.52188.10.10.50.161.38034 (integer) 52188
69: tcpConnLocalPort.10.10.50.161.52188.10.10.50.161.38045 (integer) 52188
70: tcpConnLocalPort.10.10.50.161.52188.10.10.50.161.38052 (integer) 52188
71: tcpConnLocalPort.10.10.50.161.52188.10.10.50.161.38060 (integer) 52188
72: tcpConnLocalPort.10.10.50.161.52188.10.10.50.161.38065 (integer) 52188
73: tcpConnLocalPort.10.10.50.161.52888.10.10.50.161.56089 (integer) 52888
74: tcpConnLocalPort.10.10.50.161.52891.10.10.50.161.56089 (integer) 52891
75: tcpConnLocalPort.10.10.50.161.53066.0.0.0.0.0 (integer) 53066
76: tcpConnLocalPort.10.10.50.161.56089.0.0.0.0.0 (integer) 56089
77: tcpConnLocalPort.10.10.50.161.56089.10.10.50.161.52888 (integer) 56089
78: tcpConnLocalPort.10.10.50.161.56089.10.10.50.161.52891 (integer) 56089
79: tcpConnLocalPort.10.10.50.161.56806.10.10.50.161.47017 (integer) 56806
80: tcpConnLocalPort.10.10.50.161.57009.10.10.50.161.41494 (integer) 57009
81: tcpConnLocalPort.10.10.50.161.57014.10.10.50.161.41494 (integer) 57014
82: tcpConnLocalPort.10.10.50.161.57726.0.0.0.0.0 (integer) 57726
83: tcpConnLocalPort.10.10.50.161.58601.10.10.50.161.41288 (integer) 58601
84: tcpConnLocalPort.10.10.50.161.58882.10.10.50.161.44530 (integer) 58882
85: tcpConnLocalPort.10.10.50.161.58885.10.10.50.161.44530 (integer) 58885
86: tcpConnLocalPort.10.10.50.161.58898.10.10.50.161.44530 (integer) 58898
87: tcpConnLocalPort.10.10.50.161.60689.0.0.0.0.0 (integer) 60689
88: tcpConnLocalPort.10.10.50.161.60689.10.10.50.161.43456 (integer) 60689
89: tcpConnLocalPort.10.10.50.161.60689.10.10.50.161.43459 (integer) 60689
90: tcpConnLocalPort.10.10.50.161.60689.10.10.50.161.43465 (integer) 60689
91: tcpConnLocalPort.10.10.50.161.60689.10.10.50.161.43476 (integer) 60689
92: tcpConnLocalPort.10.10.50.161.60689.10.10.50.161.43483 (integer) 60689
93: tcpConnLocalPort.10.10.50.161.60689.10.10.50.161.43491 (integer) 60689
94: tcpConnLocalPort.127.0.0.1.19999.0.0.0.0.0 (integer) 19999
95: tcpConnLocalPort.127.0.0.1.19999.127.0.0.1.53324 (integer) 19999
96: tcpConnLocalPort.127.0.0.1.19999.127.0.0.1.53325 (integer) 19999
97: tcpConnLocalPort.127.0.0.1.19999.127.0.0.1.53327 (integer) 19999
98: tcpConnLocalPort.127.0.0.1.19999.127.0.0.1.53328 (integer) 19999
99: tcpConnLocalPort.127.0.0.1.19999.127.0.0.1.53332 (integer) 19999
100: tcpConnLocalPort.127.0.0.1.19999.127.0.0.1.53333 (integer) 19999
101: tcpConnLocalPort.127.0.0.1.19999.127.0.0.1.53334 (integer) 19999
102: tcpConnLocalPort.127.0.0.1.19999.127.0.0.1.53339 (integer) 19999
103: tcpConnLocalPort.127.0.0.1.19999.127.0.0.1.53342 (integer) 19999
104: tcpConnLocalPort.127.0.0.1.19999.127.0.0.1.53343 (integer) 19999
105: tcpConnLocalPort.127.0.0.1.19999.127.0.0.1.53345 (integer) 19999
106: tcpConnLocalPort.127.0.0.1.19999.127.0.0.1.53351 (integer) 19999
107: tcpConnLocalPort.127.0.0.1.19999.127.0.0.1.53352 (integer) 19999
108: tcpConnLocalPort.127.0.0.1.19999.127.0.0.1.53358 (integer) 19999
109: tcpConnLocalPort.127.0.0.1.19999.127.0.0.1.53359 (integer) 19999
110: tcpConnLocalPort.127.0.0.1.19999.127.0.0.1.53360 (integer) 19999
111: tcpConnLocalPort.127.0.0.1.19999.127.0.0.1.53366 (integer) 19999
112: tcpConnLocalPort.127.0.0.1.19999.127.0.0.1.53367 (integer) 19999
113: tcpConnLocalPort.127.0.0.1.53324.127.0.0.1.19999 (integer) 53324
114: tcpConnLocalPort.127.0.0.1.53325.127.0.0.1.19999 (integer) 53325
115: tcpConnLocalPort.127.0.0.1.53327.127.0.0.1.19999 (integer) 53327
116: tcpConnLocalPort.127.0.0.1.53328.127.0.0.1.19999 (integer) 53328
117: tcpConnLocalPort.127.0.0.1.53332.127.0.0.1.19999 (integer) 53332
118: tcpConnLocalPort.127.0.0.1.53333.127.0.0.1.19999 (integer) 53333
119: tcpConnLocalPort.127.0.0.1.53334.127.0.0.1.19999 (integer) 53334
120: tcpConnLocalPort.127.0.0.1.53339.127.0.0.1.19999 (integer) 53339
121: tcpConnLocalPort.127.0.0.1.53342.127.0.0.1.19999 (integer) 53342
122: tcpConnLocalPort.127.0.0.1.53343.127.0.0.1.19999 (integer) 53343
123: tcpConnLocalPort.127.0.0.1.53345.127.0.0.1.19999 (integer) 53345
124: tcpConnLocalPort.127.0.0.1.53351.127.0.0.1.19999 (integer) 53351
125: tcpConnLocalPort.127.0.0.1.53352.127.0.0.1.19999 (integer) 53352
126: tcpConnLocalPort.127.0.0.1.53358.127.0.0.1.19999 (integer) 53358
127: tcpConnLocalPort.127.0.0.1.53359.127.0.0.1.19999 (integer) 53359
128: tcpConnLocalPort.127.0.0.1.53360.127.0.0.1.19999 (integer) 53360
129: tcpConnLocalPort.127.0.0.1.53366.127.0.0.1.19999 (integer) 53366
130: tcpConnLocalPort.127.0.0.1.53367.127.0.0.1.19999 (integer) 53367



2010/3/1 cheng wan <wancheng82 at gmail.com>:
> It seems clear&add_row frequently will cause memory leak.
> Any suggestion will be approved.
> Thank you all.
>
> 2010/3/1 cheng wan <wancheng82 at gmail.com>:
>> The code below is main for:
>> 1)Get tcp info from /proc/net/tcp.
>> 2)tcp_conn_table_index() is to compute the index of tcpConnEntry.
>>   Indexes:     1: tcpConnLocalAddress
>>        2: tcpConnLocalPort
>>        3: tcpConnRemAddress
>>        4: tcpConnRemPort
>> 3)Before add one row to   tcpConnEntry::instance, I use
>> tcpConnEntry::instance->clear() to clear all the rows
>> 4)get_proc_net_tcp() is excuted by one 2 seconds timer
>> The memory keeps growning.
>> Thank you all.
>>
>>
>> uin32_t tcp_conn_local_address, tcp_conn_local_port, tcp_conn_rem_address,
>> tcp_conn_rem_port, tcp_conn_state;
>>
>> void tcp_conn_table_index(struct in_addr local_address,
>>        uint32_t local_port,
>>        struct in_addr rem_address,
>>        uint32_t rem_port,
>>        char *buf) {
>>    char *local_address_ptr = inet_ntoa(local_address); // in_addr to char *
>>    IpAddress ip_address_one(local_address_ptr);
>>    char *rem_address_ptr = inet_ntoa(rem_address); // in_addr to char *
>>    IpAddress ip_address_two(rem_address_ptr);
>>    snprintf(buf, MAXLINE, "%d.%d.%d.%d.%d.%d.%d.%d.%d.%d",
>>            (unsigned char)ip_address_one[0],
>>            (unsigned char)ip_address_one[1],
>>            (unsigned char)ip_address_one[2],
>>            (unsigned char)ip_address_one[3],
>>            local_port,
>>            (unsigned char)ip_address_two[0],
>>            (unsigned char)ip_address_two[1],
>>            (unsigned char)ip_address_two[2],
>>            (unsigned char)ip_address_two[3],
>>            rem_port
>>            );
>> }
>>
>> void get_proc_net_tcp()
>> {
>>    FILE *in = NULL;
>>    in = fopen ("/proc/net/tcp", "r");
>>    char line [MAXLINE];
>>    int32_t num;
>>    static int map_states[] = { 1, 5, 3, 4, 7, 10, 11, 1, 6, 9, 2, 8 };
>>
>>    if(!in) {
>>        XLOG_WARNING("unable to open /proc/net/tcp, %s\n", strerror(errno));
>>        return false;
>>    }
>>
>>    tcpConnEntry::instance->clear();
>>
>>    while (line == fgets(line, sizeof(line), in)) {
>>        sscanf(line, "%d: %x:%x %x:%x %x", &num,
>>                 &tcp_conn_local_address,
>>                 &tcp_conn_local_port,
>>                 &tcp_conn_rem_address,
>>                 &tcp_conn_rem_port,
>>                 &tcp_conn_state);
>>        struct in_addr local_address, rem_address;
>>        memcpy(&local_address, &tcp_conn_local_address, sizeof(struct in_addr));
>>        memcpy(&rem_address, &tcp_conn_rem_address, sizeof(struct in_addr));
>>
>>        string local_address_str = inet_ntoa(local_address);
>>        string rem_address_str = inet_ntoa(rem_address);
>>
>>        char buf[MAXLINE];
>>        memset(buf, 0, sizeof(buf));
>>        tcp_conn_table_index(local_address, tcp_conn_local_port,
>>                rem_address, tcp_conn_rem_port, buf);
>>
>>        MibTableRow* row = tcpConnEntry::instance->add_row(buf);
>>
>>        if((tcp_conn_state >= 0) && (tcp_conn_state < 12)) {
>>            tcp_conn_state = map_states[tcp_conn_state];
>>        } else {
>>            tcp_conn_state = 1;
>>        }
>>
>>        tcpConnEntry::instance->set_row(row,
>>                tcp_conn_state,
>>                local_address_str.c_str(),
>>                tcp_conn_local_port,
>>                rem_address_str.c_str(),
>>                tcp_conn_rem_port
>>                );
>>
>>    }
>>    fclose(in);
>>    return true;
>> }
>>
>



More information about the AGENTPP mailing list