473,888 Members | 2,213 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

template class in DLL (g++/Cygwin)

Hi all,

I am trying to learn more about how to use g++/Cygwin to produce dll
files on WinXP. And I have a problem which at the first look seems to
be an obvious dll-export problem, but I don't think this is really the
issue. Here is a description of my problem.

MathLibrary.dll :
- SpaceTimeVector (uses various stuff from this dll)

PhysLibrary.dll :
- PointMassless (uses instances of SpaceTimeVector )
- PointMassive (derived from PointMassless + a few simple data and
method members)
- SetPoints as a template class (see bellow)

Test framework(binar y .exe):
- TestPhysLibrary Template as a template class for various tests. It
has a static member function TestPhysLibrary Abstract* getInstance() {
return(new T);}
- TestPhysLibrary Abstract (from this type are devired various tester
classes, each has to have a member method called void runTest() which
contains concrete tests)
- PhysLibraryTest (instantiates TestPhysLibrary Template and passes
this instance to member function testFactory as a parameter, where
runTest() is ran and the result is evaluated
This works for me fine for some simple tests on PointMassless and
PointMassive instances (of regular classes). But I have a big problem
with SetPoints which is a template class. This class contains a
private member called points which is std::vector of the same type as
the class template type parameter. Linker gives following info:

Info: resolving vtable for PhysLibrary::Po intMasslessby linking to
__imp___ZTVN11P hysLibrary13Poi ntMasslessE (auto-import)
Info: resolving vtable for PhysLibrary::Po intMassiveby linking to
__imp___ZTVN11P hysLibrary12Poi ntMassiveE (auto-import)

It seems o.k. to me, because it vas resolved to the proper template
class instances. But running the test it crashes with

5 [main] PhysLibraryTest 2136 handle_exceptio ns: Exception:
STATUS_ACCESS_V IOLATION

What seems strange to me, the problem only arises when I use at least
two instances of the template class. For the first one, it works fine,
the runtime error arises only if I try to add data to the vector<> of
the second instance.

Does anybody know here, what conceptual mistake am I doing here ?
Bellow is copy of the relevant parts of the code.

Thank you very much in advance.

David Komanek

setPoints.h:
============
#ifndef _SETPOINTS_H_
#define _SETPOINTS_H_

#include <ctype.h>
#include <vector>
#include <bitset>
using namespace std;

namespace PhysLibrary
{

template<class PointType>
class SetPoints
{
public:
SetPoints<Point Type>() { };
virtual ~SetPoints<Poin tType>() { };
vector<PointTyp e> getPoints();
unsigned long countPoints();
void addPoint(PointT ype *);
void removePointsAll ();
//private: //commented out untill the issues are resolved to be
able to get the address of private members directly
vector<PointTyp e> points;
};

template<class PointType>
vector<PointTyp e> SetPoints<Point Type>::getPoint s()
{
vector<PointTyp e> v = new vector<PointTyp e>(this->points);
return v;
}

template<class PointType>
unsigned long SetPoints<Point Type>::countPoi nts()
{
return ((unsigned long) (points.size()) );
}

template<class PointType>
void SetPoints<Point Type>::addPoint (PointType *p)
{
points.push_bac k(*p);
}

template<class PointType>
void SetPoints<Point Type>::removePo intsAll()
{
points.clear();
}

}; // namespace

#endif //_SETPOINTS_H_

TestPhysLibrary SetPoints.h
=============== ===========
#ifndef _TESTPHYSLIBRAR YSETPOINTS_H_
#define _TESTPHYSLIBRAR YSETPOINTS_H_

#include "TestPhysLibrar yAbstract.h"
#include "../PhysLibrary/Primitives/PointMassless.h "
#include "../PhysLibrary/Primitives/PointMassive.h"
#include "../PhysLibrary/Primitives/PhysFunctions.h "
#include "../PhysLibrary/Compounds/SetPoints.h"

class TestPhysLibrary SetPoints : public TestPhysLibrary Abstract
{
public:
TestPhysLibrary SetPoints();
virtual ~TestPhysLibrar ySetPoints();
virtual void runTest();
};

#endif //_TESTPHYSLIBRAR YSETPOINTS_H_


TestPhysLibrary SetPoints.cpp
=============== =============
#include "TestPhysLibrar ySetPoints.h"

TestPhysLibrary SetPoints::Test PhysLibrarySetP oints()
{
}

TestPhysLibrary SetPoints::~Tes tPhysLibrarySet Points()
{
}

void TestPhysLibrary SetPoints::runT est()
{
double coord01[3] = {1,2,3};
double coord02[3] = {11,-3,28.0};
double veloc01[3] = {-1,8,1.5};
double veloc02[3] = {5,0,-1};
bool dimension_chara cteristics[3] = {true,true,true };
MathLibrary::Sp aceTimeVector *c1 = new
MathLibrary::Sp aceTimeVector(c oord01, 3, dimension_chara cteristics,
3);
MathLibrary::Sp aceTimeVector *c2 = new
MathLibrary::Sp aceTimeVector(c oord02, 3, dimension_chara cteristics,
3);
MathLibrary::Sp aceTimeVector *v1 = new
MathLibrary::Sp aceTimeVector(v eloc01, 3, dimension_chara cteristics,
3);
MathLibrary::Sp aceTimeVector *v2 = new
MathLibrary::Sp aceTimeVector(v eloc02, 3, dimension_chara cteristics,
3);
MathLibrary::Sp aceTimeVector *c3 = new
MathLibrary::Sp aceTimeVector(c oord01, 3, dimension_chara cteristics,
3);
MathLibrary::Sp aceTimeVector *c4 = new
MathLibrary::Sp aceTimeVector(c oord02, 3, dimension_chara cteristics,
3);
MathLibrary::Sp aceTimeVector *v3 = new
MathLibrary::Sp aceTimeVector(v eloc01, 3, dimension_chara cteristics,
3);
MathLibrary::Sp aceTimeVector *v4 = new
MathLibrary::Sp aceTimeVector(v eloc02, 3, dimension_chara cteristics,
3);
PhysLibrary::Po intMassive *pm1 = new PhysLibrary::Po intMassive(*c1,
*v1, 8);
PhysLibrary::Po intMassive *pm2 = new PhysLibrary::Po intMassive(*c2,
*v2, 8);
PhysLibrary::Po intMassless *pl1 = new PhysLibrary::Po intMassless(*c3 ,
*v3);
PhysLibrary::Po intMassless *pl2 = new PhysLibrary::Po intMassless(*c4 ,
*v4);

//following two lines are responsible for the linker info
typedef PhysLibrary::Se tPoints<PhysLib rary::PointMass ive> setMassive;
typedef PhysLibrary::Se tPoints<PhysLib rary::PointMass less>
setMassless;

setMassive *sp1 = new setMassive();
cout << typeid(sp1->points).name () << endl;
cout << (&(sp1->points)) << endl;
sp1->addPoint(pm1 ); // WORKS FINE
sp1->addPoint(pm2 ); // WORKS FINE

// commenting out the following block makes the program working :-)
setMassless *sp2 = new setMassless();
cout << typeid(sp2->points).name () << endl;
cout << (&(sp2->points)) << endl;
sp2->addPoint(pl1 ); // CRASHES THE PROGRAM !!!!
sp2->addPoint(pl2 );
}


OUTPUT of the :
=======
St6vectorIN11Ph ysLibrary12Poin tMassiveESaIS1_ EE
0x100842a4
St6vectorIN11Ph ysLibrary13Poin tMasslessESaIS1 _EE
0x100852bc
4 [main] PhysLibraryTest 3972 handle_exceptio ns: Exception:
STATUS_ACCESS_V IOLATION
604 [main] PhysLibraryTest 3972 open_stackdumpf ile: Dumping stack
trace to PhysLibraryTest .exe.stackdump
Jul 23 '05 #1
3 4474

Well, after the whole night by the comp I found the cause: missing copy
constructor. But the symptoms are evil, do not understand why compiler
did not detected it. What makes me even more frustrated is that copying
STL vector does not perform deep copying of compound objects, but this
is probably my fault, too. I am seeking for some good guide how to use
containers for storing complex objects (not pointers to them). Every
help highly is appreciated :-)

