STL vector corruption in VS projects

I have a Visual Studio 2005 solution with several projects that are built independently of each other. The main project statically links other projects. I get a very strange vector STL corruption in one of these statically linked libraries. For example, I declare std :: vector and then do sort( thatVector.begin(), thatVector.end() ) , but when I debug it and look at the disassembly, I see the following:

 std::vector<SomeOtherClass<SomeOtherTemplateType>,std::allocator<SomeOtherClass<SomeOtherTemplateType> > >::begin 

It is incredibly strange that SomeOtherClass and SomeOtherTemplate declared in the main project, so this library should not know anything at all.

I tried to freeze all the other threads, thinking that maybe one of them distorted thatVector , but not the cubes. I have a complete loss. Has anyone experienced something like this?

Compile information: - main program / Zi, user optimization (mainly debug version) - static library / Zi, / Od

Link Information: / DEBUG

+4
source share
2 answers

The problem is that the library and the program were compiled with various compiler options. As a result, you have a different implementation of iterators, but with the same signature. This is a known issue , and Microsoft recommends compiling several versions of statically linked libraries and linking the executable to the corresponding one.

+5
source

Vector is a template type, meaning that every piece of code that refers to a vector must know the full type. Not only is your Knwo library about SomeOtherClass and SomeOtherTemplateType, they need to know about them in order to reference their vector.

In this case, the full type of your vector:

 std::vector<SomeOtherClass<SomeOtherTemplateType>,std::allocator<SomeOtherClass<SomeOtherTemplateType> > > 

... which is probably declared in your code something like this:

 vector<SomeOtherClass<SomeOtherTemplateType> > thatVector; 

... with a dispenser allowing the default template argument.

Now, to your problem with corruption. There is little information about the nature of corruption, so I am going to make a few assumptions. Nameley, that you select a vector in one module and try to do something with it (for example, push_back in to it) in another module, and that this is when you do something in another module, when the damage actually occurs. Disappointingly, in many of these cases, corruption is not detected or reported when corruption occurs. This is often discovered much later in completely unrelated codes.

If the above assumptions are true, I have 2 suggestions regarding a possible reason:

  • Modules are not associated with the same version and taste of runtime libraries. Try to make sure that each module is connected to the same CRT (for example, multi-threaded Debug DLL for Windows) and try again. In most cases this is a problem.

  • Different compilers (or different versions of the same compiler) are used to create different modules. In this case, the vector looks like one for one module and looks somehow different for another module. There are many threads that foolishly discuss this issue; see here for one example.

0
source

Source: https://habr.com/ru/post/1303036/


All Articles