how to help an object to delete itself the boost way
Hi, In my code there are objects created, which delete themselves. They can do it in the constructor, or later in some function. This is ugly, and I'm getting some wierd behaviour, probably because of this bad design. Is there a good Boost way of resolving my problem? Thanks, Irek
In my code there are objects created, which delete themselves. They can do it in the constructor, or later in some function. This is ugly, and I'm getting some wierd behaviour, probably because of this bad design.
I'm at a loss to understand how an object can possibly delete itself in it's own constructor...? Maybe you were looking for smart pointers to manage object lifetime? John.
My objects can delete themselves in two cases: 1) in the constructor, when they realize they cannot handle the job they are supposed to do, because they cannot allocate the resources required, 2) in a member function, when they finish their job and free their resources. I guess that in the constructor I should throw an exception, instead of trying to delete the object. So the first case is resolved. The second case is tricky. I could keep a container of smart pointers to my objects, and delete objects by removing the pointer from the container. The problem is that this removal should be initialized by the object itself, i.e., in a member function a pointer is removed from the container, but then that function operates on an object that was just deleted. Irek W dniu 17.12.2015 o 19:20, John Maddock pisze:
In my code there are objects created, which delete themselves. They can do it in the constructor, or later in some function. This is ugly, and I'm getting some wierd behaviour, probably because of this bad design.
I'm at a loss to understand how an object can possibly delete itself in it's own constructor...?
Maybe you were looking for smart pointers to manage object lifetime?
John. _______________________________________________ Boost-users mailing list Boost-users@lists.boost.org http://lists.boost.org/mailman/listinfo.cgi/boost-users
Hi Ireneusz, Basically your class is a resource handle. If the constructor cannot complete, the accepted approach is to throw an exception. If a member function finishes the job and frees the resources, have that member function modify the object's member variables so the destructor can recognise that the resources have been freed and it doesn't need to do anything about those resources. This approach is called RAII and see this page:- https://en.wikipedia.org/wiki/Resource_Acquisition_Is_Initialization BW, Ian -- -- ACCU - Professionalism in programming - http://www.accu.org -- My writing - https://sites.google.com/site/ianbruntlett/ -- Free Software page - https://sites.google.com/site/ianbruntlett/home/free-software
Hi Ian, Thanks for your email. I only wonder how an object should trigger its deletion... Best, Irek W dniu 17.12.2015 o 20:38, Ian Bruntlett pisze:
Hi Ireneusz,
Basically your class is a resource handle. If the constructor cannot complete, the accepted approach is to throw an exception.
If a member function finishes the job and frees the resources, have that member function modify the object's member variables so the destructor can recognise that the resources have been freed and it doesn't need to do anything about those resources.
This approach is called RAII and see this page:- https://en.wikipedia.org/wiki/Resource_Acquisition_Is_Initialization
BW,
Ian
-- -- ACCU - Professionalism in programming - http://www.accu.org -- My writing - https://sites.google.com/site/ianbruntlett/ -- Free Software page - https://sites.google.com/site/ianbruntlett/home/free-software
_______________________________________________ Boost-users mailing list Boost-users@lists.boost.org http://lists.boost.org/mailman/listinfo.cgi/boost-users
Hi Ireneusz,
On 17 December 2015 at 20:15, Ireneusz Szcześniak wrote: Thanks for your email. I only wonder how an object should trigger its
deletion... That could take a lot of explaining. I'll take the easy way out and
recommend you read "The C++ Programming Language 4th Edition" by Bjarne
Stroustrup.
BW,
Ian
--
-- ACCU - Professionalism in programming - http://www.accu.org
-- My writing - https://sites.google.com/site/ianbruntlett/
-- Free Software page -
https://sites.google.com/site/ianbruntlett/home/free-software
On 12/17/15 3:15 PM, Ireneusz Szcześniak wrote:
Hi Ian,
Thanks for your email. I only wonder how an object should trigger its deletion...
Best, Irek The big issue that I see is how do you know that the object was created via new? (that is the only sort of object that CAN be deleted).
Also, if the object deletes itself, it somehow must make sure that no one else has kept a copy of its address and might try to access after it has deleted itself. Both of these questions, and some of your comments, point somewhat to the idea that it might not be the object that wants to delete itself, but that the object may be part of some framework, and it should inform the framework that it is done, and the framework can then remove the object and delete it. -- Richard Damon
On December 17, 2015 9:50:20 PM EST, Richard Damon
On 12/17/15 3:15 PM, Ireneusz Szcześniak wrote:
Hi Ian,
Thanks for your email. I only wonder how an object should trigger its deletion...
Best, Irek The big issue that I see is how do you know that the object was created
via new? (that is the only sort of object that CAN be deleted).
Also, if the object deletes itself, it somehow must make sure that no one else has kept a copy of its address and might try to access after it has deleted itself.
Both of these questions, and some of your comments, point somewhat to the idea that it might not be the object that wants to delete itself, but that the object may be part of some framework, and it should inform
the framework that it is done, and the framework can then remove the object and delete it.
Exactly my observations. Some supervising, managing, contextual pattern must be in play.
-- Richard Damon
_______________________________________________ Boost-users mailing list Boost-users@lists.boost.org http://lists.boost.org/mailman/listinfo.cgi/boost-users
-- Sent from my Android device with K-9 Mail. Please excuse my brevity.
Richard, your comments are spot-on. The problem is that the object should not care how it was created. Now I still remember that these objects are dynamically created, but later I might forget that, and create objects differently, thus causing problems. Now it's clear for me, that objects should not delete themselves. As you mention, an object should notify some piece of code to delete it later. That's how I implemented it just now. Thanks for your advice! On 18.12.2015 03:50, Richard Damon wrote:
On 12/17/15 3:15 PM, Ireneusz Szcześniak wrote:
Hi Ian,
Thanks for your email. I only wonder how an object should trigger its deletion...
Best, Irek The big issue that I see is how do you know that the object was created via new? (that is the only sort of object that CAN be deleted).
Also, if the object deletes itself, it somehow must make sure that no one else has kept a copy of its address and might try to access after it has deleted itself.
Both of these questions, and some of your comments, point somewhat to the idea that it might not be the object that wants to delete itself, but that the object may be part of some framework, and it should inform the framework that it is done, and the framework can then remove the object and delete it.
On Thu, Dec 17, 2015 at 1:38 PM, Ian Bruntlett
If a member function finishes the job and frees the resources, have that member function modify the object's member variables so the destructor can recognise that the resources have been freed and it doesn't need to do anything about those resources.
This approach is called RAII and see this page:- https://en.wikipedia.org/wiki/Resource_Acquisition_Is_Initialization
"delete this" sounds more like Resource Suicide and not RAII. -- Chris Cleeland
Hi Chris,
On 17 December 2015 at 20:43, Chris Cleeland
On Thu, Dec 17, 2015 at 1:38 PM, Ian Bruntlett
wrote: If a member function finishes the job and frees the resources, have that member function modify the object's member variables so the destructor can recognise that the resources have been freed and it doesn't need to do anything about those resources.
This approach is called RAII and see this page:- https://en.wikipedia.org/wiki/Resource_Acquisition_Is_Initialization
"delete this" sounds more like Resource Suicide and not RAII.
Yes, the original question was this:- "In my code there are objects created, which delete themselves. They can do it in the constructor, or later in some function. This is ugly, and I'm getting some wierd behaviour, probably because of this bad design." Then another question was brought up, which indicated resource suicide. I've got to go offline now so I can't help much more than I have done. BW, Ian -- -- ACCU - Professionalism in programming - http://www.accu.org -- My writing - https://sites.google.com/site/ianbruntlett/ -- Free Software page - https://sites.google.com/site/ianbruntlett/home/free-software
participants (6)
-
Chris Cleeland
-
Ian Bruntlett
-
Ireneusz Szcześniak
-
John Maddock
-
Michael
-
Richard Damon