Reputation: 11436
I wrote a C++ DLL using VS 2010 which is used to wrap some program that does boolean operations on polygons.Its structure is as follows: There is a main header and .cpp whose methods are exposed by marks :
__declspec(dllexport)
Then inside one of those method I execute a call of one of the methods of my polygon program which is contained in a different c++ class which is a part of the library.I should note that in addition to calling that method I also create(in the main dll .cpp file) variables which are defined in that polygons program.Those variables are of types required to pass and receive numeric data from the polygon program. Now ,what is wrong.When I run the an application that uses this dll I do two identical calls to the same method of the library -the one I explained above that calls a function of the polygon program.When I do those calls I pass some numeric data into the method and expect to get back a new data based on some calculations in the polygons program.First call returns the correct data but the second one returns kind of mix of the data from the first call and the second input data.I am not DLL expert but I took a look at this thread :
How do I use extern to share variables between source files?
And I wonder if that may be the source of the problem? As I mentioned I use some variables which are part of the polygon calculation class and I instantiate them in the main .cpp of the dll. Two of those variables take the input data from the user and get it into the method that calculates the result and the third variable is filled with the result returned by that function. Any help will be greatly appreciated as I am really stuck with it at the moment.
EDIT: The variables I use from the polygon source class are defined in its header as follows:
typedef std::vector< IntPoint > Polygon;
typedef std::vector< Polygon > Polygons;
DLL Test program
#include "stdafx.h"
#include "Clipper_lib.h"
int _tmain(int argc, _TCHAR* argv[])
{
int subj[]={100,100,200,100,200,200,100,200};
int clip[]={100,100,200,100,200,200,100,200};
vector<vector<int>> solution;
vector<vector<int>> solution1;
Execute(subj,clip,solution);
Execute(subj,clip,solution1);
return 0;
}
DLL header :
#include "clipper.hpp" /// this is the working C++ class of polygons operations
#include <vector>
using namespace ClipperLib;
using namespace std;
extern Polygon subj;
extern Polygon clip;
extern Polygons solution;
extern "C"
{
__declspec(dllexport) void Execute(int subj[],int clip[],vector<vector<int>> &solution);
}
DLL .cpp
// Clipper_lib.cpp : Defines the exported functions for the DLL application.
//
#include "Clipper_lib.h"
Clipper clipper;
void Execute(int subj[],int clip[],vector<vector<int>> &solution){
Polygon *subjP=new Polygon();
Polygon *clipP=new Polygon();
Polygons *solutionP=new Polygons();
for(int i=0; i<8;i+=2){
subjP->push_back(IntPoint(subj[i],subj[i+1]));
}
for(int b=0;b<8;b+=2){
clipP->push_back(IntPoint(clip[b],clip[b+1]));
}
clipper.Clear();
clipper.AddPolygon(*subjP,ptSubject);
clipper.AddPolygon(*clipP,ptClip);
clipper.Execute(ctIntersection,*solutionP);
for( vector<Polygon>::size_type d=0;d!=solutionP->size();++d){
vector<int> poly;
solution.push_back(poly);
for(vector<IntPoint>::size_type k=0;k!=solutionP[d].size();++k){
for(vector<IntPoint>::size_type s=0;s!=solutionP[d][k].size();++s){
int numX=(int)solutionP[d][k][s].X;
int numY=(int)solutionP[d][k][s].Y;
solution[d].push_back(numX);
solution[d].push_back(numY);
}
}
}
delete solutionP;
delete subjP;
delete clipP;
}
Upvotes: 1
Views: 187
Reputation: 11436
Well .Figured it out.Because the wrapper of this source was C based API I had to embrace all the C++ methods in the DLL with extern "C" .This way it works fine.Thanks for all the comments anyways .
Upvotes: 1
Reputation: 21251
Can you not debug your library call?
Eg. step through it in a debugger and try and see where the invalid values are getting set. Many IDE's let you set breakpoints that fire when data at a particular memory location changes. You could set a breakpoint on a value that you know gets set incorrectly on the second call, and it will show you what is changing it.
Upvotes: 0
Reputation: 60014
The easier thing to do: after you used data from Polygons call clear() on it. clear() it's a fast method that keep allocated memory (in case you fear for efficiency) but mark the content as unavailable. So you can fill Polygons with new data without problems. Inner vectors, i.e. objects of type Polygon, will clear on request, you can ignore these.
Upvotes: 0