Thanks,

David

David Komanek wrote:
Hi all,

I am trying to learn more about how to use g++/Cygwin to produce dll
files on WinXP. And I have a problem which at the first look seems to
be an obvious dll-export problem, but I don't think this is really the
issue. Here is a description of my problem.

MathLibrary.dll :
- SpaceTimeVector (uses various stuff from this dll)

PhysLibrary.dll :
- PointMassless (uses instances of SpaceTimeVector )
- PointMassive (derived from PointMassless + a few simple data and
method members)
- SetPoints as a template class (see bellow)

Test framework(binar y .exe):
- TestPhysLibrary Template as a template class for various tests. It
has a static member function TestPhysLibrary Abstract* getInstance() {
return(new T);}
- TestPhysLibrary Abstract (from this type are devired various tester
classes, each has to have a member method called void runTest() which
contains concrete tests)
- PhysLibraryTest (instantiates TestPhysLibrary Template and passes
this instance to member function testFactory as a parameter, where
runTest() is ran and the result is evaluated
This works for me fine for some simple tests on PointMassless and
PointMassive instances (of regular classes). But I have a big problem
with SetPoints which is a template class. This class contains a
private member called points which is std::vector of the same type as
the class template type parameter. Linker gives following info:

Info: resolving vtable for PhysLibrary::Po intMasslessby linking to
__imp___ZTVN11P hysLibrary13Poi ntMasslessE (auto-import)
Info: resolving vtable for PhysLibrary::Po intMassiveby linking to
__imp___ZTVN11P hysLibrary12Poi ntMassiveE (auto-import)

It seems o.k. to me, because it vas resolved to the proper template
class instances. But running the test it crashes with

5 [main] PhysLibraryTest 2136 handle_exceptio ns: Exception:
STATUS_ACCESS_V IOLATION

What seems strange to me, the problem only arises when I use at least
two instances of the template class. For the first one, it works fine,
the runtime error arises only if I try to add data to the vector<> of
the second instance.

Does anybody know here, what conceptual mistake am I doing here ?
Bellow is copy of the relevant parts of the code.

Thank you very much in advance.

David Komanek

setPoints.h:
============
#ifndef _SETPOINTS_H_
#define _SETPOINTS_H_

#include <ctype.h>
#include <vector>
#include <bitset>
using namespace std;

namespace PhysLibrary
{

template<class PointType>
class SetPoints
{
public:
SetPoints<Point Type>() { };
virtual ~SetPoints<Poin tType>() { };
vector<PointTyp e> getPoints();
unsigned long countPoints();
void addPoint(PointT ype *);
void removePointsAll ();
//private: //commented out untill the issues are resolved to be
able to get the address of private members directly
vector<PointTyp e> points;
};

template<class PointType>
vector<PointTyp e> SetPoints<Point Type>::getPoint s()
{
vector<PointTyp e> v = new vector<PointTyp e>(this->points);
return v;
}

template<class PointType>
unsigned long SetPoints<Point Type>::countPoi nts()
{
return ((unsigned long) (points.size()) );
}

template<class PointType>
void SetPoints<Point Type>::addPoint (PointType *p)
{
points.push_bac k(*p);
}

template<class PointType>
void SetPoints<Point Type>::removePo intsAll()
{
points.clear();
}

}; // namespace

#endif //_SETPOINTS_H_

TestPhysLibrary SetPoints.h
=============== ===========
#ifndef _TESTPHYSLIBRAR YSETPOINTS_H_
#define _TESTPHYSLIBRAR YSETPOINTS_H_

#include "TestPhysLibrar yAbstract.h"
#include "../PhysLibrary/Primitives/PointMassless.h "
#include "../PhysLibrary/Primitives/PointMassive.h"
#include "../PhysLibrary/Primitives/PhysFunctions.h "
#include "../PhysLibrary/Compounds/SetPoints.h"

class TestPhysLibrary SetPoints : public TestPhysLibrary Abstract
{
public:
TestPhysLibrary SetPoints();
virtual ~TestPhysLibrar ySetPoints();
virtual void runTest();
};

#endif //_TESTPHYSLIBRAR YSETPOINTS_H_


TestPhysLibrary SetPoints.cpp
=============== =============
#include "TestPhysLibrar ySetPoints.h"

TestPhysLibrary SetPoints::Test PhysLibrarySetP oints()
{
}

TestPhysLibrary SetPoints::~Tes tPhysLibrarySet Points()
{
}

void TestPhysLibrary SetPoints::runT est()
{
double coord01[3] = {1,2,3};
double coord02[3] = {11,-3,28.0};
double veloc01[3] = {-1,8,1.5};
double veloc02[3] = {5,0,-1};
bool dimension_chara cteristics[3] = {true,true,true };
MathLibrary::Sp aceTimeVector *c1 = new
MathLibrary::Sp aceTimeVector(c oord01, 3, dimension_chara cteristics,
3);
MathLibrary::Sp aceTimeVector *c2 = new
MathLibrary::Sp aceTimeVector(c oord02, 3, dimension_chara cteristics,
3);
MathLibrary::Sp aceTimeVector *v1 = new
MathLibrary::Sp aceTimeVector(v eloc01, 3, dimension_chara cteristics,
3);
MathLibrary::Sp aceTimeVector *v2 = new
MathLibrary::Sp aceTimeVector(v eloc02, 3, dimension_chara cteristics,
3);
MathLibrary::Sp aceTimeVector *c3 = new
MathLibrary::Sp aceTimeVector(c oord01, 3, dimension_chara cteristics,
3);
MathLibrary::Sp aceTimeVector *c4 = new
MathLibrary::Sp aceTimeVector(c oord02, 3, dimension_chara cteristics,
3);
MathLibrary::Sp aceTimeVector *v3 = new
MathLibrary::Sp aceTimeVector(v eloc01, 3, dimension_chara cteristics,
3);
MathLibrary::Sp aceTimeVector *v4 = new
MathLibrary::Sp aceTimeVector(v eloc02, 3, dimension_chara cteristics,
3);
PhysLibrary::Po intMassive *pm1 = new PhysLibrary::Po intMassive(*c1,
*v1, 8);
PhysLibrary::Po intMassive *pm2 = new PhysLibrary::Po intMassive(*c2,
*v2, 8);
PhysLibrary::Po intMassless *pl1 = new PhysLibrary::Po intMassless(*c3 ,
*v3);
PhysLibrary::Po intMassless *pl2 = new PhysLibrary::Po intMassless(*c4 ,
*v4);

//following two lines are responsible for the linker info
typedef PhysLibrary::Se tPoints<PhysLib rary::PointMass ive> setMassive;
typedef PhysLibrary::Se tPoints<PhysLib rary::PointMass less>
setMassless;

setMassive *sp1 = new setMassive();
cout << typeid(sp1->points).name () << endl;
cout << (&(sp1->points)) << endl;
sp1->addPoint(pm1 ); // WORKS FINE
sp1->addPoint(pm2 ); // WORKS FINE

// commenting out the following block makes the program working :-)
setMassless *sp2 = new setMassless();
cout << typeid(sp2->points).name () << endl;
cout << (&(sp2->points)) << endl;
sp2->addPoint(pl1 ); // CRASHES THE PROGRAM !!!!
sp2->addPoint(pl2 );
}


OUTPUT of the :
=======
St6vectorIN11Ph ysLibrary12Poin tMassiveESaIS1_ EE
0x100842a4
St6vectorIN11Ph ysLibrary13Poin tMasslessESaIS1 _EE
0x100852bc
4 [main] PhysLibraryTest 3972 handle_exceptio ns: Exception:
STATUS_ACCESS_V IOLATION
604 [main] PhysLibraryTest 3972 open_stackdumpf ile: Dumping stack
trace to PhysLibraryTest .exe.stackdump

Jul 23 '05 #2
In message <cu***********@ cuce.ruk.cuni.c z>, David Komanek
<us**@domain.in valid> writes

Well, after the whole night by the comp I found the cause: missing copy
constructor. But the symptoms are evil, do not understand why compiler
did not detected it. What makes me even more frustrated is that copying
STL vector does not perform deep copying of compound objects,
That's up to the objects themselves. All the STL containers can do is
to call their copy constructor or assignment operator. It's your
responsibility to make them (and, therefore, their members, recursively)
do the necessary deep copy.
but this is probably my fault, too. I am seeking for some good guide
how to use containers for storing complex objects (not pointers to
them).
Ensure that they satisfy the requirements of CopyConstructib le and
Assignable.
Every help highly is appreciated :-)


