Re: how to handle an array of Pixbuf?



In the code snippet that you've shown, image2 is allocated on the stack. Then of course it's deleted when the creating method is terminated. The reference count of Gdk::Pixbuf is decreased, when Glib::RefPtr<Gdk::Pixbuf> is deleted. If the reference count reaches 0, Gdk::Pixbuf is also deleted. The next question is: How do you copy image2 in XMLSeq's constructor? If you use memcpy() or a similar function, neither RefPtr's copy constructor nor its assignment operator will be called. The reference count of Gdk::Pixbuf is not increased as it should be when Glib::RefPtr<Gdk::Pixbuf> is copied. And if you only copy a pointer to the array, you will have a dangling pointer after the array has been deleted in the creating method.

If it works with std::vector, and you are satisfied with that, we need not try to find out exactly what happened to your array, unless you're interested.

2013-03-01 21:03, jody skrev:
I just passed the array to a constructor of an object who later draws itself
     XLMSeq *pXLM3 = new XLMSeq(77, 1.0, 1, EAST, 3, image2);

in the constructor i copy the array 'image2' to the object's member
variable 'image2',
which is later used for drawing.

If i understand it correctly, by passing the pointer, the images were
deleted after the method which created them terminated?

But now I tried it  with a std::vector and that worked - thank you!
  jody

On Fri, Mar 1, 2013 at 8:30 PM, Kjell Ahlstedt
<kjell ahlstedt bredband net> wrote:
How do you transfer the array from the creating method to the painting
method? With arrays it's easy to (by mistake) copy a pointer to the array
when you should have copied the whole array. A std::vector is often safer,

std::vector< Glib::RefPtr<Gdk::Pixbuf> > image2(3);

Kjell

2013-03-01 19:17, jody skrev:
Follow up:
when i fill the PixBuf array in the same method where i paint its
element this works...
Jody

On Fri, Mar 1, 2013 at 7:10 PM, jody <jody xha gmail com> wrote:
Hi

When i do
     Glib::RefPtr<Gdk::Pixbuf> image1 =
Gdk::Pixbuf::create_from_file("base1.png");

i can later (in a different method) paint this into a cairo context like
this:
     Gdk::Cairo::set_source_pixbuf(cr, image1, 0, 0);


Now i wanted to do the same with an array of PixBufs:
      Glib::RefPtr<Gdk::Pixbuf> image2[3];
      image2[0] =  Gdk::Pixbuf::create_from_file("hexT1.png");
      image2[1] =  Gdk::Pixbuf::create_from_file("hexT2.png");
      image2[2] =  Gdk::Pixbuf::create_from_file("hexT3.png");
this compiles and seems to work, but painting (in a different method) :
      Gdk::Cairo::set_source_pixbuf(cr, image2[0], 0, 0);
fails with a segmentation fault.

I suspect that for some reason the RefPtr died, but i actually have no
idea.

Can anybody help me?

Thank You
    Jody
_______________________________________________




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