2017-12-14 4 views
1

저는 class을 가지고 있습니다.이 객체는 기본적으로 다른 객체에 대한 포인터 인 2D matrix의 래퍼이고의 행렬입니다. 내 class의 소멸자가 호출 될 때마다 어떤 이유 포인터의 벡터 행렬을 사용하여 객체에서 소멸자를 호출 할 때 프로그램이 충돌합니다

, 프로그램 충돌, 그리고 그들이 충돌이 발생하는 nullptr을 경우에도 delete 포인터를 시도처럼 보인다.

CPP 파일 :

RotationSolution.h :

#ifndef PUZZLESOLVER_ROTATIONSOLUTION_H 
#define PUZZLESOLVER_ROTATIONSOLUTION_H 

#include <vector> 
#include <fstream> 
#include "PuzzlePiece.h" 

using namespace std; 

class RotationSolution { 
private: 
    int _height, _width; 
    vector<vector<PuzzlePiece*>> _matrix; 

public: 
    RotationSolution(); 

    RotationSolution(int height, int width); 

    vector<PuzzlePiece*>& operator[](int row); 

    int get_height() const; 

    int get_width() const; 
}; 


#endif //PUZZLESOLVER_ROTATIONSOLUTION_H 

RotationSolution.cpp :

#include "RotationSolution.h" 

vector<PuzzlePiece*>& RotationSolution::operator[](int row) { 
    return _matrix[row]; 
} 

RotationSolution::RotationSolution() : RotationSolution(0, 0) {} 

RotationSolution::RotationSolution(int height, int width) : 
    _height(height), _width(width), _matrix(vector<vector<PuzzlePiece*>>(_height, vector<PuzzlePiece*>(_width, nullptr))) 
{} 

int RotationSolution::get_height() const { 
    return _height; 
} 

int RotationSolution::get_width() const { 
    return _width; 
} 
다음

.h.cpp 파일입니다 0

코드는 실제로 다음과 같습니다 섹션에서 충돌한다 :

for (auto length: _rowLengths) { 
     auto height = size/length; 
     _sol = RotationSolution(height, length); 

     ... 
    } 

_sol = RotationSolution(height, length); 라인의 두 번째 반복에서.

// __p is not permitted to be a null pointer. 
    void 
    deallocate(pointer __p, size_type) 
    { ::operator delete(__p); } 

내가 지금 어떤 멍청한 놈의 실수를 용서하시기 바랍니다 c++ 새로운 여전히 오전 :

디버깅의 코드는 충돌 신호 (lib 디렉토리 파일을 인 내가 확신) new_allocator.h에서입니다 전송 나쁜 사례 :)

+0

왜 downvote? 누락 된 것이 있으면 알려주세요. –

+0

코드를 모두 붙여 놓으셨습니까? 소멸자조차도 가지고 있지 않습니다. 어떻게 거기에서 충돌 할 수 있습니까? – nvoigt

+2

[mcve]를 게시하십시오. – nvoigt

답변

0

RotationSolution 클래스에 디자인 결함이 있습니다. 원시 포인터를 저장하는 멤버 변수 _matrix가 포함되어 있으며 _matrix 내부에 저장된 객체를 복제 할 올바르게 정의 된 assigment 연산자가 누락되었습니다. Default generated copy assigment operator은 메모리를 두 번 비우고 충돌을 일으킬 수있는 포인터를 복사합니다 (here은 어떤 일이 일어나는 이유와 그 이유). "< vector < std :: shared_ptr < PuzzlePiece>>> _matrix"(또한 "메모리"를 포함)를 사용해보십시오. 잘못된 메모리 관리로 대부분의 문제를 해결해야합니다. 똑똑한 포인터에 관하여 읽을 것이다 tutorial는 여기있다.

+0

내 문제와 같은 소리. 내 호출 스택에서'operator ='를한다. 그래도 문제를 해결하는 방법을 모르겠지만 문제는 아닌 것 같습니다. 감사! –

+0

죄송합니다, 내 의견 텍스트에 어딘지 망쳐있어. 벡터 템플릿 선언 안에 PuzzlePiece * 대신 std :: shared_ptr 유형을 사용하려고하면 assigments의 문제를 해결하는 데 도움이됩니다. PuzzlePiece 객체를 설치하려면 std :: make_shared (... 생성자 매개 변수는 여기에 있습니다 ...) –

관련 문제