简体   繁体   English

共享库G ++错误分段错误

[英]Shared library g++ error segmentation fault

I am trying to learn shared library (data structures) using c++; 我正在尝试使用c ++学习共享库(数据结构)。 every time I run my program, it gives this error: 每次我运行程序时,都会出现此错误:

sachin@sachin-desktop:~$ cd /home/sachin/sDL
sachin@sachin-desktop:~/sDL$ g++ -fPIC -shared myclass.cc -o myclass.so
sachin@sachin-desktop:~/sDL$ g++ class_user.cc -ldl -o class_user
sachin@sachin-desktop:~/sDL$ ./class_user
0
Unable to load Sachin's library 
Segmentation fault (core dumped)
sachin@sachin-desktop:~/sDL$ 

shared.h file shared.h文件

typedef struct node 
{
    struct node *prev;
    int data;
    struct node *next;
}NODE,*PNODE;


class SinglyCLL
{
    private :
    PNODE head;
    PNODE tail;
    public :
    SinglyCLL();
    ~SinglyCLL();
    virtual void InsertFirst(int);
    virtual void InsertLast(int);
    virtual void InsertAtPosition(int,int);
    virtual void DeleteFirst();
    virtual void DeleteLast();
    virtual void DeleteAtPosition(int);
    virtual int Count();
    virtual void Display();

};

myclass.cc file myclass.cc文件

#include"sharedfile.h"
#include<iostream>
using namespace std;

SinglyCLL::SinglyCLL()
{
    head=NULL;
    tail=NULL;
}

void SinglyCLL::InsertFirst(int ino)
{
    PNODE temp=head;
    PNODE newN=NULL;

    newN=new NODE;
    newN->next=NULL;
    newN->data=ino;
    newN->prev=NULL;

    if((head==NULL)&&(tail==NULL))
    {
        head=newN;
        tail=newN;
        tail->next=head;
        head->prev=tail;
    }

    newN->next=head;
    head->prev=newN;
    head=newN;
    tail->next=head;
    head->prev=tail;

}

void SinglyCLL::Display()
{
    PNODE temp=head;

    if((head==NULL)&&(tail==NULL))
    {
        return;
    }

    do
    {
        cout<<temp->data<<endl;
        temp=temp->next;

    }while(tail->next!=temp);
}

SinglyCLL::~SinglyCLL()
{
    delete head;
    delete tail;
}

extern "C"
{
    SinglyCLL *create()
    {
        return new SinglyCLL;
    }
    void destroy (SinglyCLL* p)
    {
        delete p;
    }
}

class_user.cc file class_user.cc文件

#include <stdio.h>
#include <stdlib.h>
#include <dlfcn.h>
#include <iostream>
#include "sharedfile.h"
using namespace std;

int main()
{
    void *p=NULL;
    SinglyCLL *ptr=NULL;
    SinglyCLL* (*fp1)()=NULL;
    void (*fp2)(SinglyCLL*)=NULL;

    p=dlopen("/home/sachin/sDL/myclass.so",RTLD_LAZY);
    if(!p)
    {
        cout<<p<<endl;
        printf("Unable to load Sachin's library \n");
    }

    fp1=(SinglyCLL*(*)())dlsym(p,"create");
    fp2=(void(*)(SinglyCLL*))dlsym(p,"destroy");

    ptr=fp1();

    ptr->InsertFirst(10);
    /*ptr->InsertAtPosition(70,1);
    ptr->InsertAtPosition(80,2);*/
    ptr->Display();

    fp2(ptr);
    dlclose(p);
    return 0;
}

Every time I run this program, a segmentation fault occurs. 每次我运行该程序时,都会发生分段错误。 I am using singly circular list to create a linked-list to store data. 我正在使用单个循环列表创建一个链接列表来存储数据。 I have gone through the code and failed to locate the error. 我遍历了代码,但未能找到错误。 I am unable to understand why it's not running as I tried to run singly linked-list with shared library which is running fine without any error. 我无法理解为什么它没有运行,因为我尝试使用共享库运行单链接列表,该库运行良好且没有任何错误。 I have checked the permissions to access the files, which is fine. 我已经检查了访问文件的权限,这很好。

As I am learning to create data structures in a shared library just for educational purposes, there might be chances of improvement or something to change in this code outside the part which causing the problem. 当我正在学习仅出于教育目的而在共享库中创建数据结构时,可能会在导致问题的部分之外对该代码进行改进或某些更改。

EDIT: After tinkering with your code, I'm changing my answer! 编辑:修改您的代码后,我正在更改答案!

@Zsigmond is correct in pointing out that you need to define the methods in the header file; @Zsigmond指出您需要在头文件中定义方法是正确的; however, that doesn't completely solve the problem. 但是,这不能完全解决问题。

