mirror of
https://github.com/ruby-opencv/ruby-opencv
synced 2023-03-27 23:22:12 -04:00
modified CvMat#initialize to run GC and retry memory allocation when it is failed
This commit is contained in:
parent
3bba97baf1
commit
882b778e94
1 changed files with 24 additions and 2 deletions
|
@ -434,8 +434,30 @@ rb_initialize(int argc, VALUE *argv, VALUE self)
|
|||
VALUE row, column, depth, channel;
|
||||
rb_scan_args(argc, argv, "22", &row, &column, &depth, &channel);
|
||||
|
||||
DATA_PTR(self) = cvCreateMat(FIX2INT(row), FIX2INT(column),
|
||||
CV_MAKETYPE(CVMETHOD("DEPTH", depth, CV_8U), argc < 4 ? 3 : FIX2INT(channel)));
|
||||
CvMat *ptr;
|
||||
try {
|
||||
ptr = cvCreateMat(FIX2INT(row), FIX2INT(column),
|
||||
CV_MAKETYPE(CVMETHOD("DEPTH", depth, CV_8U), argc < 4 ? 3 : FIX2INT(channel)));
|
||||
}
|
||||
catch(cv::Exception& e) {
|
||||
if (e.code != CV_StsNoMem) {
|
||||
rb_raise(rb_eRuntimeError, "%s", e.what());
|
||||
}
|
||||
|
||||
// When memory allocation is failed, run GC and retry it
|
||||
rb_gc_start();
|
||||
try {
|
||||
ptr = cvCreateMat(FIX2INT(row), FIX2INT(column),
|
||||
CV_MAKETYPE(CVMETHOD("DEPTH", depth, CV_8U), argc < 4 ? 3 : FIX2INT(channel)));
|
||||
}
|
||||
catch (...) {
|
||||
fprintf(stderr, "[FATAL] failed to allocate memory\n");
|
||||
exit(EXIT_FAILURE);
|
||||
}
|
||||
}
|
||||
free(DATA_PTR(self));
|
||||
DATA_PTR(self) = ptr;
|
||||
|
||||
return self;
|
||||
}
|
||||
|
||||
|
|
Loading…
Add table
Add a link
Reference in a new issue