看板 C_and_CPP 關於我們 聯絡資訊
IDE: BCB 問題: 程式執行後會有access violation, 判斷應該是由於程式中CScenePoint tmp0(w,h)以及arr.push_back(tmp0)皆會執行解構子 因此我有另外對 CScenePoint& CScenePoint::operator=(const CScenePoint &) 進行overwrite,但沒有用...想請問各位,是否能給些提示告訴我應該加什麼。 謝謝! 程式碼如下: //--------------------------------------------------------------------------- #include <vector> #pragma hdrstop using namespace std; //--------------------------------------------------------------------------- #pragma argsused //--------------------------------------------------------------------------- class CScenePoint { public: CScenePoint(): m_width(0), m_height(0), m_bits(16), m_data(NULL) {}; CScenePoint(int w, int h): m_bits(16) { m_width = w; m_height = h; m_data = (int*)malloc(sizeof(int)*m_width*m_width*m_bits); } ~CScenePoint() { if(m_data) free(m_data); m_data = NULL; } private: int m_width, m_height, m_bits; int *m_data; }; //--------------------------------------------------------------------------- int main(int argc, char* argv[]) { vector<CScenePoint> arr(1); int w = 1392, h = 1040; CScenePoint tmp0(w,h); arr.push_back(tmp0); return 0; } //--------------------------------------------------------------------------- -- ※ 發信站: 批踢踢實業坊(ptt.cc) ◆ From: 118.169.40.70
flamo:tmp0.m_data被free兩次囉 12/08 23:35
flamo:可考慮視情況用auto_ptr或shared_ptr 12/09 00:00
tinlans:當然沒有用,你還需要 "overload" copy constructor。 12/09 00:20
HeyScng:謝謝 加上copy constructor後果然OK了 12/09 23:20