返回值为-1时,getline函数内存泄漏

时间:2017-01-25 18:24:06

标签: c

#include <stdio.h>                              
int main(int argc, char** argv)                 
{                                               
    FILE *fp = NULL;                            
    char* line = NULL;                          
    ssize_t read = 0;                           
    ssize_t len = 0;                            
    fp = popen("lspci | grep aaaaaa", "r");     

    if((read = getline(&line, &len, fp)) == -1){
        pclose(fp);                             
        return -1;                              
    }                                           

    pclose(fp);                                 
}

这是测试程序。

我在这里写了功能测试代码。我想知道函数getline何时返回-1是否需要释放缓冲区。我在我的环境中测试它。我发现当getline函数返回-1时,存在内存泄漏。以下是我的调试步骤:

Breakpoint 1, main (argc=1, argv=0xbffff754) at getline.c:8
8           FILE *fp = NULL;
(gdb) n
9           char* line = NULL;
(gdb)
10          ssize_t read = 0;
(gdb)
11          ssize_t len = 0;
(gdb)
12          fp = popen("lspci | grep aaaaaa", "r");
(gdb)
14          if((read = getline(&line, &len, fp)) == -1){
(gdb) i locals
fp = 0x804b008
line = 0x0
read = 0
len = 0
(gdb) n
15              pclose(fp);
(gdb) i locals
fp = 0x804b008
line = 0x804b0b8 ""
read = -1
len = 120

1 个答案:

答案 0 :(得分:0)

这是getline函数的第一个问题。另一个问题在这里。我想知道getline函数是否使用realloc函数,并在缓冲区不够时释放缓冲区。以下是另一个测试代码。我得到了getline函数用128字节初始化malloc内存的点。但是当缓冲区不够时,它不使用realloc函数来分配缓冲区。

以下测试代码分配3个缓冲区。 getline函数缓冲区位于中间。初始化的getline缓冲区是128个字节。然后我再次调用getline函数来获取超过128个字节的缓冲区。我在自由函数中设置了一个断点,但它没有被调用。

#include <stdio.h> 
#include <stdlib.h>
#include <string.h>

int main(int argc, char** argv)                 
{

    char *buffer = NULL;
    char *buffer1 = NULL;
    char *buffer2 = NULL;
    size_t bufsize = 32;
    size_t characters;


    buffer1 = (char *)malloc(bufsize * sizeof(char));
    if( buffer1 == NULL)
    {
        perror("Unable to allocate buffer");
        exit(1);
    }

    printf("buffer1 = 0x%p\n", buffer1);
    printf("Type something: ");
    characters = getline(&buffer,&bufsize,stdin);

    printf(" get line buffer  = 0x%p\n", buffer);
    buffer2 = (char *)malloc(bufsize * sizeof(char));
    if( buffer2 == NULL)
    {
        perror("Unable to allocate buffer");
        exit(1);
    }
    memset(buffer2, 21,bufsize);

    printf("buffer2 = 0x%p\n", buffer2);
    printf("buffer2 content = %s\n", buffer2);
    printf("%zu characters were read.\n",characters);
    printf("You typed: '%s'\n",buffer);
    printf("The address is  = 0x%p\n", buffer);

    characters = getline(&buffer,&bufsize,stdin);
    printf("%zu characters were read.\n",characters);
    printf("You typed: '%s'\n",buffer);
    printf("The address is change = 0x%p\n", buffer);
    free(buffer);
    return(0);
}

调试过程:

Breakpoint 1, main (argc=1, argv=0xbffff754) at getline.c:22
22          char *buffer = NULL;
(gdb) n
23          char *buffer1 = NULL;
(gdb)
24          char *buffer2 = NULL;
(gdb)
25          size_t bufsize = 32;
(gdb)
29          buffer1 = (char *)malloc(bufsize * sizeof(char));
(gdb)
30          if( buffer1 == NULL)
(gdb)
36          printf("buffer1 = 0x%p\n", buffer1);
(gdb)
buffer1 = 0x0x804b008
38          printf("Type something: ");
(gdb)
39          characters = getline(&buffer,&bufsize,stdin);
(gdb)
Type something: aaaaaa
41          printf(" get line buffer  = 0x%p\n", buffer);
(gdb)
 get line buffer  = 0x0x804b030
42          buffer2 = (char *)malloc(bufsize * sizeof(char));
(gdb) n
43          if( buffer2 == NULL)
(gdb) n
48          memset(buffer2, 21,bufsize);
(gdb) n
50          printf("buffer2 = 0x%p\n", buffer2);
(gdb) n
buffer2 = 0x0x804b0b0
52          printf("%zu characters were read.\n",characters);
(gdb) p  0xb0 -0x30
$1 = 128
(gdb) b free
Breakpoint 2 at 0xb7e8cc60: free. (2 locations)
(gdb) n
7 characters were read.
53          printf("You typed: '%s'\n",buffer);
(gdb) n
You typed: 'aaaaaa
'
54          printf("The address is  = 0x%p\n", buffer);
(gdb) n
The address is  = 0x0x804b030
56          characters = getline(&buffer,&bufsize,stdin);
(gdb)
aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa
57          printf("%zu characters were read.\n",characters);
(gdb) n
190 characters were read.
58          printf("You typed: '%s'\n",buffer);
(gdb)
You typed: 'aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa
'
59          printf("The address is change = 0x%p\n", buffer);
(gdb)
The address is change = 0x0x804b130
60          free(buffer);
(gdb) i b
Num     Type           Disp Enb Address    What
1       breakpoint     keep y   0x08048566 in main at getline.c:22
        breakpoint already hit 1 time
2       breakpoint     keep y   <MULTIPLE>
2.1                         y     0xb7e8cc60 in __GI___libc_free at malloc.c:2912
2.2                         y     0xb7ff3730 in free at dl-minimal.c:119