2016-11-22 74 views
1

當我們手動添加緩衝區的地址data對象的屬性時會發生什麼,然後刪除該緩衝區?當我們覆蓋cv :: Mat.data時發生了什麼

例如,

cv::Mat test; 
test.data = (address of Buffer A); 

Buffer A被刪除會發生什麼test.data

+0

你能後的代碼做你描述一下? – Tim

+1

爲什麼不使用這兩個構造函數之一? 'Mat(int rows,int cols,int type,void * data,size_t step = AUTO_STEP); 墊(尺寸大小,整型,無效*數據,爲size_t步驟= AUTO_STEP);」他們將設置數據指針,以及正確的類型和大小。讀到這裏的文檔http://docs.opencv.org/3.1.0/d3/d63/classcv_1_1Mat.html –

回答

2

文檔:http://docs.opencv.org/3.1.0/d3/d63/classcv_1_1Mat.html

int rows, cols, type; // you need initialize them 
void* data = (address of Buffer A) 
cv::Mat test = cv::Mat(rows, cols, type, data); // according to documentation, test does not own data 
cv::Mat copy = test.clone() // copy copies is a deep copy of test 

所以因爲測試沒有自己的緩衝區A,一旦它刪除,如果你訪問test.data,這是UB。 然而,由於拷貝是深拷貝,你可以訪問copy.data

相關問題