[cairo] Memory leaks in cairomm

Rodrigo Rivas rodrigorivascosta at gmail.com
Mon Jun 12 23:39:22 PDT 2006


Well, I'm new to this list and maybe I'm asking a silly question... but
why there is need of the RefPtr stuff at all? It has been briefly
discussed before, but with little arguments...

What I mean is: what is wrong with the reference semantics?
Take for example the following class:

-----------8<---------------
class Surface
{
private:
  cairo_surface_t *cobj;
public:
  Surface(cairo_surface_t *obj=0, bool take_ownership = true)
  :cobj(obj)
  {
    if (obj && !take_ownership)
      cairo_surface_reference(obj)
  }
  Surface(const Surface &other)
  :cobj(other.cobj)
  {
    if (!cobj)
      cairo_surface_reference(obj)
  }
  ~Surface()
  {
    if (cobj)
        cairo_surface_destroy(cobj);
  }
  const Surface &operator=(const Surface &other)
  {
    Surface temp(other);
    swap(temp);
    return *this;
  }
  void swap(Surface &other)
  {
    cairo_surface_t *o = other.cobj;
    other.cobj = cobj;
    cobj = o;
  }

// here comes the usual member functions, e.g.:
  void Flush()
  {
    cairo_surface_flush(cobj);
  }
//....
};
--------------8<--------

Now you'll 'never' need a pointer to a Surface object. You can even do
std::vector<Surface>.

I can't see the need for the RefPtr stuff here, or for allocating our
C++ objects from the heap. IMHO this way is simpler and more
efficient: just copy them all around.

What do you think?

--
Rodrigo.


More information about the cairo mailing list