Sign in to follow this  
fatima

exporting Template calsses

Recommended Posts

fatima    122
Hi I want ot export my simple templated link list calss to dll.while developing source ,every thing works well. but if i use exported class in a test application, it will crack on special function, where i have used "delete some pointer". as i told the code works well while it is not exported to dll.
template<class T> class mylinklist
{
//...
};
class __declspec(dllexport) A;
{
};
class __declspec(dllexport) B
{
};

class __declspec(dllexport) C
{
mylinklist<A> member;
mylinklist<B> member;
};


sorry for my great english. [Edited by - fatima on April 30, 2010 1:48:47 AM]

Share this post


Link to post
Share on other sites
iMalc    2466
From your description you could be deleting memory that was allocated by the DLL within the exe, or vice-versa. You can't do that in most circumstances.

Beyond that, you'll need to give more information. Since your English isn't so good the good news is that I'd suggest posting more of the code rather than just describing it more. Please try and make it the unedited code also.

Share this post


Link to post
Share on other sites
fatima    122
Thank you for attention.
this code is my link list class.
[Source]

template<class Object> struct Field
{
Field<Object>* NextField;
Object Data;
};

template<class Object> class List
{
public:
List<Object>(void);
~List<Object>(void);
protected:
unsigned int ObjectSCount;
Field<Object>* TopNode;
CException Exception;
public:
Field<Object>* GetPointer();
unsigned int Size();
Object& operator[] (unsigned int Index);
Field<Object>* Field (unsigned int Index);
void Add(Object NewObject);
void Insert(Object NewObject, unsigned int Index);
void Remove(unsigned int Index);
void Clear();
};

template <class Object> List<Object>::List(void)
{
this->ObjectSCount = 0;
this->TopNode = 0;
}

template <class Object> List<Object>::~List(void)
{
this->Clear();
}

template <class Object> Field<Object>* List<Object>::GetPointer()
{
return this->TopNode;
}

template <class Object> unsigned int List<Object>::Size()
{
return this->ObjectSCount;
}

template <class Object> Object& List<Object>::operator[] (unsigned int Index)
{
if(Index < 0 || Index >= this->ObjectSCount)
{
this->Exception.Message = L"Index is out of range.";
throw this->Exception;
}
Field<Object>* TempNode = TopNode;
for(unsigned int i = this->ObjectSCount-1; i > Index;i--)
{
TempNode = TempNode->NextField;
}
return TempNode->Data;
}

template <class Object> Field<Object>* List<Object>::Field (unsigned int Index)
{
if(Index < 0 || Index >= this->ObjectSCount)
{
this->Exception.Message = L"Index is out of range.";
throw this->Exception;
}
Field<Object>* TempNode = TopNode;
for(unsigned int i = this->ObjectSCount-1; i > Index;i--)
{
TempNode = TempNode->NextField;
}
return TempNode;
}

template <class Object> void List<Object>::Add(Object NewObject)
{
Field<Object>* NewNode = new Field<Object>;
NewNode->Data = NewObject;
NewNode->NextField = this->TopNode;
this->TopNode = NewNode;
ObjectSCount++;
}

template <class Object> void List<Object>::Insert(Object NewObject, unsigned int Index)
{
if(Index < 0 || Index >= this->ObjectSCount)
{
this->Exception.Message = L"Index is out of range.";
throw this->Exception;
}
Field<Object>* NewNode = new Field<Object>;
NewNode->Data = NewObject;
Field<Object>* TempNode = 0;
TempNode = this->Field(Index)->NextField;
this->Field(Index)->NextField = NewNode;
NewNode->NextField = TempNode;
ObjectSCount++;
}

template <class Object> void List<Object>::Remove(unsigned int Index)
{
if(Index < 0 || Index >= this->ObjectSCount)
{
this->Exception.Message = L"Index is out of range.";
throw this->Exception;
}
if(Index == this->ObjectSCount-1)
{
Field<Object>* TempNodeLeft = this->Field(Index)->NextField;
delete this->Field(Index);
this->TopNode = TempNodeLeft;
}
else
{
Field<Object>* TempNodeLeft = this->Field(Index)->NextField;
Field<Object>* TempNodeRight = this->Field(Index+1);
delete this->Field(Index);
TempNodeRight->NextField = TempNodeLeft;
}
ObjectSCount--;
if(ObjectSCount < 0) ObjectSCount = 0;
}

template <class Object> void List<Object>::Clear()
{
Field<Object>* TempNode = TopNode;
Field<Object>* TempCopy = 0;
while(TempNode != 0)
{
TempCopy = TempNode->NextField;
delete TempNode;
TempNode = TempCopy;
}
this->TopNode = 0;
this->ObjectSCount = 0;
}




other classes are using this class are large and I can't embed them here,but I know problem is occurs when List::clear called.
BTW problem is not an exception, it is a breakpoint that automatically triggered by windows(as MS Visual Studio says).if I run executable file directly this break wont occurred.
and about my english,is it realy so bad?
[I edited only text,code is same].

Share this post


Link to post
Share on other sites

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now

Sign in to follow this