동적으로 할당 된 메모리로 처리하는 동안 나타나는 문제가 있습니다. 비슷한 문제를 찾고 있었지만 안타깝게도 해결책이 없었습니다. 내 수업의디버그 어설 션이 실패했습니다! 잘못된 메모리 해제
선언 : 내 수업의
class StructuralElement
{
private:
short rows;
short columns;
short *se;
friend class Morph;
public:
StructuralElement(char *path);
~StructuralElement();
short getAt(short row, short column);
int getSize();
};
정의 : 내 수업의 코드는 다음과 같습니다
StructuralElement::StructuralElement(char *path)
{
std::string line;
short rows=0, elements=0;
short it = 0;
std::string token;
try
{
std::ifstream file(path); //counting rows and columns in a file!
if (file.is_open() == NULL){ CannotOpenException ex; throw ex; }
if (file.fail()) { CannotOpenException ex; throw ex; }
while (getline(file,line))
{
rows++;
std::stringstream ss(line);
while (getline(ss, token, ' '))
{
elements++;
}
}
file.close();
this->rows = rows;
if (rows!=0)
this->columns = (elements/rows);
se = new short[elements];
std::ifstream file2(path);
if (!file2.is_open()) throw;
while (getline(file2, line))
{
std::stringstream ss(line);
while (getline(ss, token, ' '))
{
this->se[it++] = (static_cast<int>(token.at(0))-48);
}
}
file2.close();
}
catch (CannotOpenException &ex)
{
std::cerr << "Error occured! Unable to load structural element!";
}
}
StructuralElement::~StructuralElement()
{
if (se != NULL) delete[] se;
}
프로그램은 작업 완료 이미 텍스트가 나타납니다 때 " Cake any key .... "라고 표시되면 콘솔에 오류가 발생합니다.
디버그 어설 션 오류
.......
식 : _BLOCK_TYPR_IS_VALID (pHeap-> nBlockUse)
제가 = SE 라인을 변경할 때, 새로운 단락 [요소]; 님께 : se = new short [];
그런 다음 메시지가 표시됩니다. 프로그램이 중단 점에서 실행되었습니다.
내가 잘못 만들었습니까? 팁을 주셔서 미리 감사드립니다.