I compiled your source and recreated the problem you saw (the segmentation fault on load). 我编译了源代码,并重新创建了您看到的问题(加载时出现分段错误)。 I resolved that by adding implementations for the other functions in shared.h 我通过在shared.h中添加其他功能的实现来解决这一问题

After that, I got this error: 在那之后,我得到了这个错误:

*** Error in ./class_user: double free or corruption (fasttop): 0x0000000002478660 ***

I was able to narrow this down to the fp2(ptr) line; 我能够将其范围缩小到fp2(ptr)行; commenting this line out removed the error. 注释掉这一行可以消除错误。

Updated code is below. 更新的代码如下。

Updated shared.h: 更新了shared.h:

// Header Guard (prevents duplicate symbols)
#ifndef __SHARED_H
#define __SHARED_H

// C++ syntax for a struct doesn't need typedef
struct node
{
    node* prev;
    int data;
    node* next;
};


class SinglyCLL
{
private :
    node* head;
    node* tail;
public :
    SinglyCLL();
    ~SinglyCLL();
    virtual void InsertFirst(int);
    virtual void InsertLast(int);
    virtual void InsertAtPosition(int,int);
    virtual void DeleteFirst();
    virtual void DeleteLast();
    virtual void DeleteAtPosition(int);
    virtual int Count();
    virtual void Display();

};

#endif // __SHARED_H

Updated myclass.cc: 更新了myclass.cc:

#include"shared.h"
#include<iostream>
using namespace std;

SinglyCLL::SinglyCLL()
{
    head=NULL;
    tail=NULL;
}

void SinglyCLL::InsertFirst(int ino)
{
    node* temp=head;
    node* newN=NULL;

    newN=new node;
    newN->next=NULL;
    newN->data=ino;
    newN->prev=NULL;

    if((head==NULL)&&(tail==NULL))
    {
        head=newN;
        tail=newN;
        tail->next=head;
        head->prev=tail;
    }

    newN->next=head;
    head->prev=newN;
    head=newN;
    tail->next=head;
    head->prev=tail;

}

void SinglyCLL::Display()
{
    node* temp=head;

    if((head==NULL)&&(tail==NULL))
    {
        return;
    }

    do
    {
        cout<<temp->data<<endl;
        temp=temp->next;

    }while(tail->next!=temp);
}

SinglyCLL::~SinglyCLL()
{
    delete head;
    delete tail;
}

extern "C"
{
    SinglyCLL *create()
    {
        return new SinglyCLL;
    }
    void destroy (SinglyCLL* p)
    {
        delete p;
    }
}

// Empty implementations for the other functions
void SinglyCLL::InsertLast(int)
{

}

void SinglyCLL::InsertAtPosition(int,int)
{

}

void SinglyCLL::DeleteFirst()
{

}

void SinglyCLL::DeleteLast()
{

}

void SinglyCLL::DeleteAtPosition(int)
{

}

int SinglyCLL::Count()
{

}

Updated class_user.cc: 更新了class_user.cc:

#include <stdlib.h>
#include <dlfcn.h>
#include <iostream>
#include "shared.h"
using namespace std;

int main()
{
    void *p=NULL;
    SinglyCLL *ptr=NULL;
    SinglyCLL* (*fp1)()=NULL;
    void (*fp2)(SinglyCLL*)=NULL;

    p=dlopen("/home/mike/Projects/C++/myclass.so",RTLD_LAZY);
    if(!p)
    {
        cout<<p<<endl;
        cout << "Unable to load Sachin's library" << endl;
    }

    fp1=(SinglyCLL*(*)())dlsym(p,"create");
    fp2=(void(*)(SinglyCLL*))dlsym(p,"destroy");

    ptr=fp1();

    ptr->InsertFirst(10);
    ptr->InsertAtPosition(70,1);
    ptr->InsertAtPosition(80,2);
    ptr->Display();

    // Un-comment next line to force the 'double free' error.
    //fp2(ptr);
    dlclose(p);
    return 0;
}

Edited on Mike P's suggestion: 根据Mike P的建议进行编辑:

You not only have to implement every virtual method you declare in the header; 您不仅必须实现您在标头中声明的每个虚方法,还必须实现所有虚方法。 you have to fix your destructor too. 您也必须修复析构函数。 It would be something like this: 就像这样:

SinglyCLL::~SinglyCLL()
{
    node *q, *next;

    if (head) {
        next= head->next;
        delete head;

        while ((q= next) != head) {
            next=  q->next;
            delete q;
        }
    }
    head= tail= NULL;
}

Note: actually, as it is a circular list, 'tail' pointer could be removed. 注意:实际上,由于它是一个循环列表,因此可以删除“ tail”指针。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM