Overlapping multidimensional array corruption [closed] - c++

Closed. This question needs debugging details. It is not currently accepting answers.
Edit the question to include desired behavior, a specific problem or error, and the shortest code necessary to reproduce the problem. This will help others answer the question.
Closed 1 year ago.
Improve this question
I have a very insidious issue with two dynamically allocated and reallocated multidimensional arrays, defined as follows:
int *pRawVals[20][181];
and
int *pMaxPlaneRawPlanes[20][46];
These are allocated in a single function: (code condensed for clarity)
...
// iNlat and iNVer bounds checked and OK
for (int lat = 0; lat < iNlat; lat++)
{
pData->pBlendedRawPlanes[lat] = new long long[1 * (iNver + 1)];
for (int sensor = 0; sensor < 20; sensor++)
{
pData->pRawVals[sensor][lat] = new int[1 * (iNver + 1)];
for (int vert = 0; vert <= iNver; vert++)
{
pData->pRawVals[sensor][lat][vert] = SENSOR_UNREAD;
} // end for
} // end for
} // end for
...
for (int sensor = 0; sensor < 20; sensor++)
{
delete[] pData->strayVals[sensor];
pData->strayVals[sensor] = new int[1 * (iNver + 1)];
for (int vert = 0; vert < iNver + 1; vert++)
{
pData->strayVals[sensor][vert] = SENSOR_UNREAD;
} // end for
for (int lat = 0; lat < 46; lat++)
{
pData->pMaxPlaneRawPlanes[sensor][lat] = new int[361];
// for (int iFillMem = 0; iFillMem < 361; iFillMem++)
for (int vert = 0; vert < iNver; vert++)
{
pData->pMaxPlaneRawPlanes[sensor][lat][vert] = SENSOR_UNREAD;
} // end for
} // end for
} // end for
... and deallocated in another function:
for (int lat = 0; lat < pData->iNUserLats; lat++) // iNUserLats <= 181
{
for (int sensor = 0; sensor < 20; sensor++)
{
if (pData->pRawVals[sensor][lat] != NULL)
{
delete[] pData->pRawVals[sensor][lat];
pData->pRawVals[sensor][lat] = NULL;
} // end if
} // end for
}
...
for (int sensor = 0; sensor < 20; sensor++)
{
for (int lat = 0; lat < 46; lat++)
{
if (pData->pMaxPlaneRawPlanes[sensor][lat] != NULL)
{
delete[] pData->pMaxPlaneRawPlanes[sensor][lat];
pData->pMaxPlaneRawPlanes[sensor][lat] = NULL;
} // end if
} // end for
} // end for
Hope I got enough of the code posted. At any rate, when deallocating pMaxPlaneRawPlanes on a second pass through the code, it turns out that certain elements of that array overlap elements of pRawVals and the deallocation causes a crash - appears that those locations may have been freed previously but I can't seem to point the finger at exactly how it's happening.
Any hints on how to narrow that kind of thing down? I run development on Windows 7 Ultimate 64 bit, but the app is compiled for 32 bit.
Thanks

You may go out of range in any of your array, or you may go out of range elsewhere.
If you are using Visual Studio 2019, or you are using gcc or clang, you can give ASan (Address Sanitizer) a try. (VS2019 link)
Otherwise you can try Application Verifier with Full Heap check enabled.
It is also possible to use custom allocation and allocate memory the way that it will access violation on out of bounds, even if off by one. It is essentially duplicating Application Verifier for particular allocations.
Using std::array or std::vector recommended in comments will also help making fewer errors that lead to heap corruption in the first place. vector manages deallocations automatically, and also in debug builds may have checks for out-of-range errors.

Related

Showing different windows 1 by 1

I have a problem with my code. I want to show different windows one by one by clicking the "Next" button to show the next one like this:
int i = 0;
do {
auto polyLines = new ExportPolyLines(nodeNames);
polyLines->show();
if (!polyLines->isVisible()) {
i++;
}
} while (i < 3);
The code compiled but did not work!
Can anyone help me about this?
As an improvement to your comment I would suggest that you fix the memory leak.
if (_logIsSelected)
{
for (int i = 0; i < _selectedWellLogs.size(); i++)
{
auto exportLogsData = new ExportsLogs(_selectedWellLogs[i], _wellLogsName[i], _folder);
exportLogsData->exec();
delete exportLogsData; // Free the memory that has been used for ExportLogs
exportLogsData = nullptr; // Not really necessary here, but good practice
}
}

C++ Segmentation fault: 11

