Randomly occuring error DAMAGE: after Normal block

G

Guest

Hi Folks,

I got an error that drives me crazy because it only occurs sometimes and I
can`t even reproduce it.

I got a __gc class here is it`s header:

#pragma once

#include "../empLib/empImg.h"

namespace empDll
{
public __gc struct Image {
int x;
int y;
int z;
float data __gc[];
};

public __gc class Imager {
private:
char __nogc *_cfile;
empImg<float> __nogc *_img;
public:
Imager (System::String *filename);
~Imager(void);

int getDimX ();
int getDimY ();
int getDimZ ();

Image* load (int startPos, int count );
};

}

and here the code:

#include "Imager.h"
#include "../empLib/empDiscIO.h"

#using <mscorlib.dll>



using namespace System;

namespace empDll {
Imager::Imager (System::String *filename) {
try {
_cfile = new char [filename->Length];
memset (_cfile,0, strlen(_cfile));

for (int i=0; i < filename->Length; i++) {
_cfile = (char) filename->get_Chars(i);
}
_img = new empImg<float>;
} catch (empError e) {
e.msg();
}
}

Imager::~Imager(void) {
delete _img;
delete _cfile;
}

int Imager::getDimX () { return _img->dimx();}
int Imager::getDimY () { return _img->dimy();}
int Imager::getDimZ () { return _img->dimz();}

Image* Imager::load (int startPos, int count) {
try {
empDiscIO __nogc* imgIO = new empDiscIO(_cfile);
*_img = imgIO->load(_cfile, startPos, count).normalize(0,255);

Image *tmp = new Image;

tmp->data = new float __gc[_img->size()];
tmp->x = _img->dimx();
tmp->y = _img->dimy();
tmp->z = _img->dimz();

System::Runtime::InteropServices::Marshal::Copy(_img->data, tmp->data, 0
,_img->size());
delete imgIO;
return tmp;
} catch (empError e) {
e.msg();
} catch (...) {
cout << "ERROR";
}

}

}

I use this managed and __gc class from C# in the following way:

public emData(string filename)
{
empDll.Imager myIo = new empDll.Imager(filename);
empDll.Image temp = myIo.load(0,0);
m_dims = new int[3];
m_dims[0] = temp.x;
m_dims[1] = temp.y;
m_dims[2] = temp.z;

m_emData = new float[m_dims[0]*m_dims[1]*m_dims[2]];

long bytecount = m_dims[0]*m_dims[1]*m_dims[2];

for (long i = 0; i<bytecount; i++) m_emData = temp.data;

myIo = null;
}

When I use the emData Class in C# sometimes it simply works, sometimes i get
an null reference exception when accessing temp.x, temp.y or something else
and sometimes I get a very strange debugging error message:

