Re: Memory leak of RefPtr?



Rose,

On a side note, as a purely form of programming recommendation, there should almost never be a reason to create a pointer to a smart pointer.  (Invariably, there's got to be an exception to the case but I've never come across one yet)

The best reason for using smart pointers is to avoid the need to explicity call delete on any memory.  In fact, the only case I have ever seen the absolute need to call delete when properly using smart pointers is when there a thread creation call fails.

Reading the documentation on the boost::shared_ptr and friends over at www.boost.org helped quite a bit in learning how to use smart pointers in general.

As a rule of thumb, if you ever find your self using delete without a very specific reason why a smart pointer won't work, you're probably doing something thats a Bad Idea.

Totally unrelated, Rose Cumming and Murray Cumming, is there a relation?

Paul

On 9/9/06, Murray Cumming < murrayc murrayc com> wrote:
On Thu, 2006-08-24 at 20:11 +0000, Rose Cumming wrote:
> Hello,
>
> I'm not familiar with smart pointer. I'm just wondering will the following
> code cause memory leak?
>
> For example,
>
> in my class, I have a member variable
>
> Glib::RefPtr<Gdk::Pixbuf> viewBuffer_;
>
> in the constructor of this class I do this,
>
> viewBuffer_ = Gdk::Pixbuf::create(Gdk::COLORSPACE_RGB, false, 8, 100, 100);
>
> in a member function of this class I do this,
>
> void draw()
> {
>    Glib::RefPtr<Gdk::Pixbuf> *buffer;
>    someObjectPointer_->getImage(&buffer);

You are using a pointer to the RefPtr. Like any use of a pointer like
this you would need to document whether the caller should delete the
instance that it has been given (e.g. "delete buffer" to delete the
refptr to the buffer, hence deleting one reference to the buffer
itself.).


But it's far easier to just enjoy the benefits of using the
smartpointer:

Glib::RefPtr<Gdk::Pixbuf> buffer = someObjectPointer_->getImage();

or

Glib::RefPtr<Gdk::Pixbuf> buffer;
someObjectPointer_->getImage(buffer);

which would need

SomeObject::getImage(Glib::RefPtr<Gdk::Pixbuf>& buffer);

[snip]

--
Murray Cumming
murrayc murrayc com
www.murrayc.com
www.openismus.com

_______________________________________________
gtkmm-list mailing list
gtkmm-list gnome org
http://mail.gnome.org/mailman/listinfo/gtkmm-list



[Date Prev][Date Next]   [Thread Prev][Thread Next]   [Thread Index] [Date Index] [Author Index]