双重免费或腐败(外出)fclose

时间:2012-06-16 17:47:12

标签: c glibc

*** glibc detected *** /home/ghoshs/workspace/Simulator/Debug/Simulator: double free or corruption (out): 0x00000000017e6030 ***
======= Backtrace: =========
/lib/x86_64-linux-gnu/libc.so.6(+0x7e626)[0x7f34b8553626]
/lib/x86_64-linux-gnu/libc.so.6(fclose+0x155)[0x7f34b85432a5]
/home/ghoshs/workspace/Simulator/Debug/Simulator[0x40e7bf]
/home/ghoshs/workspace/Simulator/Debug/Simulator[0x40e94a]
/home/ghoshs/workspace/Simulator/Debug/Simulator[0x40e357]
/lib/x86_64-linux-gnu/libc.so.6(__libc_start_main+0xed)[0x7f34b84f676d]
/home/ghoshs/workspace/Simulator/Debug/Simulator[0x405e19]
======= Memory map: ========
00400000-0128c000 r-xp 00000000 08:06 1576650                            /home/ghoshs/workspace/Simulator/Debug/Simulator
0148b000-0148c000 r-xp 00e8b000 08:06 1576650                            /home/ghoshs/workspace/Simulator/Debug/Simulator
0148c000-014bd000 rwxp 00e8c000 08:06 1576650                            /home/ghoshs/workspace/Simulator/Debug/Simulator
014bd000-014c7000 rwxp 00000000 00:00 0 
017e6000-01807000 rwxp 00000000 00:00 0                                  [heap]
7f34b84d5000-7f34b8688000 r-xp 00000000 08:06 4849816                    /lib/x86_64-linux-gnu/libc-2.15.so
7f34b8688000-7f34b8887000 ---p 001b3000 08:06 4849816                    /lib/x86_64-linux-gnu/libc-2.15.so
7f34b8887000-7f34b888b000 r-xp 001b2000 08:06 4849816                    /lib/x86_64-linux-gnu/libc-2.15.so
7f34b888b000-7f34b888d000 rwxp 001b6000 08:06 4849816                    /lib/x86_64-linux-gnu/libc-2.15.so
7f34b888d000-7f34b8892000 rwxp 00000000 00:00 0 
7f34b8892000-7f34b88a7000 r-xp 00000000 08:06 4853364                    /lib/x86_64-linux-gnu/libgcc_s.so.1
7f34b88a7000-7f34b8aa6000 ---p 00015000 08:06 4853364                    /lib/x86_64-linux-gnu/libgcc_s.so.1
7f34b8aa6000-7f34b8aa7000 r-xp 00014000 08:06 4853364                    /lib/x86_64-linux-gnu/libgcc_s.so.1
7f34b8aa7000-7f34b8aa8000 rwxp 00015000 08:06 4853364                    /lib/x86_64-linux-gnu/libgcc_s.so.1
7f34b8aa8000-7f34b8ac0000 r-xp 00000000 08:06 4855760                    /lib/x86_64-linux-gnu/libpthread-2.15.so
7f34b8ac0000-7f34b8cbf000 ---p 00018000 08:06 4855760                    /lib/x86_64-linux-gnu/libpthread-2.15.so
7f34b8cbf000-7f34b8cc0000 r-xp 00017000 08:06 4855760                    /lib/x86_64-linux-gnu/libpthread-2.15.so
7f34b8cc0000-7f34b8cc1000 rwxp 00018000 08:06 4855760                    /lib/x86_64-linux-gnu/libpthread-2.15.so
7f34b8cc1000-7f34b8cc5000 rwxp 00000000 00:00 0 
7f34b8cc5000-7f34b8dbe000 r-xp 00000000 08:06 4849815                    /lib/x86_64-linux-gnu/libm-2.15.so
7f34b8dbe000-7f34b8fbd000 ---p 000f9000 08:06 4849815                    /lib/x86_64-linux-gnu/libm-2.15.so
7f34b8fbd000-7f34b8fbe000 r-xp 000f8000 08:06 4849815                    /lib/x86_64-linux-gnu/libm-2.15.so
7f34b8fbe000-7f34b8fbf000 rwxp 000f9000 08:06 4849815                    /lib/x86_64-linux-gnu/libm-2.15.so
7f34b8fbf000-7f34b8fe1000 r-xp 00000000 08:06 4849818                    /lib/x86_64-linux-gnu/ld-2.15.so
7f34b91bd000-7f34b91c1000 rwxp 00000000 00:00 0 
7f34b91dd000-7f34b91e1000 rwxp 00000000 00:00 0 
7f34b91e1000-7f34b91e2000 r-xp 00022000 08:06 4849818                    /lib/x86_64-linux-gnu/ld-2.15.so
7f34b91e2000-7f34b91e4000 rwxp 00023000 08:06 4849818                    /lib/x86_64-linux-gnu/ld-2.15.so
7fffc3f01000-7fffc3fab000 rwxp 00000000 00:00 0                          [stack]
7fffc3fff000-7fffc4000000 r-xp 00000000 00:00 0                          [vdso]
ffffffffff600000-ffffffffff601000 r-xp 00000000 00:00 0                  [vsyscall]

