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 8118183
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 6, 20262026-06-06T04:24:07+00:00 2026-06-06T04:24:07+00:00

I have experienced some memory managment ambiguitys with opencv. You could do this with

  • 0

I have experienced some memory managment ambiguitys with opencv.
You could do this with the new opencv c++ classes:

cv::Mat theimage = cvLoadImage("rawimage.pgm",CV_LOAD_IMAGE_ANYDEPTH);

now what I don t understand if I do the following I will get an error:

theimage.deallocate();

maybe this is wrong. I have made some experiments and when I release the Mat object:

theimage.release();

the IplImage object still remains in the memory. Is there some way to say to the cv::Mat object destroy the IplImage object or is my first line of code wrong (since there I have lost the pointer for the IplImage object) ?
I have seen a lot of exemples in the internet where people use the first line of code. Many explains how to convert but no one explains what happens with the memory !

Tthe problem is that I have many classes that are using the IplImage object (I started my project using that). And I understand why cv::Mat is better than IplImage.

–EDIT–

In the internet I have found following solution for the mixing:

cv::Ptr<IplImage> tmp = cvLoadImage("rawimage.pgm",CV_LOAD_IMAGE_ANYDEPTH);
cv::Mat theimage(tmp);

I think this could solve some of my problems but makes my code a bit unreadable and is in my opinion stil dangerous.
If tmp gets deallocated before cv::Mat I will use some corrupt objects (I didn t test that but I think it is so). The easy way would be to use a copy:

cv::Mat theimage(tmp,true);

this is the only solution that I found but for me it feels wrong…

  • 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-06-06T04:24:09+00:00Added an answer on June 6, 2026 at 4:24 am

    The ambiguity comes from an extremelly horrible practice: mixing the C interface with the C++ interface of OpenCV. Don’t do it, use cv::imread() instead.

    The destructor of cv::Mat will always deallocate memory when needed, except when it is initialized from a IplImage, then it’s up to you to free it’s resources with deallocate(). I wrote a simple experiment to verify this information:

    void load()
    {
        cv::Mat img = cvLoadImage("out.png", 1);
    
        std::cout << "* IMG was loaded\n";
        getchar();
    
        img.deallocate();
    }
    
    int main()
    {
        load();
    
        std::cout << "* IMG was deallocated\n";
        getchar();
    
        return 0;
    }
    

    The getchar() calls pause the program so you can check the memory footprint of the application. If you comment out deallocate() you’ll notice that the memory footprint doesn’t decrease after loading the image.

    On a side note, Mat::release() only decreases the reference counter and deallocates the data if needed (but it will also not deallocate in case the Mat was initialized from IplImage).

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

Sidebar

Related Questions

I have experienced Zend Framework 1 and I've build some apps with that framework.
Hey, i have some experience with MVC. but I'm new to rails. I'm using
I'm kind of new to WPF although I have some experience in Forms, and
I have been wandering about this for some time now. I'm still a beginner,
I am sure a lot of you have experienced this. Sometimes while debugging a
I'm sure some of you already experienced it. I have two linked lists of
Little bit of background, I'm more experienced with Java, and have some C/C++ experience.
I'm new to the C++ world, but I have some experience with C and
I have a program like this: it compiles files in memory and then executes
I have some experience using parallel extensions in .Net development, but I was looking

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.