[英]Shared library g++ error segmentation fault
我正在嘗試使用c ++學習共享庫(數據結構)。 每次我運行程序時,都會出現此錯誤:
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文件
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文件
#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文件
#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;
}
每次我運行該程序時,都會發生分段錯誤。 我正在使用單個循環列表創建一個鏈接列表來存儲數據。 我遍歷了代碼,但未能找到錯誤。 我無法理解為什么它沒有運行,因為我嘗試使用共享庫運行單鏈接列表,該庫運行良好且沒有任何錯誤。 我已經檢查了訪問文件的權限,這很好。
當我正在學習僅出於教育目的而在共享庫中創建數據結構時,可能會在導致問題的部分之外對該代碼進行改進或某些更改。
編輯:修改您的代碼后,我正在更改答案!
@Zsigmond指出您需要在頭文件中定義方法是正確的; 但是,這不能完全解決問題。
我編譯了源代碼,並重新創建了您看到的問題(加載時出現分段錯誤)。 我通過在shared.h中添加其他功能的實現來解決這一問題
在那之后,我得到了這個錯誤:
*** Error in ./class_user: double free or corruption (fasttop): 0x0000000002478660 ***
我能夠將其范圍縮小到fp2(ptr)
行; 注釋掉這一行可以消除錯誤。
更新的代碼如下。
更新了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
更新了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()
{
}
更新了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;
}
根據Mike P的建議進行編輯:
您不僅必須實現您在標頭中聲明的每個虛方法,還必須實現所有虛方法。 您也必須修復析構函數。 就像這樣:
SinglyCLL::~SinglyCLL()
{
node *q, *next;
if (head) {
next= head->next;
delete head;
while ((q= next) != head) {
next= q->next;
delete q;
}
}
head= tail= NULL;
}
注意:實際上,由於它是一個循環列表,因此可以刪除“ tail”指針。
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.