As the title says, I got a problem with the same value assigned by two different constructors to two unrelated variables in my program.
This is the first part:
template<typename N>
N* readMany( const json::Value& jsonSource, std::size_t size ) {
N* result = new N[size];
//check code
std::cout << "result: " << result << std::endl;
std::cout << std::flush;
for( size_t i = 0; i < size; i++ ) {
auto number = jsonSource.get( i, false );
result[i] = static_cast<N>( number.asFloat() );
}
return result;
}
And this is the second part:
Value::Value() {
element = new JsonNull();
//check code
std::cout << "Constructor call: " << element << std::endl;
std::cout << std::flush;
}
And this is what is printed to stdout:
Constructor call: 0x55dd64bfddb0
Constructor call: 0x55dd64c23040
Constructor call: 0x55dd64c23060
Constructor call: 0x55dd64c23080
Constructor call: 0x55dd64c23040
Constructor call: 0x55dd64c23060
Constructor call: 0x55dd64c23080
result: 0x55dd64c23060 <-- HERE
Constructor call: 0x55dd64c23080
Constructor call: 0x55dd64c23060 <-- AND HERE
Constructor call: 0x55dd64c23040
As you can see in two parts of my code the variables got the same values. And because these values are deleted in two other parts of my code in destructors, I'm getting SEGFAULT for double freeing of my memory.
I's using a debugger and then the above print code and got the same results. I don't know what may be the issue. I can't even post any sample code for You to test, because I can't isolate the problem in a sample. The above findings are all what I got.
EDIT:
These are destructors:
Value::~Value() {
delete element;
}
NumberArray::~NumberArray() {
delete[] array;
}