Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 6818353
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 26, 20262026-05-26T21:10:05+00:00 2026-05-26T21:10:05+00:00

My memory is getting full fairly quick once using the next piece of code.

  • 0

My memory is getting full fairly quick once using the next piece of code. Valgrind shows a memory leak, but everything is allocated on stack and (supposed to be) freed once the function ends.

void mult_run_time(int rows, int cols)
{
    Mat matrix(rows,cols,CV_32SC1);
    Mat row_vec(cols,1,CV_32SC1);

    /* initialize vector and matrix */
    for (int col = 0; col < cols; ++col)
    {
        for (int row = 0; row < rows; ++row)
        {
            matrix.at<unsigned long>(row,col) = rand() % ULONG_MAX;
        }

        row_vec.at<unsigned long>(1,col) = rand() % ULONG_MAX;
    }
    /* end initialization of vector and matrix*/

    matrix*row_vec;  
}

int main()
{
    for (int row = 0; row < 20; ++row)
    {
        for (int col = 0; col < 20; ++col)
        {
            mult_run_time(row,col);
        }
    }

    return 0;
}

Valgrind shows that there is a memory leak in line Mat row_vec(cols,1,CV_32CS1):

==9201== 24,320 bytes in 380 blocks are definitely lost in loss record 50 of 50
==9201==    at 0x4026864: malloc (vg_replace_malloc.c:236)
==9201==    by 0x40C0A8B: cv::fastMalloc(unsigned int) (in /usr/local/lib/libopencv_core.so.2.3.1)
==9201==    by 0x41914E3: cv::Mat::create(int, int const*, int) (in /usr/local/lib/libopencv_core.so.2.3.1)
==9201==    by 0x8048BE4: cv::Mat::create(int, int, int) (mat.hpp:368)
==9201==    by 0x8048B2A: cv::Mat::Mat(int, int, int) (mat.hpp:68)
==9201==    by 0x80488B0: mult_run_time(int, int) (mat_by_vec_mult.cpp:26)
==9201==    by 0x80489F5: main (mat_by_vec_mult.cpp:59)

Is it a known bug in OpenCV or am I missing something?

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-26T21:10:05+00:00Added an answer on May 26, 2026 at 9:10 pm

    There is no use in using unsigned long at

    matrix.at<unsigned long>(row,col) = rand() % ULONG_MAX;
    

    because rand() returns an intenger anyways, so there is no gain in the total range, so use unsigned int instead.

    In the line:

    row_vec.at<unsigned long>(1,col) = rand() % ULONG_MAX;
    

    you are accessing an outside range index. In c++, vectors starts from 0 and not 1. And the matrix are stored row-by-row in opencv. You are acessing unallocated memory area, thats probably the reason why valgrind is finding memory leaks. Use:

    row_vec.at<unsigned int>(col, 0) = rand() % ULONG_MAX;
    

    I presume that you are not compiling your program in Debug Mode because if it were the case, opencv uses an assertion before accessing an index to make sure you are inside the total range of the vector, if you were compiling in debug mode, your program would throw an assertion failure during the execution of your code, what makes easier to track this kind of mistakes. I recomend you to start prototyping your code in debug mode.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I'm getting memory leak problem in this code: UIImage myImage1 = [[UIImage alloc] initWithData:[NSData
I am getting memory leak in instruments in the code -(void)connectionDidFinishLoading:(NSURLConnection *)connection { NSAutoreleasePool
Hi I am getting memory leak in Instruments for the following line of code
HI, I am getting memory leak at NSObject allocation i.e., ContactDTO* contactDTO = [[ContactDTO
I'm getting the memory leak message upon shutdown, saying that I'm leaking 3 of
Im getting the same memory leak as I mentioned the link below. NSXMLParser Leaking
i thought i getting the hang of Cocoa memory management, but apperently i have
I have the following piece of code. This is written for getting the list
I am getting memory leak at text fiels please suggest me how to resolve
Iam getting memory leaks in below code. Is this is the proper way to

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.