我做的很简单 FILE* temp_dataset = fopen(dataset_filnames,"w"); 经过几次写操作后,我用简单的方法关闭它 fclose(temp_dataset); 在这一点上,我得到双重免费腐败错误。在函数中,我只生成两个随机数,它们将充当源和目标,算法将进一步使用它来查找图上的路径。但是那部分完全独立于代码的这一部分。 (对不起,无关的信息堆栈溢出不允许我发布问题。)

void generate_dataset(int number_of_src_dest,
        char dataset_filnames[],NI* network_info, int k, int network_set,int dataset_num)
{
    int i,j;

    printf("Generating Dataset for the Network Set...\n");
    sprintf(dataset_filnames,"src_dest_pair_#nodes_%d_#SDP_%d_NS_%d_DS_%d",NUMBER_OF_NODES,number_of_src_dest,network_set,dataset_num);
    printf("in file.. %s\n",dataset_filnames);
    int temp_topo[NUMBER_OF_NODES][NUMBER_OF_NODES];
    copy_2D_array(NUMBER_OF_NODES,NUMBER_OF_NODES,temp_topo,network_info->topology);
    FILE* temp_dataset = fopen(dataset_filnames,"w");
    int number_of_source_destination_generated = 0;
    int source_destination[_BIG_SIZE][2];
    for (i = 0; i < NUMBER_OF_NODES; ++i)
    {
        for (j = 0; j < NUMBER_OF_NODES; ++j)
        {

            if(i != j)
            {
                int shortestpath[NUMBER_OF_NODES];
                int dist = shortest_path(i,j,temp_topo,shortestpath);
                int paths[K][NUMBER_OF_NODES];
                initialize_two_d_array(K,NUMBER_OF_NODES,paths,-1);
                if(dist>=percentage_of(PERCENTAGE_LOWER_LIMIT,compute_optical_reach()) &&
                                        dist<=percentage_of(PERCENTAGE_UPPER_LIMIT,compute_optical_reach()))
                {
                    int paths_generated = k_shortest_path(i,j,K,
                            paths,temp_topo,compute_optical_reach());
                    if(paths_generated>=K)
                    {
                        source_destination[number_of_source_destination_generated][0] = i;
                        source_destination[number_of_source_destination_generated][1] = j;
                        number_of_source_destination_generated ++;
                    }


                }
            }
        }
    }
    if(number_of_source_destination_generated>=NUMBER_OF_SOURCE_DESTINATION)
    {
        int visited_demand[number_of_source_destination_generated];
        initialize_one_d_array(number_of_source_destination_generated,visited_demand,0);

        for (i = 0; i < NUMBER_OF_SOURCE_DESTINATION; ++i)
        {
            int next_demand = random_in_range(0,number_of_source_destination_generated);
            while(visited_demand[next_demand])
                next_demand = random_in_range(0,number_of_source_destination_generated);
            visited_demand[next_demand] = 1;
            fprintf(temp_dataset,"%d\t%d\n",source_destination[next_demand][0],source_destination[next_demand][1]);
        }
    }

    else
    {
        printf("Number of Source-Destination Pairs Generated: %d\n",number_of_source_destination_generated);
        printf("Cannot generate the required number of SD pairs\nExiting...");
        exit(1);
    }


    printf("Number of Source-Destination Pairs Generated: %d\n",number_of_source_destination_generated);
    printf("Freeing up memory and flushing all buffer...\n");
    fclose(temp_dataset);
    printf("Done Generating data-sets.\n");
}

我意识到当我打开文件时,network_info类型NI内的数据(struct)会被破坏。 network结构具有表示网络的二维数组。刚打开文件后,我看到数组中引入了垃圾值。使用network_info初始化(NI*) malloc(sizeof(NI*))的大小。

1 个答案:

答案 0 :(得分:1)

根据我的经验,双重免费或损坏错误通常是代码中早期错误的症状。我注意到你有以下代码片段:

    int number_of_source_destination_generated = 0;
    int source_destination[NUMBER_OF_SOURCE_DESTINATION][2];

并在循环中:

source_destination[number_of_source_destination_generated][0] = i;
source_destination[number_of_source_destination_generated][1] = j;
number_of_source_destination_generated ++;

您如何保证number_of_source_destination_generated始终小于NUMBER_OF_SOURCE_DESTINATION?特别是你以后会:

if(number_of_source_destination_generated>=NUMBER_OF_SOURCE_DESTINATION)
{

这是非错误产生的情况。这让我相信小写变量应该大于大写变量。所以,我相信这意味着你正在写入未分配给你想要的数组的内存。这可能会导致以后出现问题,可能是您要问的问题。如果我这样做,我的实现并不会总是向我抱怨,所以如果你的相似,你不能依靠编译器来捕获这些错误。

我仍然相当缺乏经验,所以对此建议采取一些怀疑态度(如你所愿)。

编辑:我刚刚点击“显示更多评论”,这已经得到了解决。所以,这里没什么新鲜的。