Today in run into a memory problem in my project, with a class using c++ 11 initializer_list. The system signals a memory problem: "Expression _BLOCK_TYPE_IS_VALID(pHead->nBlockUse) in dbgdel.cpp. I simplified the code to a simple example, it no longer throws an expression but the problem becomes apparent from the debug output. In my eyes this code is correct, also it seems to work with g++.
#include <functional>
#include <memory>
#include <string>
#include <iostream>
#include <vector>
#include <map>
#include <sstream>
#include <initializer_list>
using namespace std;
class B {
public:
char data[256];
B(const string& x) {
cout << "Init " << this << endl;
}
B(const B& b) {
cout << "Copy " << this << endl;
}
~B() {
cout << "Deleting b " << this << endl;
}
};
class C {
public:
vector<B> bs;
C(initializer_list<B> bb) {
for(auto& b : bb) {
bs.push_back(b);
}
}
};
int main(int argc, char** argv) {
C bb { B("foo"), B("bar") };
return 0;
}
The output is:
Init 00B7FAE8 Init 00B7FBE8 Copy 00E108A0 Copy 00E10AE8 (?????) Deleting b 00E108A0 Copy 00E10BE8 Deleting b 00B7FBE8 Deleting b 00B7FAE8 Deleting b 00B7FAE8 (Deleted twice!)
What mistake I make here or is this not supposed to work?