c - dlclose() 损坏的双链表 :

标签 c shared-libraries

我正在使用基本的 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 个指针并循环直到 6:

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

关于c - dlclose() 损坏的双链表 :,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/23554518/

相关文章:

c - 使用 time.h 的奇怪 CLOCKS_PER_SEC 值

c++ - 变量在传递给共享库函数时没有相同的值

c - 让 gdb 显示汇编指令而不是 C

c++ - GCC -m32 标志 :/usr/bin/ld: skipping incompatible

c - 如何解决两个共享库之间的相互引用?

windows - Linux 的 ldd 在 Windows 上的等效项是什么?

c - c 库中的线程安全说明

C 数组长度(填充)

c++ - 如何在 CLI 中打印表格

c - linux dup2 和 printf