DAMAGE: after Normal block (#114) at 0x060C5230

and everything crashes.

I assume that somehow memory allocated in the managed C++ DLL is not freeed
or something similar but I have no idea what I am doing wrong isn`t calling
delete enough in this case? Can someone please help me? Additionally I am
quite sure that I don`t have to loop through arrays and copy them value by
value I am just doing this to make sure this is not the problem here.

Thanks in Advance

Chucker
 
B

Bob Milton

Chucker,
That message occurs because some code wrote more bytes to an array than
the allocated length. In debug mode, the C++ allocator puts a distinctive
bit pattern both before and after the memory it returns to you. When that
memory is deleted, the code checks to see if those bit patterns are still
the same. If not, you get a message about damage before or after the memory
block.
Bob
Chucker said:
Hi Folks,

I got an error that drives me crazy because it only occurs sometimes and I
can`t even reproduce it.

I got a __gc class here is it`s header:

#pragma once

#include "../empLib/empImg.h"

namespace empDll
{
public __gc struct Image {
int x;
int y;
int z;
float data __gc[];
};

public __gc class Imager {
private:
char __nogc *_cfile;
empImg<float> __nogc *_img;
public:
Imager (System::String *filename);
~Imager(void);

int getDimX ();
int getDimY ();
int getDimZ ();

Image* load (int startPos, int count );
};

}

and here the code:

#include "Imager.h"
#include "../empLib/empDiscIO.h"

#using <mscorlib.dll>



using namespace System;

namespace empDll {
Imager::Imager (System::String *filename) {
try {
_cfile = new char [filename->Length];
memset (_cfile,0, strlen(_cfile));

for (int i=0; i < filename->Length; i++) {
_cfile = (char) filename->get_Chars(i);
}
_img = new empImg<float>;
} catch (empError e) {
e.msg();
}
}

Imager::~Imager(void) {
delete _img;
delete _cfile;
}

int Imager::getDimX () { return _img->dimx();}
int Imager::getDimY () { return _img->dimy();}
int Imager::getDimZ () { return _img->dimz();}

Image* Imager::load (int startPos, int count) {
try {
empDiscIO __nogc* imgIO = new empDiscIO(_cfile);
*_img = imgIO->load(_cfile, startPos, count).normalize(0,255);

Image *tmp = new Image;

tmp->data = new float __gc[_img->size()];
tmp->x = _img->dimx();
tmp->y = _img->dimy();
tmp->z = _img->dimz();

System::Runtime::InteropServices::Marshal::Copy(_img->data, tmp->data, 0
,_img->size());
delete imgIO;
return tmp;
} catch (empError e) {
e.msg();
} catch (...) {
cout << "ERROR";
}

}

}

I use this managed and __gc class from C# in the following way:

public emData(string filename)
{
empDll.Imager myIo = new empDll.Imager(filename);
empDll.Image temp = myIo.load(0,0);
m_dims = new int[3];
m_dims[0] = temp.x;
m_dims[1] = temp.y;
m_dims[2] = temp.z;

m_emData = new float[m_dims[0]*m_dims[1]*m_dims[2]];

long bytecount = m_dims[0]*m_dims[1]*m_dims[2];

for (long i = 0; i<bytecount; i++) m_emData = temp.data;

myIo = null;
}

When I use the emData Class in C# sometimes it simply works, sometimes i
get
an null reference exception when accessing temp.x, temp.y or something
else
and sometimes I get a very strange debugging error message:

DAMAGE: after Normal block (#114) at 0x060C5230

and everything crashes.

I assume that somehow memory allocated in the managed C++ DLL is not
freeed
or something similar but I have no idea what I am doing wrong isn`t
calling
delete enough in this case? Can someone please help me? Additionally I am
quite sure that I don`t have to loop through arrays and copy them value by
value I am just doing this to make sure this is not the problem here.

Thanks in Advance

Chucker
 
G

Guest

Hi Bob,

the only place where I think this can happen is here:

System::Runtime::InteropServices::Marshal::Copy(_img->data, tmp->data,
0,_img-size());

maybe

System::Runtime::InteropServices::Marshal::Copy(_img->data, tmp->data,
0,_img-size()-1);

might solve the problem, I will check this out. What if the native C++ Class
Library that I am wrapping here has an internal memory leak, is there any way
to deal with?

Thanks

Chucker

Bob Milton said:
Chucker,
That message occurs because some code wrote more bytes to an array than
the allocated length. In debug mode, the C++ allocator puts a distinctive
bit pattern both before and after the memory it returns to you. When that
memory is deleted, the code checks to see if those bit patterns are still
the same. If not, you get a message about damage before or after the memory
block.
Bob
Chucker said:
Hi Folks,

I got an error that drives me crazy because it only occurs sometimes and I
can`t even reproduce it.

I got a __gc class here is it`s header:

#pragma once

#include "../empLib/empImg.h"

namespace empDll
{
public __gc struct Image {
int x;
int y;
int z;
float data __gc[];
};

public __gc class Imager {
private:
char __nogc *_cfile;
empImg<float> __nogc *_img;
public:
Imager (System::String *filename);
~Imager(void);

int getDimX ();
int getDimY ();
int getDimZ ();

Image* load (int startPos, int count );
};

}

and here the code:

#include "Imager.h"
#include "../empLib/empDiscIO.h"

#using <mscorlib.dll>



using namespace System;

namespace empDll {
Imager::Imager (System::String *filename) {
try {
_cfile = new char [filename->Length];
memset (_cfile,0, strlen(_cfile));

for (int i=0; i < filename->Length; i++) {
_cfile = (char) filename->get_Chars(i);
}
_img = new empImg<float>;
} catch (empError e) {
e.msg();
}
}

Imager::~Imager(void) {
delete _img;
delete _cfile;
}

int Imager::getDimX () { return _img->dimx();}
int Imager::getDimY () { return _img->dimy();}
int Imager::getDimZ () { return _img->dimz();}

Image* Imager::load (int startPos, int count) {
try {
empDiscIO __nogc* imgIO = new empDiscIO(_cfile);
*_img = imgIO->load(_cfile, startPos, count).normalize(0,255);

Image *tmp = new Image;

tmp->data = new float __gc[_img->size()];
tmp->x = _img->dimx();
tmp->y = _img->dimy();
tmp->z = _img->dimz();

System::Runtime::InteropServices::Marshal::Copy(_img->data, tmp->data, 0
,_img->size());
delete imgIO;
return tmp;
} catch (empError e) {
e.msg();
} catch (...) {
cout << "ERROR";
}

}

}

I use this managed and __gc class from C# in the following way:

public emData(string filename)
{
empDll.Imager myIo = new empDll.Imager(filename);
empDll.Image temp = myIo.load(0,0);
m_dims = new int[3];
m_dims[0] = temp.x;
m_dims[1] = temp.y;
m_dims[2] = temp.z;

m_emData = new float[m_dims[0]*m_dims[1]*m_dims[2]];

long bytecount = m_dims[0]*m_dims[1]*m_dims[2];

for (long i = 0; i<bytecount; i++) m_emData = temp.data;

myIo = null;
}

When I use the emData Class in C# sometimes it simply works, sometimes i
get
an null reference exception when accessing temp.x, temp.y or something
else
and sometimes I get a very strange debugging error message:

DAMAGE: after Normal block (#114) at 0x060C5230

and everything crashes.

I assume that somehow memory allocated in the managed C++ DLL is not
freeed
or something similar but I have no idea what I am doing wrong isn`t
calling
delete enough in this case? Can someone please help me? Additionally I am
quite sure that I don`t have to loop through arrays and copy them value by
value I am just doing this to make sure this is not the problem here.

Thanks in Advance

Chucker

 
B

Bob Milton

A memory leak will NOT cause this error message! This is a data overrun,
and is a serious problem. Somewhere in the C++ DLL is a call to new which is
too small for the data being copied. BTW, this error message is for the C++
side, not the C# side of things.
Bob

Chucker said:
Hi Bob,

the only place where I think this can happen is here:

System::Runtime::InteropServices::Marshal::Copy(_img->data, tmp->data,
0,_img-size());

maybe

System::Runtime::InteropServices::Marshal::Copy(_img->data, tmp->data,
0,_img-size()-1);

might solve the problem, I will check this out. What if the native C++
Class
Library that I am wrapping here has an internal memory leak, is there any
way
to deal with?

Thanks

Chucker

Bob Milton said:
Chucker,
That message occurs because some code wrote more bytes to an array
than
the allocated length. In debug mode, the C++ allocator puts a distinctive
bit pattern both before and after the memory it returns to you. When that
memory is deleted, the code checks to see if those bit patterns are still
the same. If not, you get a message about damage before or after the
memory
block.
Bob
Chucker said:
Hi Folks,

I got an error that drives me crazy because it only occurs sometimes
and I
can`t even reproduce it.

I got a __gc class here is it`s header:

#pragma once

#include "../empLib/empImg.h"

namespace empDll
{
public __gc struct Image {
int x;
int y;
int z;
float data __gc[];
};

public __gc class Imager {
private:
char __nogc *_cfile;
empImg<float> __nogc *_img;
public:
Imager (System::String *filename);
~Imager(void);

int getDimX ();
int getDimY ();
int getDimZ ();

Image* load (int startPos, int count );
};

}

and here the code:

#include "Imager.h"
#include "../empLib/empDiscIO.h"

#using <mscorlib.dll>



using namespace System;

namespace empDll {
Imager::Imager (System::String *filename) {
try {
_cfile = new char [filename->Length];
memset (_cfile,0, strlen(_cfile));

for (int i=0; i < filename->Length; i++) {
_cfile = (char) filename->get_Chars(i);
}
_img = new empImg<float>;
} catch (empError e) {
e.msg();
}
}

Imager::~Imager(void) {
delete _img;
delete _cfile;
}

int Imager::getDimX () { return _img->dimx();}
int Imager::getDimY () { return _img->dimy();}
int Imager::getDimZ () { return _img->dimz();}

Image* Imager::load (int startPos, int count) {
try {
empDiscIO __nogc* imgIO = new empDiscIO(_cfile);
*_img = imgIO->load(_cfile, startPos, count).normalize(0,255);

Image *tmp = new Image;

tmp->data = new float __gc[_img->size()];
tmp->x = _img->dimx();
tmp->y = _img->dimy();
tmp->z = _img->dimz();

System::Runtime::InteropServices::Marshal::Copy(_img->data, tmp->data,
0
,_img->size());
delete imgIO;
return tmp;
} catch (empError e) {
e.msg();
} catch (...) {
cout << "ERROR";
}

}

}

I use this managed and __gc class from C# in the following way:

public emData(string filename)
{
empDll.Imager myIo = new empDll.Imager(filename);
empDll.Image temp = myIo.load(0,0);
m_dims = new int[3];
m_dims[0] = temp.x;
m_dims[1] = temp.y;
m_dims[2] = temp.z;

m_emData = new float[m_dims[0]*m_dims[1]*m_dims[2]];

long bytecount = m_dims[0]*m_dims[1]*m_dims[2];

for (long i = 0; i<bytecount; i++) m_emData = temp.data;

myIo = null;
}

When I use the emData Class in C# sometimes it simply works, sometimes
i
get
an null reference exception when accessing temp.x, temp.y or something
else
and sometimes I get a very strange debugging error message:

DAMAGE: after Normal block (#114) at 0x060C5230

and everything crashes.

I assume that somehow memory allocated in the managed C++ DLL is not
freeed
or something similar but I have no idea what I am doing wrong isn`t
calling
delete enough in this case? Can someone please help me? Additionally I
am
quite sure that I don`t have to loop through arrays and copy them value
by
value I am just doing this to make sure this is not the problem here.

Thanks in Advance

Chucker

 
G

Guest

I solved it! The problem was that the static lib linked to the managed DLL
was linked against the static runtime, the managed lib was linked against the
DLL Runtime and the C# Application was linked against CRT.

Now I rebuild everything and linked everything against the multithreaded
debug dll runtime and suddenly everything is fine!

Thanks for your efforts

Chucker
 
Top