I am working in a solution with projects of both VS2010 and VS2012.
The VS2010 project calls functions in VS2012 and vice-versa. This worked fine at first, but when I also needed to share variables between both projects I noticed variables doesn't seem to have the same memory alignments and each project interprets the same memory address differently.
Update: It only seems to occur when using STL-containers, other structs and classes which doesn't contain std:: works fine.
To illustrate the problem, the following code should yeld different results when run on different Visual Studio versions.
#include <string>
#include <vector>
int main()
{
int stringSize = sizeof(std::string); // Yelds 32 on VS2010, 28 on VS2012
int intVectorSize = sizeof(std::vector<int>); // Yelds 20 on VS2010, 16 on VS2012
return 0;
};
Updating both projects to the same version is not possible for me yet as I have a few dependencies tied to each version.
Does anyone know of a solution or a way to bypass the problem?
I will upgrade both project to the VS2012 compiler as soon as it's possible, but right now I'm hopping for a quick and dirty solution so I just can get along with working. Since it only seems to occur with STL-containers, is it perhaps possible to use an older version of the library on all projects? Or is it possible to fool the compiler? Perhaps changing the padding size?
Also, the first element in a std::vector seems to read fine, only subsequent elements in the vector seems to get scrambled. (See picture.)
Image of debugging the "Fetched" variable in "main.cpp" compiled in both 2010 and 2012.
Someone wanted me to clarify the way variables is being shared.
We are compiling the first project into a DLL in VS2012 compile mode and then trying to access that one in VS2010.
Here's some code to recreate the problem. If you would like to try for yourself you can download the full VS2012 solution here.
This code is compiled into a DLL using VS2012.
DllExport.h
#ifdef DLLHELL_EX
#define DLL_API __declspec(dllexport)
#else
#define DLL_API __declspec(dllimport)
#endif
#include <vector>
#include <string>
class DLL_API Foo
{
public:
Foo();
~Foo();
std::vector<std::string>* exposedMember;
};
DllExport.cpp
#include "DllExport.h"
Foo::Foo()
{
// Create member
exposedMember = new std::vector<std::string>();
// Fill member with juicy data
for(int i=0; i<5; i++)
exposedMember->push_back("Fishstick");
}
Foo::~Foo()
{
// Clean up behind ourselves like good lil' programmers
delete exposedMember;
}
This code uses the DLL and is compiled using VS2010.
main.cpp
#include "DllExport.h"
int main()
{
// Creating class from DLL
Foo bar;
// Fetching "exposedMember" from class
std::vector<std::string>* member = bar.exposedMember;
return 0;
}
The DLL was created, using this tutorial