_CrtIsValidHeapPointer(pUserData)
i had problems when using char arrays , when closing my app ther was a "DAMAGE: after normal block" exeption.
i changed everything to std::string and now i get , when closing the app, an error Expression : _CrtIsValidHeapPointer(pUserData)
my only remaining char * are things like char * FileName (which i think are ok)
what could cause this ?
whats the solution?
thank you
[395 byte] By [
urika] at [2007-11-18 15:11:07]

# 1 Re: _CrtIsValidHeapPointer(pUserData)
Originally posted by urika
i had problems when using char arrays , when closing my app ther was a "DAMAGE: after normal block" exeption.
i changed everything to std::string and now i get , when closing the app, an error
my only remaining char * are things like char * FileName (which i think are ok)
what could cause this ?
whats the solution?
thank you Show us how you are initializing and using char *FileName.
Regards,
Paul McKenzie
# 2 Re: _CrtIsValidHeapPointer(pUserData)
i found out whats wrong!
it happens when idelete m_StrInfo;
where m_StrInfo is SStrInfoData* m_StrInfo;
and SStrInfoData is struct SStrInfoData
{
std::string time;
std::string Vx;
std::string h;
std::string comment;
};
if i cooment the delete line its ok but i dont understand why it happens (probably it was the cause of the DAMAGE:... error as well)
does anyone know why ?
urika at 2007-11-11 2:01:06 >

# 3 Re: _CrtIsValidHeapPointer(pUserData)
Its probably something silly like a double deletion or even an attempted deletion of a stack object. Why dont you convert from bare pointers to a smart pointer like std::auto_ptr or Boost::shared_ptr to manage your structure.
# 4 Re: _CrtIsValidHeapPointer(pUserData)
Originally posted by urika
i found out whats wrong!
it happens when idelete m_StrInfo;
where m_StrInfo is SStrInfoData* m_StrInfo;
and SStrInfoData is struct SStrInfoData
{
std::string time;
std::string Vx;
std::string h;
std::string comment;
};
if i cooment the delete line its ok but i dont understand why it happens (probably it was the cause of the DAMAGE:... error as well)
does anyone know why ? Where do you do:
m_StrInfo = new SStrInfoData;
?
If you didn't call operator new, then the call to operator delete is illegal.
If you did call operator new, then for each call to opreator new must be matched with a call to operator delete. In other words, two calls to new must have two (and only two) calls to delete.
If you did match calls to new and delete correctly then you haven't solved your problem. What you did was only cover up a problem that exists elsewhere, and depending on conditions, will crop up again.
Regards,
Paul McKenzie
# 5 Re: _CrtIsValidHeapPointer(pUserData)
If you did match calls to new and delete correctly then you haven't solved your problem. What you did was only cover up a problem that exists elsewhere, and depending on conditions, will crop up again.
this is the case.
however im deleting 2 other arrays , but they include floats and ints and there is no problem with that.
if i try to do new SStrInfoData[buffsize];
globaly (like the other arrays)
i get:
error C2390: '$S25' : incorrect storage class 'auto'
which i dont understand
so what could be the cause of the problem?
urika at 2007-11-11 2:04:07 >

# 6 Re: _CrtIsValidHeapPointer(pUserData)
new returns a pointer. You must not lose that pointer. Can you post a small subset of your work that shows the problem. Post an example of you declaring/using/destroying a dynamic array of your structures.
# 7 Re: _CrtIsValidHeapPointer(pUserData)
this how i declare the other 2 arrays (global)
SInfoData* m_Info=new SInfoData[buffsize];
SInfoData* m_TempArray=new SInfoData[buffsize];
since i am reading info , where i dont know how long its going to be i am doing this:
void EnlargeArrays()
{
int i;
buffsize*=2;
for (i=0;i<ArrayIndex;i++)
{
m_TempArray[i]=m_Info[i];
}
delete m_Info;
m_Info=new SInfoData[buffsize];
for (i=0;i<ArrayIndex;i++)
{
m_Info[i]=m_TempArray[i];
}
delete m_TempArray;
m_TempArray=new SInfoData[buffsize];
}
then i use the array that makes the trouble:
void ProcessData()
{
// SStrInfoData tmp;
m_StrInfo=new SStrInfoData[buffsize];
for (int i=0;i<ArrayIndex;i++)
{
m_StrInfo[i].time=toString(m_Info[i].time);
m_StrInfo[i].Vx=toString(m_Info[i].Vx);
m_StrInfo[i].h=toString(m_Info[i].h);
m_StrInfo[i].comment=(" ");
if(m_Info[i].comment!=0) // HERE PUT A SWITCH ACCORDING TO COMMENT
{
m_StrInfo[i].comment.erase();
m_StrInfo[i].comment=("Comment ");
}
}
}
later when closing the app in DeInit()
i do delete m_Info;
delete m_TempArray;
// delete m_StrInfo;
if its uncommented there is the error.
i hope this helps.
thanks
urika at 2007-11-11 2:06:17 >

# 8 Re: _CrtIsValidHeapPointer(pUserData)
First, you used operator new[], therefore you use operator delete[] -- note the emphasis. You are missing the [], therefore you are not destroying the elements properly. Mixing the array new with the non-array delete leads to undefined behavior.
Allocate with new, deallocate with delete
Allocate with new[], deallocate with delete[].
Once you change this, then try again. Even if it doesn't solve your problem, you still need to make this change.
Second, your EnlargeArrays function looks suspicious in that I don't know why you need to make m_TempArray global -- it should be a local pointer. The way you enlarge an array is the following:
#include <algorithm>
void Enlarge()
{
// Assume that m_Data is the array
// and buffsize is a global holding the size of the array
SInfoArray *pTemp = new SInfoArray[buffsize * 2];
std::copy(m_Data, m_Data + buffsize, pTemp );
delete [] m_Data;
m_Data = pTemp;
buffsize *= 2;
}
Having said all of this, I personally, and most experienced C++ programmers wouldn't have written what I wrote above, or would even have a function called EnlargeArrays. Remember that you are using C++. Code to resize arrays as you have written is unnecessary. C++ has a std::vector class that takes care of all of these details.
Here is your code rewritten using std::vector.
#include <vector>
//...
typedef std::vector<SInfoData> SInfoArray;
//...
SInfoArray m_Info;
//...resize m_Info
//...
void EnlargeArrays()
{
m_Info.resize(m.size()*2);
}
void DeInit()
{
// Nothing to do
}
I've reduced your code, to one line. In that one line, it did all you tried to do, but the difference is that vector does it correctly and efficiently.
Also, there is no cleanup for you to do at the end, since vector will remove the elements on destruction. And to top it off, std::vector has an overloaded operator [] that works exactly as if you were using an array, so that most of your code stays intact when you switch from arrays to vectors.
I suggest you get a few books on the various C++ classes that would make your work much easier. Maybe "Accelerated C++" by Koenig & Moo is a good start, with an indispensible reference book such as "The C++ Standard Library" by Josuttis as another. There are also articles in dev-archive discussing vector.
When your application is a Win32 app, this does not mean that you have to resort to "old" C++ coding. The C++ library has a lot to offer, such as vector, map, list, etc. -- basically all the stuff you had in MFC, but without any added libraries, and IMO, superior.
Regards,
Paul McKenzie
# 9 Re: _CrtIsValidHeapPointer(pUserData)
thank u is an understatement
:D
by the way it works with delete[] !!
urika at 2007-11-11 2:08:15 >