I am getting Segmentation fault: 11 error when trying to run my program (I'm quite a n00b with c++ so take it easy on me). I know it has something to do with memory allocation but I'm not sure what exactly I am doing wrong. Can anyone please help and spot the problem/s?
Basically I'm trying to chop one vector into many small vectors, and analyse each one separately.
std::vector<double> test::getExactHit(std::vector<double> &hitBuffer, double threshold){
int resolution = 100;
int highestRMSBin = 0;
std::vector<double> exactHit(8192);
double* rmsInEachBin = new double[hitBuffer.size()/resolution];
double highestRMSValue = threshold;
for(int i = 0; i<hitBuffer.size()-resolution; i+=resolution){
std::vector<double>::const_iterator first = hitBuffer.begin() + i;
std::vector<double>::const_iterator last = hitBuffer.begin() + i + resolution;
std::vector<double> hitBufferBin(first, last);
rmsInEachBin[i/resolution] = calcRMS(hitBufferBin);
if(rmsInEachBin[i/resolution]>highestRMSValue){
highestRMSValue = rmsInEachBin[i/resolution];
highestRMSBin = i;
}
}
for(int j = 0 ; j < exactHit.size(); j++) {
exactHit[j]=hitBuffer[j+highestRMSBin];
}
return exactHit;
}
Please deallocate all the memory assigned using new or else it will cause memory leak and other bugs also might get introduced because of this .
http://cs.baylor.edu/~donahoo/tools/gdb/tutorial.html
You can debug using GDB , It will be handy to know a debugger if you are programming in C++ .
Hope this info will help you .

C++ Index variable changing radically in Debug Version [closed]

This question is unlikely to help any future visitors; it is only relevant to a small geographic area, a specific moment in time, or an extraordinarily narrow situation that is not generally applicable to the worldwide audience of the internet. For help making this question more broadly applicable, visit the help center.
Closed 9 years ago.
I have found a lot of answers that have to do with release version problems but none with the exact opposite.
I have a loop similar to the following:
while(index < 7 && FlagIsUp)
{
// process
Inner Loop
Inner Inner Loop
Array[index] = number;
++index;
}
Problem is that index changes radically from 6 (the last iteration) to 17209 for an int16_t and 1133165442 for size_t. NOTHING IN THE LOOP changes the index except the ++index. I replaced the while with a for and it still happens.
It only happens in debug mode, in release version it does finish without issues.
I also added volatile to the index and results were the same, it still overflowed.
Any ideas, pointers, would be appreciated. I can't provide a working copy of the bug so any theories are welcomed, I want to exhaust my options to find the problem.
EDIT:
Yes I'm sorry. I gave to little information.
First off I'm working with QNX Momentics Version: 4.6.0 and my debugger is part of the GNU Compiler Collection 4.3.3.
Now the inner loop is this:
cSignalNoIndex = 0;
while ((cSignalNoIndex < (2 * NO_PHASES + 1)) && !ShutDownFlag)
{
wSF0 = 0;
wExtSF = 0;
dwSFAcc = 0;
dwExtSFAcc = 0;
std::string SignalNo= " Waveform number " + Tool::toString(cSignalNoIndex);
Results[cSignalNoIndex].printWaveForm(SignalNo);
// Prepare Input vectors for FFT compute
cComponent = 0;
while (cComponent < (HCYCLE_SAMPLES << 1))
{
awReal[cComponent] = static_cast<int>(Results[cSignalNoIndex].WaveForm[cComponent/64][cComponent % 64]);
awImg[cComponent] = 0;
pwSource++;
cComponent++;
}
Results[cSignalNoIndex].printWaveForm(SignalNo);
// Get FFT (forward)
// Changed the wPwr from 7 to something else
iFft(&awReal[0], &awImg[0], wPwr, 1);
Results[cSignalNoIndex].printWaveForm(SignalNo);
// Compute magnitudes
//fMult = pInBlock3->fMult[cSignalNoIndex]; // Get Multiplier
fMult = 1;
for (cComponent = 0; cComponent < HCYCLE_SAMPLES && !ShutDownFlag; cComponent++)
{
int64_t dlOp = static_cast<int64_t>(awReal[cComponent]) * awReal[cComponent] + static_cast<int64_t>(awImg[cComponent]) * awImg[cComponent];
dlOp <<= 1; // Apply sqrt(2) term to result
dlOp = static_cast<int>(fMult * isqrt64(dlOp));
// Store into FFT object
oFFTMag3.wFFT[cSignalNoIndex][cComponent] = static_cast<int16_t>( dlOp );
// Set Base frequency magnitude and accumulate harmonics
if (cComponent == 1) // Base
{
wSF0 = dlOp;
if(cSignalNoIndex == 6)
{
wRefMagnitude = static_cast<int16_t> ( 0.4 * wSF0 );
}
if(awReal[1] != 0) // Also get phase for Base
{
dfPhase = std::atan((double)((float)awImg[1]/awReal[1])) * 180.0 / PI;
}
else
{
if(awImg[1] >= 0)
{
dfPhase = 90.0;
}
else
{
dfPhase = -90.0;
}
}
if(awReal[1] < 0) // convert to 2*PI range
{
dfPhase += 180.0;
}
else if(awImg[1] < 0)
{
dfPhase += 360.0;
}
//// THIS IS THE LINE
fPhase[cSignalNoIndex] = dfPhase; ////////// WTF! cSignalNoIndex = 6 - cComponent = 2
/// HERE cSignalNoIndex is overflown
}
}
You haven't really posted enough code but my best bet is Array[index] = number overwrites index at some point. The fact that it only happens sometimes (in your case, when debugging) is a good example of "undefined behavior".

Deleting double pointer causes heap corruption

I am using a double pointer but when I try to delete it it causes Heap Corruption: CRT detected that the application wrote to memory after end of heap. It "crashes" inside the destructor of the object:
Map::~Map()
{
for(int i = 0; i < mTilesLength; i++)
delete mTiles[i];
delete[] mTiles;
}
mTiles is declared something like this:
Tile **mTiles = NULL;
mTiles = new Tile *[mTilesLength];
for(int i = 0; i < mTilesLength; i++)
mTiles[i] = new Tile(...);
If notable mTiles is a object of "Tile" which inherits from a object "Sprite" all 3 destructors are set as virtual (map, tile, sprite), not sure if that makes any difference but seemed to work until now.
The code you posted does not seem to have any problems in it. I created a simple, self contained, compiling (and correct) example from it:
struct Tile {int x; Tile():x(7) {}};
struct Map {
Tile **mTiles;
int mTilesLength;
Map(int TilesLength_);
~Map();
};
Map::~Map()
{
for(int i = 0; i < mTilesLength; i++) {
delete mTiles[i];
}
delete[] mTiles;
}
Map::Map(int TilesLength_):
mTiles(),
mTilesLength(TilesLength_)
{
mTiles = new Tile *[mTilesLength];
for(int i = 0; i < mTilesLength; i++) {
mTiles[i] = new Tile();
}
}
int main() {
Map* m = new Map(1000);
delete m;
}
I compiled and ran it <- link, and nothing bad was noticed.
Your problem lies in code you have not shared with us. In order to find the code that is causing the problem and ask the right question, go here: http://sscce.org/
Then take your code and start trimming parts off it until the code is simple, yet still demonstrates your heap corruption. Keep copies of each version as you trim away irrelevant code so you don't skip over the part where the problem occurs (this is one of the many reasons you want a version control system even on your personal projects).

Using NaN value in MSChart gives Overflow Exception

I'm working in a real time ploting appication with MSChart...I need to set some y values to NaN but I'm getting an overflow exception. Here is the part of the code where it happen:
if (j_ecg < 2569)
{
for (int i = 0; i < 32; i++)
{
this.Invoke((MethodInvoker)delegate
{
ECG.Points.AddXY(puntos_ecg[j_ecg].X,puntos_ecg[j_ecg].Y);
});
j_ecg++;
}
}
else
{
for (int i = 0; i < 32; i++)
{
this.Invoke((MethodInvoker)delegate
{
ECG.Points[ecg_s].SetValueY(puntos_ecg[j_ecg].Y);
for (int j = 1; j < 10; j++){ ECG.Points[ecg_s + j].SetValueY(double.NaN); }
});
j_ecg++;
ecg_s++;
if (ecg_s == 2560) { ecg_s = 0; }
}
}
The Invokes are there to avoid cross threads issues.
Any idea of how can I do it for not getting the exception? I've try using unchecked keyword just before the SetValueY call but nothing changes.
First thing to try is disabling autoscaling so that min/max don't need to be calculated:
chart1.ChartAreas["Default"].AxisY.Minimum = <your min>;
chart1.ChartAreas["Default"].AxisY.Maximum = <your max>;
However you still need to have at least one real value in one of your series.
I recommend you keep track of your displayed values. If everything is NaN, stop plotting! When the next valid value comes in resume plotting.
Note: using zeroes instead of NaN is another solution.