Jump to content
  • Advertisement
Sign in to follow this  
MadMax1992

Program wrote to memory after end of heap buffer

This topic is 3500 days old which is more than the 365 day threshold we allow for new replies. Please post a new topic.

If you intended to correct an error in the post then please contact us.

Recommended Posts

The title of the thread is the error I'm getting in my program, to be specific:
Debug Error!

Program: location/program.exe

HEAP CORRUPTION DETECTED: after Normal block (#169) at 0x007F1c48
CRT detected that the application wrote to memory after end of heap buffer
The error occurs at the end of my main function. I can't post code, because it's simply too complex for anybody to understand at it's current (undocumented) state. A lot of dynamic allocation and freeing is happening in the background, but I'm almost certain I freed up all the memory i dynamicly allocated... What could cause this error, and what does it mean?

Share this post


Link to post
Share on other sites
Advertisement
Okay, quick reply from myself, I've managed to track down the problem to the following:

float **m_fOutputs //is a class private member

Class::Class(unsigned int nNumVariants, unsigned int nNumOutputs)
{
m_fOutputs = new float*[nNumVariants];

for (unsigned int i = 0; i < nNumVariants; i++) {
m_fOutputs = new float[nNumOutputs];
}
}

Class::~Class()
{
//the following code gives errors when the constructor was
//called with nNumOutputs = 1
for (unsigned int i = 0; i < nNumVariants; i++) {
delete [] m_fOutputs;
}

delete [] m_fOutputs;
}


I tried the following to eliminate the problem, but I still get the same error message

Class::~Class()
{
for (unsigned int i = 0; i < nNumVariants; i++) {
if (nNumOutputs > 1) {
delete [] m_fOutputs;
} else {
delete m_fOutputs;
}
}

delete [] m_fOutputs;
}


but the error is still there.

Somebody knows how to solve this?

Share this post


Link to post
Share on other sites
Its pretty easy, if you rely on the well written standard library to manage your memory for you you can avoid such bugs.

template <class T>
class array2d
{
public:
array2d(int width, int height)
:
width(width),
data(width * height)
{
}

int width() const { return width; }
int height() const { return data.size() / width; }

T &operator() (int x, int y) { return data[index(x,y)]; }

const T &operator() (int x, int y) const { return data[index(x,y)]; }

private:
int index(int x, int y)
{
return y * width + x;
}

int width;
std::vector<T> data;
};

class Class
{
Class(unsigned numVariants, unsigned numOutputs)
:
outputs(numVariants, numOutputs)
{
}

void frobnicate()
{
// use outputs(x,y)
}

private:
array2d<float> outputs;
};


The array2d is just for illustration. You could use a premade version, like boost.multi_array.

Share this post


Link to post
Share on other sites
what an idiotic idea, turning a 2d map into 1d memory blocks! It would make my life so much easier and less complicated!

Thank you very much, stupid from me to not think of a solution like this one! Thank you for the quick response and the template!

Share this post


Link to post
Share on other sites
Sign in to follow this  

  • Advertisement
×

Important Information

By using GameDev.net, you agree to our community Guidelines, Terms of Use, and Privacy Policy.

We are the game development community.

Whether you are an indie, hobbyist, AAA developer, or just trying to learn, GameDev.net is the place for you to learn, share, and connect with the games industry. Learn more About Us or sign up!

Sign me up!