Welcome, Guest. Please login or register. Did you miss your activation email?

Author Topic: Is sf::GlResource not missing an explicit copy constructor?  (Read 4270 times)

0 Members and 1 Guest are viewing this topic.

kralo9

  • Newbie
  • *
  • Posts: 44
    • View Profile
    • Email
Is sf::GlResource not missing an explicit copy constructor?
« on: August 03, 2014, 07:46:01 pm »
GlResource::GlResource()
{
    ...
        // Increment the resources counter
        count++;
    ...
}
 

As far as I can tell, every created instance of GlResource increases the count. I am just wondering, why there is no copy constructor defined that would increase it, too?

The default copy constructor just copies all attributes (which is none).

Therefore, if I am not fully mistaking, it is possible to cause a call to ensureContext() after globalCleanup(), because the count is smaller than the actual number of GlResource instances.

Please tell me, if I am missing something.


EDIT: I just tested, whether a copying of a derived class really calls the parent's copy constructor. And it does. I also tested it without explicit declaration of a copy constructor and the default one is called. Thus, my thoughts are correct. But maybe SFML handles it another way.
« Last Edit: August 03, 2014, 07:59:12 pm by kralo9 »
I'm totally exhausted. I've got 3 children and no money! Why can't I have no children and 3 money? - Homer S.

Laurent

  • Administrator
  • Hero Member
  • *****
  • Posts: 32504
    • View Profile
    • SFML's website
    • Email
Re: Is sf::GlResource not missing an explicit copy constructor?
« Reply #1 on: August 03, 2014, 08:21:53 pm »
Hmm... I'd say that you're right. This looks like an omission, but I'll check the sources to make 100% sure it is not on purpose.
Laurent Gomila - SFML developer

binary1248

  • SFML Team
  • Hero Member
  • *****
  • Posts: 1405
  • I am awesome.
    • View Profile
    • The server that really shouldn't be running
Re: Is sf::GlResource not missing an explicit copy constructor?
« Reply #2 on: August 03, 2014, 09:05:00 pm »
What you are forgetting is that any (even the implicitly) declared base class copy constructor has to be explicitly called by the derived class copy constructor, else the base class constructor will be called instead. GlResource does have an implicitly declared and defined copy constructor, but since none of the derived classes explicitly call it, its normal constructor is called instead, thus incrementing the reference count as expected every time a GlResource is copy constructed.

Here is a demonstration of this.
SFGUI # SFNUL # GLS # Wyrm <- Why do I waste my time on such a useless project? Because I am awesome (first meaning).

kralo9

  • Newbie
  • *
  • Posts: 44
    • View Profile
    • Email
Re: Is sf::GlResource not missing an explicit copy constructor?
« Reply #3 on: August 03, 2014, 09:13:19 pm »
What you are forgetting is that any (even the implicitly) declared base class copy constructor has to be explicitly called by the derived class copy constructor, else the base class constructor will be called instead. GlResource does have an implicitly declared and defined copy constructor, but since none of the derived classes explicitly call it, its normal constructor is called instead, thus incrementing the reference count as expected every time a GlResource is copy constructed.

Here is a demonstration of this.

I tested this and I disagree.
I'm totally exhausted. I've got 3 children and no money! Why can't I have no children and 3 money? - Homer S.

binary1248

  • SFML Team
  • Hero Member
  • *****
  • Posts: 1405
  • I am awesome.
    • View Profile
    • The server that really shouldn't be running
Re: Is sf::GlResource not missing an explicit copy constructor?
« Reply #4 on: August 03, 2014, 09:18:01 pm »
Mind showing us your test then?

I showed you mine, and the result speaks for itself.
SFGUI # SFNUL # GLS # Wyrm <- Why do I waste my time on such a useless project? Because I am awesome (first meaning).

kralo9

  • Newbie
  • *
  • Posts: 44
    • View Profile
    • Email
Re: Is sf::GlResource not missing an explicit copy constructor?
« Reply #5 on: August 03, 2014, 09:24:42 pm »
Mind showing us your test then?

I showed you mine, and the result speaks for itself.

Oh, sorry. I did not test with an explicit copy ctor of a derived class. You are right. This calls the parent's ctor if not defined explicitly.

But still, an implicit copy ctor on both classes causes the counter not to increase. I would recommend adding an explicit copy ctor to sf::GlResource nevertheless. Just for safety.
I'm totally exhausted. I've got 3 children and no money! Why can't I have no children and 3 money? - Homer S.

Laurent

  • Administrator
  • Hero Member
  • *****
  • Posts: 32504
    • View Profile
    • SFML's website
    • Email
Re: Is sf::GlResource not missing an explicit copy constructor?
« Reply #6 on: August 03, 2014, 10:50:32 pm »
What's the point of arguing? Since it can be called, it must be defined, so that it does what it's supposed to do.
Laurent Gomila - SFML developer

kralo9

  • Newbie
  • *
  • Posts: 44
    • View Profile
    • Email
Re: Is sf::GlResource not missing an explicit copy constructor?
« Reply #7 on: August 03, 2014, 11:03:33 pm »
What's the point of arguing? Since it can be called, it must be defined, so that it does what it's supposed to do.

If you call the default copy ctor of a derived class the default copy ctor of the parent will be called too. That is the point.
I'm totally exhausted. I've got 3 children and no money! Why can't I have no children and 3 money? - Homer S.

Laurent

  • Administrator
  • Hero Member
  • *****
  • Posts: 32504
    • View Profile
    • SFML's website
    • Email
Re: Is sf::GlResource not missing an explicit copy constructor?
« Reply #8 on: August 04, 2014, 07:33:52 am »
Quote
If you call the default copy ctor of a derived class the default copy ctor of the parent will be called too. That is the point.
Yes. That's what I'm saying: since it can be called we must define it instead of using the incorrect generated one. I was saying that there's no point arguing against it. Sorry if that was not clear from my previous post.
Laurent Gomila - SFML developer

kralo9

  • Newbie
  • *
  • Posts: 44
    • View Profile
    • Email
Re: Is sf::GlResource not missing an explicit copy constructor?
« Reply #9 on: August 04, 2014, 09:21:08 am »
Yes, I misunderstood that.
I'm totally exhausted. I've got 3 children and no money! Why can't I have no children and 3 money? - Homer S.