c++ - cvCreateMat memory leak (OpenCV) -


alright; i'm finding odd memory leak when attempting use cvcreatemat make room soon-to-be-filled mat. below attempting do; adaptivethreshold didn't when put 3-channel image in, wanted split separate channels. works! every time go through particular function gain ~3mb of memory. since function expected run few hundred times, becomes rather noticeable problem.

so here's code:

void adaptivecolorthreshold(mat *inputshot, int adaptivemethod, int blocksize, int csubtraction) {     mat newinputshot = (*inputshot).clone();     mat inputblue = cvcreatemat(newinputshot.rows, newinputshot.cols, cv_8uc1);     mat inputgreen = cvcreatemat(newinputshot.rows, newinputshot.cols, cv_8uc1);     mat inputred = cvcreatemat(newinputshot.rows, newinputshot.cols, cv_8uc1);      for(int rows = 0; rows < newinputshot.rows; rows++)     {         for(int cols = 0; cols < newinputshot.cols; cols++)         {             inputblue.data[inputblue.step[0]*rows + inputblue.step[1]*cols] = newinputshot.data[newinputshot.step[0]*rows + newinputshot.step[1]*cols + 0];             inputgreen.data[inputgreen.step[0]*rows + inputgreen.step[1]*cols] = newinputshot.data[newinputshot.step[0]*rows + newinputshot.step[1]*cols + 1];             inputred.data[inputred.step[0]*rows + inputred.step[1]*cols] = newinputshot.data[newinputshot.step[0]*rows + newinputshot.step[1]*cols + 2];         }     }      adaptivethreshold(inputblue, inputblue, 255, adaptivemethod, thresh_binary, blocksize, csubtraction);     adaptivethreshold(inputgreen, inputgreen, 255, adaptivemethod, thresh_binary, blocksize, csubtraction);     adaptivethreshold(inputred, inputred, 255, adaptivemethod, thresh_binary, blocksize, csubtraction);      for(int rows = 0; rows < (*inputshot).rows; rows++)     {         for(int cols = 0; cols < (*inputshot).cols; cols++)         {             (*inputshot).data[(*inputshot).step[0]*rows + (*inputshot).step[1]*cols + 0] = inputblue.data[inputblue.step[0]*rows + inputblue.step[1]*cols];             (*inputshot).data[(*inputshot).step[0]*rows + (*inputshot).step[1]*cols + 1] = inputgreen.data[inputgreen.step[0]*rows + inputgreen.step[1]*cols];             (*inputshot).data[(*inputshot).step[0]*rows + (*inputshot).step[1]*cols + 2] = inputred.data[inputred.step[0]*rows + inputred.step[1]*cols];         }     }      inputblue.release();     inputgreen.release();     inputred.release();     newinputshot.release();      return; } 

so going through 1 line @ time...

  • newinputshot adds ~3mb
  • inputblue adds ~1mb
  • inputgreen adds ~1mb
  • and inputred adds ~1mb

so far, - need memory hold data. newinputshot gets data right off bat, inputrgb need data newinputshot - allocate space filled in upcoming for-loop, (as expected) allocates no new memory, fills in space claimed.

the adaptivethresholds don't add new memory either, since they're supposed overwrite there, , next for-loop writes straight inputshot; no new memory needed there. around (manually) releasing memory.

  • releasing inputblue frees 0mb
  • releasing inputgreen frees 0mb
  • releasing inputred frees 0mb
  • releasing newinputshot frees ~3mb

now, according opencv documentation site: "opencv handles memory automatically."

first of all, std::vector, mat, , other data structures used functions , methods have destructors deallocate underlying memory buffers when needed. means destructors not deallocate buffers in case of mat. take account possible data sharing. destructor decrements reference counter associated matrix data buffer. buffer deallocated if , if reference counter reaches zero, is, when no other structures refer same buffer. similarly, when mat instance copied, no actual data copied. instead, reference counter incremented memorize there owner of same data. there mat::clone method creates full copy of matrix data.

tldr quote: related mats clumped in super-mat gets released @ once when nothing left using it.

this why created newinputshot clone (that doesn't clumped inputshot) - see if occurring inputrgbs. well... nope! inputrgbs own beast refuse deallocated. know isn't of intermediate functions because snippet exact same thing:

void adaptivecolorthreshold(mat *inputshot, int adaptivemethod, int blocksize, int csubtraction) {     mat newinputshot = (*inputshot).clone();     mat inputblue = cvcreatemat(newinputshot.rows, newinputshot.cols, cv_8uc1);     mat inputgreen = cvcreatemat(newinputshot.rows, newinputshot.cols, cv_8uc1);     mat inputred = cvcreatemat(newinputshot.rows, newinputshot.cols, cv_8uc1);      inputblue.release();     inputgreen.release();     inputred.release();     newinputshot.release();      return; } 

that's simple gets. allocate - fail deallocate. what's going on cvcreatemat?

i suggest not use cvcreatemat , don't need clone original mat either. using split() , merge() functions. dirty work , return mat's handle memory you. don't have opencv installed right can't test of code i'm sure that's route want take.


Comments

Popular posts from this blog

css - SVG using textPath a symbol not rendering in Firefox -

Java 8 + Maven Javadoc plugin: Error fetching URL -

datatable - Matlab struct computations -