【问题标题】:dlclose() corrupted double-linked list:dlclose() 损坏的双链表:
【发布时间】:2014-05-08 23:47:38
【问题描述】:

我正在使用基本的 C 插件系统dlclose()。这是我的代码:

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

char** getPlugins()
{
    int i;
    char** tab=malloc(sizeof(char*)*5);
    for(i=0;i<6;++i)
        tab[i]=malloc(sizeof(char)*10);

    strcpy(tab[0],"plugin1");
    strcpy(tab[1],"plugin2");
    strcpy(tab[2],"plugin3");
    strcpy(tab[3],"plugin4");
    tab[4]=NULL;
    return tab;
}

这是调用它的函数(包含共享库路径的 libtab):

#include "loadlib.h"
#include <stdio.h>
#include <stdlib.h>
#include <dlfcn.h>

void loadlib(char** libtab, Node** bst)
{
    int i=0;
    void* (*mime_type) ();
    void* handle;
    while(libtab[i]!=NULL)
    {
        handle=dlopen(libtab[i],RTLD_LAZY);
        if(handle==NULL)
            fprintf(stderr,"%s\n",dlerror());
        else
        {
            mime_type=dlsym(handle,"getPlugins");
            fill_tree(bst,mime_type());
        }
        ++i;
        /*dlclose(handle);*/
    }
}

此代码仅使用一次循环迭代进行了测试,并且函数本身正确地完成了它的工作。当运行所有 6 个项目时,我得到一个错误:

*** Error in `./plugin': corrupted double-linked list: 0x0000000000e6aad0 ***
Inconsistency detected by ld.so: dl-open.c: 220: dl_open_worker: Assertion `_dl_debug_initialize (0, args->nsid)->r_state == RT_CONSISTENT' failed!

有人可以翻译这个错误的含义以及我做错了什么吗?

【问题讨论】:

  • 您分配了 5 个指针,但循环 0-5(包括 6 个项目)。
  • 非常感谢。实际上就是这样。这个奇怪的错误实际上让我着迷,因为它看起来更加微妙。

标签: c shared-libraries


【解决方案1】:

你循环太远了,分配了 5 个指针并循环到 6:

char** tab=malloc(sizeof(char*)*5);
for(i=0;i<6;++i)
    tab[i]=malloc(sizeof(char)*10);

【讨论】:

    猜你喜欢
    • 2015-09-22
    • 1970-01-01
    • 1970-01-01
    • 2016-01-01
    • 1970-01-01
    • 2016-07-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多