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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 15, 20262026-06-15T08:47:13+00:00 2026-06-15T08:47:13+00:00

I work a lot with the OpenCV C++ interface and designed a number of

  • 0

I work a lot with the OpenCV C++ interface and designed a number of classes which use Mat’s as private resources.

Recently, I got concerned about the Mat class, as it always uses image data as shared resource unless I explicitly call clone. Even if I write const Mat I can’t be sure that the imagedata does not get changed later from the outside.

So I need to clone to ensure encapsulation. But the problem with needing to explicitly clone a Mat is that it is often unnecessary and expensive. On the other hand I understand that the need for shared imagedata originates from roi selectors, and being able to write something like this:
Mat m_small = m_big(my_roi).

My questions are:

1.) Should not the cv::Mat class be rather lazily cloned? So the user will not see Mat’s as shared resource handlers from the outside. Should not the user explicitly instantiate a class called something like SharedMat when a real shared imagedata is needed?
2.) Do you have any better strategy than always cloning in case of cv::Mat as private resource for a class?


UPDATE: “you do not use Mat::clone() unless you plan to modify the data.” (by Vadim Pisarevsky)
This idea has a problem.
Consider the situation that you have this class:

class Res_handler{
public:
  const Mat emit_mat(){ return m_treasure; } // I argue you are compelled to clone here.
private:
  Mat m_treasure;
};

If you do not clone in this case you can write

Mat m_pirate = res_handler.emit_mat(); m_pirate = Scalar(0,0,0);

which causes a full black-out in the m_treasure inside the res_handler through the shared image data between m_pirate and m_treasure. 🙂 So to avoid accidental modification of the inner m_treasure, you need to clone it.

On the other hand, this solution is also flawed:

const Mat m_pirate = res_handler.emit_mat(); 

because m_treasure can get modified too, so the content of m_pirate gets changed in the background, causing great headache to the pirate’s programmer. 🙂

  • 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-15T08:47:14+00:00Added an answer on June 15, 2026 at 8:47 am

    Yes, this is a poor design. Because Mat implements shared ownership internally, it’s not compatible with the standard way of choosing ownership policy, namely smart pointers. The basic issue is that data and ownership are orthogonal, and ought to be separated.

    Because it is mutable, even a const Mat is more like a const shared_ptr<Mat>, with no way of describing that the contained Mat ought to be mutable, i.e., shared_ptr<const Mat>. This bears great similarity to the problems with final in Java, if you’re familiar.

    I believe you can skirt these issues by wrapping Mat in a class that exposes the same interface as Mat, but which implements copy-on-write behaviour atop the default shared implementation.

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

Sidebar

Related Questions

I've been doing a lot of work with tuples and lists of tuples recently
I work a lot with files which contain data on fixed positions. Non-delimited CSV
I work a lot with decision makers looking to use technology better in their
We work with a lot of real estate, and while rearchitecting how the data
I do a lot of work with temperatures in Textmate and I'd love to
I have an MVC app that does a lot of work with jQuery ajax
My work is running SQL Server 2008 and I spend a lot of time
Having a lot of trouble getting texture maps to work in openGL ES (iphone).
I used to work in JavaScript a lot and one thing that really bothered
The company I work for produces a lot of video and we want 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.