I meet a memory leak problem and the minimum code as follows. My compiler is VS2008.
//haar.h
#include "stdio.h"
#include "stdlib.h"
class HaarClass
{
public:
HaarClass(void);
~HaarClass(void);
void Memory_Leak_Test();
private:
long* feature_vector_;
};
HaarClass::HaarClass(void)
{
feature_vector_ = NULL;
}
HaarClass::~HaarClass(void)
{
if (feature_vector_ != NULL) {
delete[] feature_vector_;
}
}
void HaarClass::Memory_Leak_Test(){
if (feature_vector_!=NULL) {
delete[] feature_vector_;
}
for (int i=0; i<10; i++)
{
feature_vector_ = new long[100];
if (feature_vector_!=NULL) {
delete[] feature_vector_;
}
}
}
And the main file is
//main.cpp
#include <cstdio>
#include "haar.h"
int main()
{
HaarClass a;
a.Memory_Leak_Test();
return 0;
}
And the error or alert is
_ASSERTE(_BLOCK_TYPE_IS_VALID(pHead->nBlockUse));
in dbgdel.cpp Line: 52.
Therefore, what's the error? Thanks.
if (feature_vector_!=NULL)-- What compiler are you using? Ifnew[]fails, astd::bad_allocexception is thrown, and not a NULL is returned. - PaulMcKenzieif (feature_vector_!=NULL) { delete[] feature_vector_; }? isfeature_vector_initialized toNULLor pointer to buffer allocated and not deleted properly before calling this function? - MikeCATfeature_vector_[-1]). It's detected in the destructor forfeature_classwhen it tries to delete the allocated pointers. You're not NULLing outfeature_vector_after you delete it, so this might also be due to a double free (not sure if VS detects that differently). - 1201ProgramAlarm