Release vs Debug version: suddenly runtime error - c++

I have a program that performs an FFT on a 2d array. In order to work with the fft library fftw3 I have to use a temporary array (called FTtemp) that reads out the result of the FFT: it is 3d since it contains the x & y axis plus the real and imaginary value for each (x,y) tupel.
The transfer of the data from the FFT array (which has a special variable type) to the ordinary array is working in debug mode but not in release. In release I get the following runtime error: Access violation writing location 0x02913000.
From my google search I found that release version bugs are usually related to uninitialized objects. This led me to explicitly initialise every item in FTtemp with 0.0, however to no avail. Furthermore, I printed the FFt array items to console and numbers appeared which means that they are also initialised. Hence, I am a bit out of ideas and wondered if someone might be wiser than me?
Here is the code snippet I am talking about. Since the program relies on a lot of other things, I was not quite able to recreate a minimal example yet, but I will add one as soon as I got the same error.
Fun fact: I print the I & j values of the loop to the console for trouble shooting and it is another (I,j) tupel where it crashes every time when I run it: eg: 49,212 or 116,169. I am really confused by this.
FTtemp = new double** [width];
for (i = 0; i < width; i++) {
FTtemp[i] = new double*[height];
}
for ( i = 0; i < width; i++)
{
for (j = 0; j < height; j++) {
FTtemp[i][j] = new double[2];
FTtemp[i][j][0] = 0.0;
FTtemp[i][j][1] = 0.0;
}
}
cout << "width,height: " << width << "," << height << endl;
for (i = 0; i < width; i++)
{
for (j = 0; j < height; j++) {
/*
cout << "access to out: " << out[indexFFT(i, j)][0] << endl;
cout << "access to FTtemp: " << FTtemp[i][j][1] << endl;
*/
cout << "i,j is: " << i << "," << j << endl;
FTtemp[i][j][1] = out[indexFFT(i, j)][0]; <--------- error occours here
FTtemp[i][j][2] = out[indexFFT(i, j)][1];
}
}
Thank you for your consideration.
All the best,
Blue

There is an error in this line:
FTtemp[i][j][2] = out[indexFFT(i, j)][1];
Notice that FTtemp[i][j] is initialized to new double[2] earlier in your code, which means that FTtemp[i][j][2] is an out-of-bounds write.
There may be other issues here - perhaps indexFFT(i, j) gives a bad index? - but without seeing how out was initialized or how indexFFT works we can't be sure.
Hope this helps!

Related

SIGSEGV through writing to local variable