--
Richard Herring
Jul 23 '05 #3
David Komanek wrote:

Well, after the whole night by the comp I found the cause: missing copy
constructor. But the symptoms are evil, do not understand why compiler
did not detected it.


Well. The thing is:
There is no copy constructor missing. If you don't write one on your
own, then the compiler will write one for you. But of course it can
be that this compiler generated one does the wrong thing. It does:
* copy construct all the base classes
* do a memberwise construction and initialzation from the passed object
(and thus is a shallow copy)
If this does not fit your needs (especially the second point), then you
will see such symptoms, and you have to write on on your own.

So in a nutshell: *Whenever* you write a class or add a member to a class,
you should check for:
do I have special needs for some of the members in the destructor ?
do I have special needs for some of the members in the copy constructor ?
do I have special needs for some of the members in the assignment operator ?

Now it can be that you really don't want a copy constructor. That can be eg. because
copy construction is costly process and you want to hinder yourself to do that.
In that case you can do: declare (but do not implement!) a copy constructor in
the private section of your class. This way you supress the compiler to generate
one of his own. But since it is private it cannot be used outside the class. And
even if you use it inside a class member function (which can access private members)
you will get a linker error, because it is not implemented.

Oh. BTW: There is the famous 'rule of three':
If you need one of:
destructor
copy constructor
assignment operator
you most likely need all 3 of them

