[Kstars-devel] Possible double free in skyobject

Khudyakov Alexey alexey.skladnoy at gmail.com
Sun May 31 19:38:10 CEST 2009


On Sunday 31 of May 2009 14:51:06 Akarsh Simha wrote:
> > I've just found a problem in SkyObject. It's possible to have double free
> > in the destructor. If object is copied and has auxInfo then both object
> > will share same pointer to auxInfo. After destruction of one object
> > second will hold pointer to already destroyed object which is problem on
> > its own. When destroyed it will delete again leading to double free.
> >
> > Proposed solution - use QSharedDataPointer to hold data. With that we get
> > implicit data sharing for free. Explicit copy constructor is not needed,
> > default will do just fine. (And it doen't make much sense to have custom
> > copy ctor and no custom assignment operator anyway)
>
> Hmm... I'm okay with this proposal. I don't think we have a situation
> in KStars where we write different data to two copies of a SkyObject.
>
We have. At conjuctions tool.

After some consideration. I desided that it's better to hide copy constructor 
at all. It's not safe to copy objects with it. For example

> SkyObject *p = new KSSun(..);
> SkyObject copy = SkyObject(*p);

In this case only SkyObject part will be copied. So it's better to hide both 
copy ctor nd assignment operator and provide virtual function which actually 
copy objects like this:

> virtual SkyObject* SkyObject::clone() const { return new SkyObejct(*this); }
> virtual TrailObject* TrailObject::clone() const 
>   { return new TrailObejct(*this); }

Such function is required for each class in SkyObject family. But it's only 
safe method to create copy of object.


Anyway. This situation is kind of landmine. No one knows when will it explode.



More information about the Kstars-devel mailing list