I'm currently encountering the most strange behavior in my C++ project. What I'm trying to do is calculate the euclidean distance between two double vectors (well, actually, vectors of double vectors, hence the m_Data[0].size()).
This is the source:
double NEAT::Behavior::Distance_To(NEAT::PhenotypeBehavior* other)
{
double sum = 0.0;
for (int i = 0; i < m_Data[0].size() && i < other->m_Data[0].size(); i++) {
double x1 = m_Data[0][i];
double x2 = b->m_Data[0][i];
double difference = x1 - x2;
difference *= difference;
sum += difference;
}
return sqrt(sum);
}
I initially had all this written in one line, but I've split it up to locate the error. What happens is that after a few thousand calls to this function, it throws a SIGSEGV at the last line of the for loop:
sum += difference;
I have NO idea how this could happen. I've checked the stack trace, it's from the Distance_To(...) function and it gets thrown at this line precisely. As soon as I comment it out, everything's fine (but of course the function won't work lol). The signal gets thrown at the same time each time I run the program with the same objects interacting.
Help would be much appreciated. Thanks!
Edit: I've verified the integrity of the pointers in this method by printing out the needed values before entering the loop. All values get printed correctly. Here is the complete version of the function I used for debugging purposes:
double NEAT::Behavior::Distance_To(NEAT::PhenotypeBehavior* other)
{
double sum = 0.0;
Behavior* b = (Behavior*) other;
// Gets executed without any problems
if (genomeid == 300 && b->genomeid == 399) {
std::cout << "PROBLEM CASE" << std::endl;
std::cout << "Printing values for 300..." << std::endl;
for (int i = 0; i < m_Data[0].size(); i++) std::cout << m_Data[0][i] << std::endl;
std::cout << "Printing values for 399..." << std::endl;
for (int i = 0; i < m_Data[0].size(); i++) std::cout << b->m_Data[0][i] << std::endl;
}
// Doesn't get executed
if (m_Data[0].size() != other->m_Data[0].size()) {
std::cout << "Different sizes, " << m_Data[0].size() << " and " << b->m_Data[0].size() << std::endl;
}
// SIGSEGV at size() call
for (int i = 0; i < m_Data[0].size() && i < b->m_Data[0].size(); i++) {
double x1 = m_Data[0][i];
double x2 = b->m_Data[0][i];
double difference = x1 - x2;
difference *= difference;
// If this line gets commented out, no SIGSEGV but the program starts behaving weirdly afterwards (suddenly different sizes after the faulty run)
sum += difference;
}
return sqrt(sum);
}
ASAN and valgrind are the tools you should use to identify the root cause of this type of errors. Eventhough the error thrown at line sum += difference, your actual error could be somewhere else before hitting this point which corrupts your memory. These tools will help you to track that.
Sorry guys, I missed out on some MultiNEAT framework functions I should've used but didn't for initializing the objects etc. Anyways, thanks a lot to all of you, I learned a lot about using valgrind and ASAN (both are really handy and I didn't know about either of them before! lol) and even got a few good articles to read. Duh!

Double vector hides variables

I'm having an interesting problem when I'm accessing a double vector. The idea is that I have deleted all information prior to accessing the vector. A for loop tries to access the vector and successful says that the vector is empty, but when I access the vector point directly it shows that there are variables still in the vector.
Also, the vector was set up like so:
vector<vector<string>> proTable;
Here is the loop attempting to access the vector.
for(int a = 0; a < proTable.size(); a++)
{
for(int b = 0; b < proTable[a].size(); b++)
{
cout << proTable[a][b] << "\t";
}
}
But if I edit the for loop this way it returns the variable inside.
for(int a = 0; a < proTable.size(); a++)
{
for(int b = 0; b < proTable[a].size(); b++)
{
cout << proTable[a][b] << "\t";
}
cout << proTable[0][0];
}
The first prints nothing out. The second prints X which was in the vector before. Also, the vector does not show that it is empty.
This is how I was deleting it if it matters.
void MRelation::RemoveColumn(vector<int> rem)
{
while(!rem.empty())
{
int z = rem[rem.size() - 1];
for(int a = 0; a < proTable.size(); a++)
{
for(int b = z; b < proTable[a].size() - 1; b++)
{
proTable[a][b] = proTable[a][b+1];
}
proTable[a].pop_back();
}
rem.pop_back();
}
}
The vector rem holds the columns that need to be deleted from the table.
I have deleted all information prior to accessing the vector.
Accessing an vector out of bounds has undefined behaviour. Since your vector is empty, proTable[0] is out of bounds. In the line cout << proTable[0][0];, you access proTable[0]. Therefore the behaviour of your program is undefined.
it shows that there are variables still in the vector.
You cannot jump to such conclusion from observing undefined behaviour.
"There are variables still in the vector" was not necessarily the reason why you saw output. You saw output because the behaviour was undefined.
I found out what it was. I you delete the contents of the inner vector but don't delete the vectors themselves then the vector will think that it contains something still and will pull out information that doesn't exist anymore. here is the code I was have problems with. It has been edited with an if statement to correct it.
void MRelation::FinalPrint()
{
if(curTable.size() < 2)
{
ss << "? No\n";
}
else
{
ss << "? Yes(" << curTable.size() - 1 << ")\n";
}
if(!proTable[0].empty()) //This was added in after to correct the problem
{
for(int c = 1; c < proTable.size(); c++)
{
ss << " " << proTable[0][0] << "=" << proTable[c][0];
for(int d = 1; d < proTable[0].size(); d++)
{
ss << ", ";
ss << proTable[0][d] << "=" << proTable[c][d];
}
ss << "\n";
}
}
}
Sorry about not putting everything in context before. I was trying to put in as much relavant information without putting in 300 lines of code.

Unresolved identifiers for C++ in Eclipse Mars IDE

Receiving debugger notifications in the IDE; however the project compiles and runs fine. I have added flags for X0 in an attempt to remedy the problem (thinking debugger software might be pre-c++11), but that has not remedied the issue. I'm experiencing the issue with arrays directly, but have seen it occur with other data types. I have tried refreshing the index in the project and "freshening" the files. I'm also receiving an odd 'invalid overload of endl' notification. Code below is just for experimenting with the IDE. Errors located at every line with an array class function call and the cout statements.
Any thoughts?
#include <iostream>
#include <array>
using namespace std;
int main()
{
array <double, 5> rainfall;
rainfall[0] = 2.3;
rainfall[1] = 0.3;
rainfall[2] = 0.0;
rainfall[3] = 4.1;
rainfall[4] = 0.5;
rainfall.at(5) = 7.2;
for(size_t x = 0; x < rainfall.size(); x++)
{
cout << rainfall[x] << endl;
}
for(size_t x = 0; x < rainfall.size(); x++)
{
cout << "Please enter a rainfall amount: " << endl;
cin >> rainfall[x];
}
for(size_t x = 0; x < rainfall.size(); x++)
{
cout << rainfall[x] << endl;
}
return 0;
}
See this answer: https://stackoverflow.com/a/22001437/1274747
By default the indexer parser does not have C++11 enabled, so to use the C++11 functionality and classes, the -std=c++11 parameter needs to be added to the index parser command line.
You can also check the other answers in the thread, but this one was working in my case.

Finding this strange bug? It crashes without noticing anything

I wrote a function within my code that should create some sort of matrices. It is fine when the size is small, but when it gets bigger, it crashes at the middle of this function without giving any information. I did that with both debug and release mode and same thing happened. Any idea on what can be wrong? Someone suggested me it could be buffer overrun.
In this function when kl.mechelms get bigger than a certain number, it crashes. The following code uses a function and gets a 3 by 3 matrix and stores it in kl.scoff which size is [3][3][kl.mechelms][kl.mechelms]. The problem happens when kl.mechelms are like bigger than 7000, but we need far more than that for our code.
Could the function Calc_3D which I use within this part cause the problem? I think it shouldn't since it just reads some values.
for (int z = 0;z<3;z++) {
for (int q = 0;q<3;q++) {
kl.scofsarr[z][q] = new double *[kl.mechelms];
}
}
for (int i = 0;i<kl.mechelms;i++) {
cout << "element " << i << "of " << kl.mechelms << endl;
kl.comments << "element " << i << "of " << kl.mechelms << endl;
for (int z = 0;z<3;z++) {
for (int q = 0;q<3;q++) {
kl.scofsarr[z][q][i] = new double[kl.mechelms];
}
}
for (int j = 0;j<kl.mechelms;j++) {
Calc_3D(i,j, kl.elmx[j], kl.elmy[j], kl.elmz[j], kl.anglemat[j], kl.dip[j], kl.elmx[i],kl.elmy[i],kl.elmz[i],
kl.elma[i],kl.rectza,kl.anglemat[i],kl.dip[i], kl.G, kl.v, kl.scofs, kl.rdepth);
for (int z = 0;z<3;z++) {
for (int q = 0;q<3;q++) {
kl.scofsarr[z][q][i][j] = kl.scofs[z][q];
}
}
}
}

CPP: Mysterious error for array initialization and crash?

My program seems to always produce ridiculous errors.
Please provide directions for me. The following code segment cutout all irrelevant parts.
Thanks.
Part A of the code segment seems failed to initialize the array correctly, how to debug?
Part B of the code segment always crash, is there anything i missed?
typedef unsigned long T_PSIZE;
int main()
{
int AG_TOTAL = 6 ;
/* part A1 */
T_PSIZE* cntPeopleByAge = new T_PSIZE[AG_TOTAL + 1];
/* part A2 - originally i use static array like this, but it also fails */
//T_PSIZE cntPeopleByAge T_PSIZE[AG_TOTAL + 1];
for (int i = 0; i < (AG_TOTAL + 1); i++)
{
std::cout << i << ":" << cntPeopleByAge[i] << "\t";
cntPeopleByAge[i] = 0;
std::cout << cntPeopleByAge[i] << "\n";
}
std::cout << "cntPeopleByAge:" << cntPeopleByAge[ AG_TOTAL + 1 ] << "\n";
/* part B */
delete [] cntPeopleByAge;
return 0; // <--- crash here!
}
Sample Output
0:200320 0
1:201581 0
2:201582 0
3:201583 0
4:0 0
5:0 0
cntPeopleByAge:1799119387:0:0
Platform: win 7 x64
Compiler: TDM-GCC x64
for (int i = 0; i < (AG_TOTAL + 1); i++)
{
std::cout << i << ":" << cntPeopleByAge[i] << "\t";
// ^^^^^^^^^^^^^^^^
// You're reading uninitialized memory here
cntPeopleByAge[i] = 0;
std::cout << cntPeopleByAge[i] << "\n";
}
And here
std::cout << "cntPeopleByAge:" << cntPeopleByAge[ AG_TOTAL + 1 ] << "\n";
you're going out of bounds. The last valid index is AG_TOTAL.
You've got undefined behaviour (UB). The errors are only as ridiculous as UB can be.
/* Sorry. but that earlier answer is not correct. The loop correctly starts at zero and ends at < the limit. The problem is that you are declaring an array of pointers, but never allocating memory to the objects that they point to. Your output is showing you addresses not numbers. One way is to allocate the objects as you use them (you must delete them individually as well) */
T_PSIZE* cntPeopleByAge = new T_PSIZE[AG_TOTAL + 1];
for (int i = 0; i < (AG_TOTAL + 1); i++)
{
cntPeopleByAge[i] = new T_PSIZE();
}
What you really want to use is the vector class in the standard library which handles all of this for you:
#include <vector>
std:vector<T_PSIZE *> cntPeopleByAge;
cntPeopleByAgex.resize(AG_TOTAL + 1);
Good luck ...