Opened 17 years ago

Closed 13 years ago

#445 closed Feature Requests (wontfix)

Can't delete automatically in circularly included object.

Reported by: nobody Owned by: Peter Dimov
Milestone: To Be Determined Component: smart_ptr
Version: None Severity: Problem
Keywords: Cc:

Description

hi,all:
I am a chinese student.Please forgive me because of 
my poor english.
I found an error on smart pointer shared_ptr when i add 
an shared_ptr object to itself.
The code is below.Class StatusBase  is a subclass of 
Status.There is a method name addEventResult in class 
StatusBase. As you can see, EventResult is an object 
which made up of object Status( StatusBase actually 
because of Status is an abstract class ),Action and 
Event.In the case, I try to let them passed in share_ptr 
wrpper whenever in constructor or normal method.As my 
opinion,I think that will not be error and when I exit my 
program it will free the memory for me automatically. 
But in fact it will not.

                shared_ptr<Status> A( new  Status() );
                
                shared_ptr<Event> B( new  Event() );
                shared_ptr<Action> C( new  Action() );
                shared_ptr<EventResult> ABC( new  
EventResult( A, B, C ) );
                //the row below will result the object A can't 
be released, so as to the object ABC , B  and C
                A.addEventResult(  ABC  );

                //next is about the decleartion of StatusBase 
and EventResult.  For class Event and Action, it is the 
history.


	class StatusBase : public Status //{{{
	{
	public:
		StatusBase(){}
		virtual ~StatusBase(){}

	public:
		int getEventResultCount() const ;
		boost::shared_ptr<EventResult> 
getEventResult( int index ) const ;
		void addEventResult( const 
boost::shared_ptr<EventResult>& v ) ;

	private:
		std::vector< 
boost::shared_ptr<EventResult> > events ;
	};  //}}}

	class EventResult : public pqkit::Object //{{{
	{
		public:
			EventResult( const 
boost::shared_ptr<Event>& e, const 
boost::shared_ptr<Action>& a, const 
boost::shared_ptr<Status>& s ) : event(e),action
(a),newStatus(s){}
			virtual ~EventResult() {}
			
		public:
			virtual const std::string 
toString() const
			{
				return 
Object::toString() ;
			}
			virtual const 
pqkit::Class getClass() const 
			{
				return 
pqkit::Class("EventResult","inhert:Object->EventResult");
			}
		
	boost::shared_ptr<Event> getEvent() const { 
return event; }
		
	boost::shared_ptr<Action> getAction() const 
{ return action; }
		
	boost::shared_ptr<Status> getNewStatus() 
const { return newStatus; }
		
		private:
		
	boost::shared_ptr<Event> event ;
		
	boost::shared_ptr<Action> action ;
		
	boost::shared_ptr<Status> newStatus ;
	}; //}}}


                   yours JinheZeng at GDUT from China.

Change History (3)

comment:1 by Peter Dimov, 17 years ago

Logged In: YES 
user_id=305912

The inability to free circular references is a documented
limitation of shared_ptr and not a bug. I'm moving this to
Feature Requests.

comment:2 by Peter Dimov, 15 years ago

Milestone: To Be Determined
Severity: Problem

comment:3 by Peter Dimov, 13 years ago

Resolution: Nonewontfix
Status: assignedclosed
Note: See TracTickets for help on using tickets.