--
Karl Heinz Buchegger
kb******@gascad .at
Jul 23 '05 #4

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

3
1878
by: Norman Evanson | last post by:
Hi Folks. Many thanks for the solution to this problem Given a templated class template <class T, int I> foo { /* ....*/ } how do I go about( if indeed it's possible) adding (for example) a foo<T,x> to a foo<T,y> where x!=y
3
1967
by: Ravi | last post by:
I have the following template definition template<class T> inline T& at(std::vector<T>& v, int pos) { #ifdef _MSC_VER return v.at(pos); #endif return v; }
3
1657
by: Steve | last post by:
Sorry to be stupid, but I've been poring over TC++PL, and according to what I see on page 351 (3rd ed.) the following code (split over three files) should work. Instead, when I try to compile with g++ 3.3.3 (cygwin special), I get the following error. What am I doing wrong? // the error: ----------------------------------------------- ~/cpplib/tests g++ template_test.cpp template_main.cpp...
7
2142
by: Lionel B | last post by:
Greetings. The following code compiles ok and does what I'd expect it to do: ---------- START CODE ---------- // test.cpp
2
1456
by: Stephen Starkie | last post by:
Hi, For a while I have had some problem understanding just how template specialisation works in certain cases. In abridged form my code looks like this; --MyTemplate.h-- #ifndef MyTemplateH #define MyTemplateH template <class Type>
4
3699
by: Chuck Chopp | last post by:
I have an application that I originally built using Visual Studio .NET 2003 as native C++ . This application includes a template class that was derived from the string class that's part of the C++ STL. Specifically, it extends the class with some additional methods to support changes in encoding as well as sprintf-style in-place formatting. Now, I've been asked to provide a version of the application running on Linux. I'm using GCC as...
1
3635
by: toton | last post by:
Hi, Is there any tool for template debugging, during compilation process ? Just like gdb or visual studio debugger do it during program execution, a step by step debug for the program, I am looking for something which will do the process during compilation and show what is getting substituted for which call. Otherwise sometimes, compilation fails saying some value_type can't be substituted by value_type& and etc inside a template call,...
2
5545
by: Dave Rudolf | last post by:
Hey all, So I have a template class whose only template parameter is a type that the class is to manage. It's actually an implementation of the common Singleton design pattern. The class looks like this: template< class Type > class Singleton
7
6487
by: nguyen.h.khanh | last post by:
Hi, I have this code #include <vector> #include <string> #include <iostream> using namespace std;
0
9799
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can effortlessly switch the default language on Windows 10 without reinstalling. I'll walk you through it. First, let's disable language synchronization. With a Microsoft account, language settings sync across devices. To prevent any complications,...
0
11173
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers, it seems that the internal comparison operator "<=>" tries to promote arguments from unsigned to signed. This is as boiled down as I can make it. Here is my compilation command: g++-12 -std=c++20 -Wnarrowing bit_field.cpp Here is the code in...
1
10878
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows Update option using the Control Panel or Settings app; it automatically checks for updates and installs any it finds, whether you like it or not. For most users, this new feature is actually very convenient. If you want to control the update process,...
0
9593
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing, and deployment—without human intervention. Imagine an AI that can take a project description, break it down, write the code, debug it, and then launch it, all on its own.... Now, this would greatly impact the work of software developers. The idea...
0
7143
by: conductexam | last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and then checking html paragraph one by one. At the time of converting from word file to html my equations which are in the word document file was convert into image. Globals.ThisAddIn.Application.ActiveDocument.Select();...
0
5810
by: TSSRALBI | last post by:
Hello I'm a network technician in training and I need your help. I am currently learning how to create and manage the different types of VPNs and I have a question about LAN-to-LAN VPNs. The last exercise I practiced was to create a LAN-to-LAN VPN between two Pfsense firewalls, by using IPSEC protocols. I succeeded, with both firewalls in the same network. But I'm wondering if it's possible to do the same thing, with 2 Pfsense firewalls...
0
6011
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
2
4239
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.
3
3245
bsmnconsultancy
by: bsmnconsultancy | last post by:
In today's digital era, a well-designed website is crucial for businesses looking to succeed. Whether you're a small business owner or a large corporation in Toronto, having a strong online presence can significantly impact your brand's success. BSMN Consultancy, a leader in Website Development in Toronto offers valuable insights into creating effective websites that not only look great but also perform exceptionally well. In this comprehensive...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.