# Bug regarding Matrix return

**URL:** <https://forum.opencv.org/t/bug-regarding-matrix-return/13359>\
**Category:** Uncategorized\
**Tags:** core\
**Created:** [May 30, 2023, 12:24pm UTC](https://forum.opencv.org/t/bug-regarding-matrix-return/13359 "2023-05-30T12:24:31Z")\
**Posts on this page:** 3\
**Page:** 1

<div class="post-metadata">

**Author:** ![Aditya](https://avatars.discourse-cdn.com/v4/letter/a/df705f/32.png) [@Aditya](https://forum.opencv.org/u/Aditya)\
**Post date:** [May 30, 2023, 12:24pm UTC](https://forum.opencv.org/t/bug-regarding-matrix-return/13359/1 "2023-05-30T12:24:31Z")

</div>

I found one bug  
I initialized one matrix inside one function and returned through function  
then i get that returned matrix.  
I found that matrix elements gets deleted  
below is demo code

```auto

Mat testDouble()
	{
		double transVals[] = { 10.0, 20.0, 30.0, 1.0, 1.0, 1.0, 1.0, 1.0, 1.0 };
		Mat translationMat(3, 3, CV_64F, &transVals);
		cout << "translationMat:" << translationMat << endl;
		return translationMat;
	}

	void main()
	{
		Mat testMat = testDouble();
		cout << "testMat:" << testMat << endl;
	}

```

Following is the output values after testDouble is junk

---

<div class="post-metadata">

**Author:** ![berak](https://avatars.discourse-cdn.com/v4/letter/b/85f322/32.png) [@berak](https://forum.opencv.org/u/berak)\
**Post date:** [May 30, 2023, 12:34pm UTC](https://forum.opencv.org/t/bug-regarding-matrix-return/13359/2 "2023-05-30T12:34:59Z")

</div>

known behaviour, not a bug.  
this constructor:

> [@Aditya](#):
>
> ```auto
> Mat translationMat(3, 3, CV_64F, &transVals);
> 		
> 
> ```

does neither allocate own memory, nor does it add refcounts.  
(indeed the mem is invalid, once it leaves the function scope)  
please `return translationMat.clone();` in that case.

---

<div class="post-metadata">

**Author:** ![Aditya](https://avatars.discourse-cdn.com/v4/letter/a/df705f/32.png) [@Aditya](https://forum.opencv.org/u/Aditya)\
**Post date:** [May 31, 2023, 4:15am UTC](https://forum.opencv.org/t/bug-regarding-matrix-return/13359/3 "2023-05-31T04:15:39Z")

</div>

Thank you i will check
