Index: library/bdm/base/user_info.h
===================================================================
--- library/bdm/base/user_info.h (revision 494)
+++ library/bdm/base/user_info.h (revision 527)
@@ -24,4 +24,5 @@
 #include "libconfig/libconfig.h++"
 #include "../bdmroot.h"
+#include "../shared_ptr.h"
 #include "itpp/itbase.h"
 
@@ -253,5 +254,12 @@
 	static void assert_type ( const Setting &element, Setting::Type type );
 
-	//! Method assembling a typeless instance, it is implemented in descendant class ParticularUI<T>
+	/*!
+	  \brief Method constructing a configured instance
+
+	  The returned pointer must be allocated using operator new
+	  (it's deleted at the end of its life cycle). The method is
+	  implemented in descendant class ParticularUI<T>, which knows
+	  the correct type T.
+	*/
 	virtual root* new_instance() const = 0;
 
@@ -274,4 +282,5 @@
 	//! This method converts a Setting into a real scalar
 	static void from_setting ( double &real, const Setting &element );
+
 	//! This method converts a Setting into a class T descendant
 	template<class T> static void from_setting ( T* &instance, const Setting &element ) {
@@ -287,9 +296,12 @@
 		const UI& related_UI = MappedUI::retrieve_ui ( class_name );
 
-		root* typeless_instance = related_UI.new_instance();
+		root *typeless_instance = related_UI.new_instance();
+		it_assert_debug ( typeless_instance, "UI::new_instance failed" );
 
 		instance = dynamic_cast<T*> ( typeless_instance );
-		if ( !instance )
+		if ( !instance ) {
+			delete typeless_instance;
 			throw UIException ( "class " + class_name + " is not a descendant of the desired output class. Try to call the UI::build<T> function with a different type parameter.", link.result );
+		}
 
 		try {
@@ -301,4 +313,14 @@
 	}
 
+	//! This method converts a Setting into a descendant of class
+	//! T, wrapped in an instance of shared_ptr<T> .
+	template<class T>
+	static void from_setting ( shared_ptr<T> &instance, const Setting &element ) {
+		T *tmp_inst = 0;
+		from_setting ( tmp_inst, element );
+		it_assert_debug ( tmp_inst, "UI::from_setting failed" );
+		instance = tmp_inst;
+	}
+
 	//! This methods converts a Setting into a new templated array of type Array<T>
 	template<class T> static void from_setting ( Array<T> &array_to_load, const Setting &element ) {
@@ -320,5 +342,7 @@
 	//! The exception can help to find the place where the template is misused and also to correct it.
 	template<class T> static void from_setting ( T &variable_to_load, const Setting &element ) {
-		throw UIException ( "from_setting is not implemented for this type", element );
+		std::string msg = "from_setting is not implemented for type ";
+		msg += typeid(T).name();
+		throw UIException ( msg, element );
 	}
 
@@ -341,14 +365,15 @@
 	//! The new instance of type T* is constructed and initialized with values stored in the Setting element[name]
 	//!
-	//! If there is not any sub-element named #name, the null pointer is returned.
-	template<class T> static T* build ( const Setting &element, const string &name, SettingPresence settingPresence = optional ) {
+	//! If there is not any sub-element named #name and settingPresence is #optional, an empty shared_ptr<T> is returned. When settingPresence is #compulsory, the returned shared_ptr<T> is never empty (an exception is thrown when the object isn't found).
+	template<class T>
+	static shared_ptr<T> build ( const Setting &element, const string &name, SettingPresence settingPresence = optional ) {
 		if ( !element.exists ( name ) ) {
 			if ( settingPresence == optional )
-				return NULL;
+				return shared_ptr<T>();
 			else
 				throw UIException ( "the compulsory Setting named \"" + name + "\" is missing", element );
 		}
 
-		T* instance;
+		shared_ptr<T> instance;
 		from_setting<T> ( instance, to_child_setting ( element, name ) );
 		return instance;
@@ -483,4 +508,8 @@
 	}
 
+	template< class T> static void save ( const shared_ptr<T> &instance, Setting &element, const string &name = "" ) {
+		save<T> ( instance.get(), element, name );
+	}
+
 	//! An Array<T> instance is stored in the new child Setting appended to the passed element
 	template<class T> static void save ( const Array<T> &array_to_save, Setting &element, const string &name = "" ) {
Index: library/bdm/base/bdmbase.h
===================================================================
--- library/bdm/base/bdmbase.h (revision 513)
+++ library/bdm/base/bdmbase.h (revision 527)
@@ -379,8 +379,7 @@
 		//!@}
 		void from_setting (const Setting &set) {
-			RV* r = UI::build<RV> (set, "rv");
+			shared_ptr<RV> r = UI::build<RV> ( set, "rv", UI::optional );
 			if (r) {
 				set_rv (*r);
-				delete r;
 			}
 		}
@@ -455,5 +454,6 @@
 			return rvc;
 		}
-		int dimension() {
+
+		int dimension() const {
 			return ep->dimension();
 		}
Index: library/bdm/base/datasources.h
===================================================================
--- library/bdm/base/datasources.h (revision 477)
+++ library/bdm/base/datasources.h (revision 527)
@@ -173,5 +173,5 @@
 	};
 	//! Set
-	void set_drv ( RV &yrv, RV &urv, RV &rrv ) {
+	void set_drv ( const RV &yrv, const RV &urv, const RV &rrv ) {
 		Rrv = rrv;
 		Urv = urv;
@@ -265,9 +265,12 @@
 
 class stateDS : public DS {
+private:
+	//!conditional pdf of the state evolution \f$ f(x_t|x_{t-1}) \f$
+	shared_ptr<mpdf> IM;
+
+	//!conditional pdf of the observations \f$ f(d_t|x_t) \f$
+	shared_ptr<mpdf> OM;
+
 protected:
-	//!conditional pdf of the state evolution \f$ f(x_t|x_{t-1}) \f$
-	mpdf* IM;
-	//!conditional pdf of the observations \f$ f(d_t|x_t) \f$
-	mpdf* OM;
 	//! result storage
 	vec dt;
@@ -278,4 +281,5 @@
 	//! Logger
 	int L_xt;
+
 public:
 	void getdata ( vec &dt0 ) {
@@ -286,17 +290,14 @@
 	}
 
-	stateDS ( mpdf* IM0, mpdf* OM0, int usize ) : DS ( ), IM ( IM0 ), OM ( OM0 ),
-			dt ( OM0->dimension() ), xt ( IM0->dimension() ), ut ( usize ) {}
-
-	stateDS() {}
-
-	~stateDS() {
-		delete IM;
-		delete OM;
-	}
+	stateDS ( const shared_ptr<mpdf> &IM0, const shared_ptr<mpdf> &OM0, int usize ) : IM ( IM0 ), OM ( OM0 ),
+		dt ( OM0->dimension() ), xt ( IM0->dimension() ),
+		ut ( usize ), L_xt(0) { }
+
+	stateDS() : L_xt(0) { }
+
 	virtual void step() {
 		xt = IM->samplecond ( concat ( xt, ut ) );
 		dt = OM->samplecond ( concat ( xt, ut ) );
-	};
+	}
 
 	virtual void log_add ( logger &L ) {
Index: library/bdm/base/bdmbase.cpp
===================================================================
--- library/bdm/base/bdmbase.cpp (revision 507)
+++ library/bdm/base/bdmbase.cpp (revision 527)
@@ -169,14 +169,12 @@
 
 void mpdf::from_setting ( const Setting &set ) {
-	RV *r = UI::build<RV> ( set, "rv" );
+	shared_ptr<RV> r = UI::build<RV> ( set, "rv", UI::optional );
 	if ( r ) {
 		set_rv ( *r );
-		delete r;
-	}
-
-	r = UI::build<RV> ( set, "rvc" );
+	}
+
+	r = UI::build<RV> ( set, "rvc", UI::optional );
 	if ( r ) {
 		set_rvc ( *r );
-		delete r;
 	}
 }
Index: library/bdm/base/datasources.cpp
===================================================================
--- library/bdm/base/datasources.cpp (revision 477)
+++ library/bdm/base/datasources.cpp (revision 527)
@@ -57,7 +57,7 @@
 
 void ArxDS::from_setting ( const Setting &set ) {
-	RV *yrv = UI::build<RV> ( set, "y" , UI::compulsory );
-	RV *urv = UI::build<RV> ( set, "u" , UI::compulsory );
-	RV *rrv = UI::build<RV> ( set, "rgr" , UI::compulsory );
+	shared_ptr<RV> yrv = UI::build<RV> ( set, "y" , UI::compulsory );
+	shared_ptr<RV> urv = UI::build<RV> ( set, "u" , UI::compulsory );
+	shared_ptr<RV> rrv = UI::build<RV> ( set, "rgr" , UI::compulsory );
 
 	mat Th;
@@ -112,5 +112,5 @@
 
 void ITppFileDS::from_setting ( const Setting &set ) {
-	RV* rvtmp = UI::build<RV> ( set, "rv" , UI::compulsory );
+	shared_ptr<RV> rvtmp = UI::build<RV> ( set, "rv" , UI::compulsory );
 
 	it_file it ( set["filename"] );
@@ -131,5 +131,7 @@
 	ut.set_length ( 0 );
 
+#if 0
 	RV* rvtmp = UI::build<RV> ( set["IM"], "rvu", UI::compulsory );
 	//set_drv(rvtmp);
+#endif
 }
