Skip to content

Memory leak #102

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Closed
cike111 opened this issue Jun 17, 2024 · 4 comments · Fixed by #104 or #105
Closed

Memory leak #102

cike111 opened this issue Jun 17, 2024 · 4 comments · Fixed by #104 or #105
Labels
bug Something isn't working

Comments

@cike111
Copy link

cike111 commented Jun 17, 2024

Describe the bug
In iOS, if you call grayMat.set(y, x, grayLevel) every 2 seconds, it will cause the memory to continuously increase

To Reproduce
17186112505115

pubspec.yaml code:

dependencies:
  flutter:
    sdk: flutter

  # The following adds the Cupertino Icons font to your application.
  # Use with the CupertinoIcons class for iOS style icons.
  cupertino_icons: ^1.0.6
  opencv_dart: ^1.0.9

mian.dart

 void initState() {
    super.initState();
    _timer = Timer.periodic(Duration(milliseconds: 2000), (timer) {
      test();
    });
  }
test(){
  CV.Mat grayMat = CV.Mat.zeros(
     1000, 1000, CV.MatType.CV_8UC1);
  int maxY = grayMat.rows - 1;
  int maxX = grayMat.cols;
  for (int y = maxY; y >= 0; y--) {
    for (int x = 0; x < maxX; x++) {
      grayMat.set<int>(0, 0, 30);
    }
  }
}
@cike111 cike111 added the bug Something isn't working label Jun 17, 2024
@rainyl
Copy link
Owner

rainyl commented Jun 18, 2024

Confirmed, Mat_Set will cause a memory leak even though manually dispose Mat, but every thing looks ok, not sure why...

CvStatus Mat_SetUChar(Mat m, int row, int col, uint8_t val)
{
BEGIN_WRAP
m.ptr->at<uchar>(row, col) = val;
END_WRAP
}

void setU8(int row, int col, int val, [int? i2]) => i2 == null
? cvRun(() => CFFI.Mat_SetUChar(ref, row, col, val))
: cvRun(() => CFFI.Mat_SetUChar3(ref, row, col, i2, val));

rainyl added a commit that referenced this issue Jun 18, 2024
This was linked to pull requests Jun 18, 2024
rainyl added a commit that referenced this issue Jun 18, 2024
@rainyl
Copy link
Owner

rainyl commented Jun 18, 2024

@cike111 v1.0.10 and 2.0.0-dev.12 have been published, please update and try if it has been solved, if not please open this issue again.

@cike111
Copy link
Author

cike111 commented Jun 19, 2024

@rainyl Your approach seems to have some effect, but it hasn't completely solved the problem.

image

@cike111 cike111 mentioned this issue Jun 19, 2024
@rainyl
Copy link
Owner

rainyl commented Jun 19, 2024

@cike111 emmmmm..., alright, I suggest you to dispose it manually if you have to create mats in a loop, due to the limitation of dart GC (#64 (comment)), the attached native resources may not be freed timely, in the above example, change it to

test(){
  CV.Mat grayMat = CV.Mat.zeros(
     1000, 1000, CV.MatType.CV_8UC1);
  int maxY = grayMat.rows - 1;
  int maxX = grayMat.cols;
  for (int y = maxY; y >= 0; y--) {
    for (int x = 0; x < maxX; x++) {
      grayMat.set<int>(0, 0, 30);
    }
  }
  // add this line
  grayMat.dispose();
}

If the problem still exists, then I have no more ideas too, and I have no apple devices so can't debug it, on my windows PC every thing is ok.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
bug Something isn't working
Projects
None yet
2 participants