Using the Mat constructor to reshape will cause the address to be cleared and released

In the following code, the value of t1 is correct before running the cv::Mat t2 = t.clone();, but after running cv::Mat t2 = t.clone();, the value of t1 is modified to be the same as t, and the address of t2 becomes the same as t1.
This problem will not occur after changing rr = cv::Mat(rr.rows, 1, CV_64FC3, rr.data); to rr = rr.reshape(3, rr.rows);.
I looked up the reshape function, and it just modifies the data layout.
Why does reshape not have this error? Did my operation cause opencv to release the memory of rr by mistake, causing t2 to overwrite the data of t1?

cv::Mat test(cv::Mat t)
{
	cv::Mat tt(3, 3, CV_64F, t.data);
	cv::Mat rr = tt + 10;

	rr = cv::Mat(rr.rows, 1, CV_64FC3, rr.data);
	//rr = rr.reshape(3, rr.rows);
	return rr;
}

int main()
{
	cv::Mat t = cv::Mat::zeros(3, 1, CV_64FC3);
	cv::Mat t1 = test(t);
	std::cout << "t1 before t clone:" << t1 << std::endl;
	cv::Mat t2 = t.clone();
	std::cout << "t1 after t clone:" << t1 << std::endl;
	return 1;
}

output:
image

first, very inconvenient variable naming. reuse also makes it hard to discuss what’s happening.

that’s a local Mat with its own backing memory. this memory’s reference count is 1.

that creates a new Mat, referencing the above-mentioned backing memory… without incrementing its reference count. it’s just a raw pointer.

this causes the first Mat, with its own backing memory, to be discarded, and the backing memory’s refcount is decremented. it’s now 0, and will be freed.

and that’s how this newly constructed Mat points to invalid memory.

you’re lucky that didn’t cause a segmentation fault.

it looks like the clone() reused the memory that was just released. so that memory is initialized to 0, instead of remaining “10” as long as it’s unused.

Thank you very much. It seems that it is unwise to construct Mat directly using the address of Mat. I will pay attention to it, of course, including the variable naming of my test code