Index: library/bdm/estim/mixtures.cpp
===================================================================
--- library/bdm/estim/mixtures.cpp (revision 477)
+++ library/bdm/estim/mixtures.cpp (revision 504)
@@ -142,5 +142,5 @@
 
 emix* MixEF::epredictor ( ) const {
-	Array<epdf*> pC ( n );
+	Array<shared_ptr<epdf> > pC ( n );
 	for ( int i = 0; i < n; i++ ) {
 		pC ( i ) = Coms ( i )->epredictor ( );
@@ -148,6 +148,5 @@
 	emix* tmp;
 	tmp = new emix( );
-	tmp->set_parameters ( weights.posterior().mean(), pC, false );
-	tmp->ownComs();
+	tmp->set_parameters ( weights.posterior().mean(), pC );
 	return tmp;
 }
Index: library/bdm/base/bdmbase.h
===================================================================
--- library/bdm/base/bdmbase.h (revision 502)
+++ library/bdm/base/bdmbase.h (revision 504)
@@ -317,14 +317,8 @@
 
 		//! Return conditional density on the given RV, the remaining rvs will be in conditioning
-		virtual mpdf* condition (const RV &rv) const  {
-			it_warning ("Not implemented");
-			return NULL;
-		}
+		virtual shared_ptr<mpdf> condition (const RV &rv) const;
 
 		//! Return marginal density on the given RV, the remainig rvs are intergrated out
-		virtual epdf* marginal (const RV &rv) const {
-			it_warning ("Not implemented");
-			return NULL;
-		}
+		virtual shared_ptr<epdf> marginal (const RV &rv) const;
 
 		//! return expected value
Index: library/bdm/base/bdmbase.cpp
===================================================================
--- library/bdm/base/bdmbase.cpp (revision 502)
+++ library/bdm/base/bdmbase.cpp (revision 504)
@@ -134,4 +134,14 @@
 }
 
+shared_ptr<mpdf> epdf::condition (const RV &rv) const {
+	it_warning ("Not implemented");
+	return shared_ptr<mpdf>();
+}
+
+shared_ptr<epdf> epdf::marginal (const RV &rv) const {
+	it_warning ("Not implemented");
+	return shared_ptr<epdf>();
+}
+
 mat epdf::sample_m ( int N ) const {
 	mat X = zeros ( dim, N );
Index: library/bdm/stat/merger.cpp
===================================================================
--- library/bdm/stat/merger.cpp (revision 487)
+++ library/bdm/stat/merger.cpp (revision 504)
@@ -157,5 +157,5 @@
 				if ( mpdfs ( i )->dimensionc() > 0 ) {
 					// Make marginal on rvc_i
-					epdf* tmp_marg = Mpred->marginal ( mpdfs ( i )->_rvc() );
+					shared_ptr<epdf> tmp_marg = Mpred->marginal ( mpdfs ( i )->_rvc() );
 					//compute vector of lw_src
 					for ( int k = 0; k < Npoints; k++ ) {
@@ -163,5 +163,4 @@
 						lw_src ( k ) += tmp_marg->evallog ( dls ( i )->get_cond ( Smp ( k ) ) );
 					}
-					delete tmp_marg;
 
 // 					sprintf ( str,"marg%d",niter );
@@ -173,5 +172,5 @@
 					///////////////
 					// There are variales unknown to mpdfs(i) : rvzs
-					mpdf* tmp_cond = Mpred->condition ( rvzs ( i ) );
+					shared_ptr<mpdf> tmp_cond = Mpred->condition ( rvzs ( i ) );
 					// Compute likelihood
 					vec lw_dbg = lw_src;
@@ -186,5 +185,4 @@
 						}
 					}
-					delete tmp_cond;
 				}
 				// Compute likelihood of the partial source
Index: library/bdm/stat/emix.cpp
===================================================================
--- library/bdm/stat/emix.cpp (revision 488)
+++ library/bdm/stat/emix.cpp (revision 504)
@@ -3,5 +3,5 @@
 namespace bdm {
 
-void emix::set_parameters ( const vec &w0, const Array<epdf*> &Coms0, bool copy ) {
+void emix::set_parameters ( const vec &w0, const Array<shared_ptr<epdf> > &Coms0 ) {
 	w = w0 / sum ( w0 );
 	dim = Coms0 ( 0 )->dimension();
@@ -14,4 +14,56 @@
 		it_assert_debug ( dim == ( Coms0 ( i )->dimension() ), "Component sizes do not match!" );
 		it_assert_debug ( !isnamed || tmp_rv.equal ( Coms0 ( i )->_rv() ), "Component RVs do not match!" );
+	}
+
+	Coms = Coms0;
+
+	if ( isnamed ) epdf::set_rv ( tmp_rv ); //coms aer already OK, no need for set_rv
+}
+
+vec emix::sample() const {
+	//Sample which component
+	vec cumDist = cumsum ( w );
+	double u0;
+#pragma omp critical
+	u0 = UniRNG.sample();
+
+	int i = 0;
+	while ( ( cumDist ( i ) < u0 ) && ( i < ( w.length() - 1 ) ) ) {
+		i++;
+	}
+
+	return Coms ( i )->sample();
+}
+
+shared_ptr<epdf> emix::marginal ( const RV &rv ) const {
+	emix *tmp = new emix();
+	shared_ptr<epdf> narrow(tmp);
+	marginal ( rv, *tmp );
+	return narrow;
+}
+
+void emix::marginal ( const RV &rv, emix &target ) const {
+	it_assert_debug ( isnamed(), "rvs are not assigned" );
+
+	Array<shared_ptr<epdf> > Cn ( Coms.length() );
+	for ( int i = 0; i < Coms.length(); i++ ) {
+		Cn ( i ) = Coms ( i )->marginal ( rv );
+	}
+
+	target.set_parameters ( w, Cn );
+}
+
+shared_ptr<mpdf> emix::condition ( const RV &rv ) const {
+	it_assert_debug ( isnamed(), "rvs are not assigned" );
+	mratio *tmp = new mratio ( this, rv );
+	return shared_ptr<mpdf>(tmp);
+}
+
+void egiwmix::set_parameters ( const vec &w0, const Array<egiw*> &Coms0, bool copy ) {
+	w = w0 / sum ( w0 );
+	dim = Coms0 ( 0 )->dimension();
+	int i;
+	for ( i = 0; i < w.length(); i++ ) {
+		it_assert_debug ( dim == ( Coms0 ( i )->dimension() ), "Component sizes do not match!" );
 	}
 	if ( copy ) {
@@ -26,58 +78,4 @@
 		destroyComs = false;
 	}
-	if ( isnamed ) epdf::set_rv ( tmp_rv ); //coms aer already OK, no need for set_rv
-}
-
-vec emix::sample() const {
-	//Sample which component
-	vec cumDist = cumsum ( w );
-	double u0;
-#pragma omp critical
-	u0 = UniRNG.sample();
-
-	int i = 0;
-	while ( ( cumDist ( i ) < u0 ) && ( i < ( w.length() - 1 ) ) ) {
-		i++;
-	}
-
-	return Coms ( i )->sample();
-}
-
-emix* emix::marginal ( const RV &rv ) const {
-	it_assert_debug ( isnamed(), "rvs are not assigned" );
-
-	Array<epdf*> Cn ( Coms.length() );
-	for ( int i = 0; i < Coms.length(); i++ ) {
-		Cn ( i ) = Coms ( i )->marginal ( rv );
-	}
-	emix* tmp = new emix();
-	tmp->set_parameters ( w, Cn, false );
-	tmp->ownComs();
-	return tmp;
-}
-
-mratio* emix::condition ( const RV &rv ) const {
-	it_assert_debug ( isnamed(), "rvs are not assigned" );
-	return new mratio ( this, rv );
-};
-
-void egiwmix::set_parameters ( const vec &w0, const Array<egiw*> &Coms0, bool copy ) {
-	w = w0 / sum ( w0 );
-	dim = Coms0 ( 0 )->dimension();
-	int i;
-	for ( i = 0; i < w.length(); i++ ) {
-		it_assert_debug ( dim == ( Coms0 ( i )->dimension() ), "Component sizes do not match!" );
-	}
-	if ( copy ) {
-		Coms.set_length ( Coms0.length() );
-		for ( i = 0; i < w.length(); i++ ) {
-			it_error ( "Not imp..." );
-			*Coms ( i ) = *Coms0 ( i );
-		}
-		destroyComs = true;
-	} else {
-		Coms = Coms0;
-		destroyComs = false;
-	}
 }
 
@@ -118,15 +116,20 @@
 }
 
-emix* egiwmix::marginal ( const RV &rv ) const {
+shared_ptr<epdf> egiwmix::marginal ( const RV &rv ) const {
+	emix *tmp = new emix();
+	shared_ptr<epdf> narrow(tmp);
+	marginal ( rv, *tmp );
+	return narrow;
+}
+
+void egiwmix::marginal ( const RV &rv, emix &target ) const {
 	it_assert_debug ( isnamed(), "rvs are not assigned" );
 
-	Array<epdf*> Cn ( Coms.length() );
+	Array<shared_ptr<epdf> > Cn ( Coms.length() );
 	for ( int i = 0; i < Coms.length(); i++ ) {
 		Cn ( i ) = Coms ( i )->marginal ( rv );
 	}
-	emix* tmp = new emix();
-	tmp->set_parameters ( w, Cn, false );
-	tmp->ownComs();
-	return tmp;
+
+	target.set_parameters ( w, Cn );
 }
 
Index: library/bdm/stat/exp_family.h
===================================================================
--- library/bdm/stat/exp_family.h (revision 488)
+++ library/bdm/stat/exp_family.h (revision 504)
@@ -99,5 +99,5 @@
 };
 
-template<class sq_T, template <typename> class TEpdf >
+template<class sq_T, template <typename> class TEpdf>
 class mlnorm;
 
@@ -142,7 +142,14 @@
 		vec variance() const {return diag (R.to_mat());}
 //	mlnorm<sq_T>* condition ( const RV &rvn ) const ; <=========== fails to cmpile. Why?
-		mpdf* condition (const RV &rvn) const ;
-		enorm<sq_T>* marginal (const RV &rv) const;
-//			epdf* marginal ( const RV &rv ) const;
+		shared_ptr<mpdf> condition ( const RV &rvn ) const;
+
+		// target not typed to mlnorm<sq_T, enorm<sq_T> > &
+		// because that doesn't compile (perhaps because we
+		// haven't finished defining enorm yet), but the type
+		// is required
+		void condition ( const RV &rvn, mpdf &target ) const;
+
+		shared_ptr<epdf> marginal (const RV &rvn ) const;
+		void marginal ( const RV &rvn, enorm<sq_T> &target ) const;
 		//!@}
 
@@ -1235,22 +1242,40 @@
 
 template<class sq_T>
-enorm<sq_T>* enorm<sq_T>::marginal (const RV &rvn) const
+shared_ptr<epdf> enorm<sq_T>::marginal ( const RV &rvn ) const
+{
+	enorm<sq_T> *tmp = new enorm<sq_T> ();
+	shared_ptr<epdf> narrow(tmp);
+	marginal ( rvn, *tmp );
+	return narrow;
+}
+
+template<class sq_T>
+void enorm<sq_T>::marginal ( const RV &rvn, enorm<sq_T> &target ) const
 {
 	it_assert_debug (isnamed(), "rv description is not assigned");
 	ivec irvn = rvn.dataind (rv);
 
-	sq_T Rn (R, irvn);  //select rows and columns of R
-
-	enorm<sq_T>* tmp = new enorm<sq_T>;
-	tmp->set_rv (rvn);
-	tmp->set_parameters (mu (irvn), Rn);
-	return tmp;
+	sq_T Rn (R, irvn);  // select rows and columns of R
+
+	target.set_rv ( rvn );
+	target.set_parameters (mu (irvn), Rn);
 }
 
 template<class sq_T>
-mpdf* enorm<sq_T>::condition (const RV &rvn) const
-{
+shared_ptr<mpdf> enorm<sq_T>::condition ( const RV &rvn ) const
+{
+	mlnorm<sq_T> *tmp = new mlnorm<sq_T> ();
+	shared_ptr<mpdf> narrow(tmp);
+	condition ( rvn, *tmp );
+	return narrow;
+}
+
+template<class sq_T>
+void enorm<sq_T>::condition ( const RV &rvn, mpdf &target ) const
+{
+	typedef mlnorm<sq_T> TMlnorm;
 
 	it_assert_debug (isnamed(), "rvs are not assigned");
+	TMlnorm &uptarget = dynamic_cast<TMlnorm &>(target);
 
 	RV rvc = rv.subt (rvn);
@@ -1276,8 +1301,7 @@
 	sq_T R_n (S11 - A *S12.T());
 
-	mlnorm<sq_T>* tmp = new mlnorm<sq_T> ();
-	tmp->set_rv (rvn); tmp->set_rvc (rvc);
-	tmp->set_parameters (A, mu1 - A*mu2, R_n);
-	return tmp;
+	uptarget.set_rv (rvn);
+	uptarget.set_rvc (rvc);
+	uptarget.set_parameters (A, mu1 - A*mu2, R_n);
 }
 
Index: library/bdm/stat/emix.h
===================================================================
--- library/bdm/stat/emix.h (revision 503)
+++ library/bdm/stat/emix.h (revision 504)
@@ -39,6 +39,8 @@
 	//! Nominator in the form of mpdf
 	const epdf* nom;
+
 	//!Denominator in the form of epdf
-	epdf* den;
+	shared_ptr<epdf> den;
+
 	//!flag for destructor
 	bool destroynom;
@@ -85,5 +87,4 @@
 	//! Default destructor
 	~mratio() {
-		delete den;
 		if ( destroynom ) {
 			delete nom;
@@ -107,7 +108,6 @@
 	vec w;
 	//! Component (epdfs)
-	Array<epdf*> Coms;
-	//!Flag if owning Coms
-	bool destroyComs;
+	Array<shared_ptr<epdf> > Coms;
+
 public:
 	//!Default constructor
@@ -115,5 +115,5 @@
 	//! Set weights \c w and components \c Coms
 	//!By default Coms are copied inside. Parameter \c copy can be set to false if Coms live externally. Use method ownComs() if Coms should be destroyed by the destructor.
-	void set_parameters ( const vec &w, const Array<epdf*> &Coms, bool copy = false );
+	void set_parameters ( const vec &w, const Array<shared_ptr<epdf> > &Coms );
 
 	vec sample() const;
@@ -164,6 +164,7 @@
 	};
 
-	emix* marginal ( const RV &rv ) const;
-	mratio* condition ( const RV &rv ) const; //why not mratio!!
+	shared_ptr<epdf> marginal ( const RV &rv ) const;
+	void marginal ( const RV &rv, emix &target ) const;
+	shared_ptr<mpdf> condition ( const RV &rv ) const;
 
 //Access methods
@@ -172,20 +173,10 @@
 		return w;
 	}
-	virtual ~emix() {
-		if ( destroyComs ) {
-			for ( int i = 0; i < Coms.length(); i++ ) {
-				delete Coms ( i );
-			}
-		}
-	}
-	//! Auxiliary function for taking ownership of the Coms()
-	void ownComs() {
-		destroyComs = true;
-	}
 
 	//!access function
-	epdf* _Coms ( int i ) {
+	shared_ptr<epdf> _Coms ( int i ) {
 		return Coms ( i );
 	}
+
 	void set_rv ( const RV &rv ) {
 		epdf::set_rv ( rv );
@@ -233,6 +224,8 @@
 	double lognc () const {
 		return 0;
-	};
-	emix* marginal ( const RV &rv ) const;
+	}
+
+	shared_ptr<epdf> marginal ( const RV &rv ) const;
+	void marginal ( const RV &rv, emix &target ) const;
 
 //Access methods
Index: library/tests/emix_test.cpp
===================================================================
--- library/tests/emix_test.cpp (revision 501)
+++ library/tests/emix_test.cpp (revision 504)
@@ -15,26 +15,26 @@
 	RV xy = concat ( x, y );
 
-	enorm<ldmat> E1;
-	E1.set_rv ( xy );
+	shared_ptr<enorm<ldmat> > E1 = new enorm<ldmat>();
+	E1->set_rv ( xy );
+	E1->set_parameters ( "1.00054 1.0455" , mat ( "0.740142 -0.259015; -0.259015 1.0302" ) );
 
-	E1.set_parameters ( "1.00054 1.0455" , mat ( "0.740142 -0.259015; -0.259015 1.0302" ) );
-	enorm<ldmat> E2;
-	E2.set_rv ( xy );
-	E2.set_parameters ( "-1.2 -0.1" , mat ( "1 0.4; 0.4 0.5" ) );
+	shared_ptr<enorm<ldmat> > E2 = new enorm<ldmat>();
+	E2->set_rv ( xy );
+	E2->set_parameters ( "-1.2 -0.1" , mat ( "1 0.4; 0.4 0.5" ) );
 
-	Array<epdf*> A1 ( 1 );
-	A1 ( 0 ) = &E1;
+	Array<shared_ptr<epdf> > A1 ( 1 );
+	A1 ( 0 ) = E1;
 
 	emix M1;
 	M1.set_rv ( xy );
-	M1.set_parameters ( vec ( "1" ), A1, false );
+	M1.set_parameters ( vec ( "1" ), A1 );
 
 	// test if ARX and emix with one ARX are the same
-	epdf* Mm = M1.marginal ( y );
-	epdf* Am = E1.marginal ( y );
-	mpdf* Mc = M1.condition ( y );
-	mpdf* Ac = E1.condition ( y );
+	shared_ptr<epdf> Mm = M1.marginal ( y );
+	shared_ptr<epdf> Am = E1->marginal ( y );
+	shared_ptr<mpdf> Mc = M1.condition ( y );
+	shared_ptr<mpdf> Ac = E1->condition ( y );
 
-	mlnorm<ldmat> *wacnd = dynamic_cast<mlnorm<ldmat> *>(Ac);
+	mlnorm<ldmat> *wacnd = dynamic_cast<mlnorm<ldmat> *>( Ac.get() );
 	CHECK(wacnd);
 	if ( wacnd ) {
@@ -51,11 +51,11 @@
 
 	// mixture with two components
-	Array<epdf*> A2 ( 2 );
-	A2 ( 0 ) = &E1;
-	A2 ( 1 ) = &E2;
+	Array<shared_ptr<epdf> > A2 ( 2 );
+	A2 ( 0 ) = E1;
+	A2 ( 1 ) = E2;
 
 	emix M2;
 	M2.set_rv ( xy );
-	M2.set_parameters ( vec ( "1" ), A2, false );
+	M2.set_parameters ( vec ( "1" ), A2 );
 
 
@@ -74,31 +74,10 @@
 	CHECK_CLOSE ( mat ( "0.740142 -0.259015; -0.259015 1.0302" ), Er, 2.0 );
 
-	epdf *nm2mrg = M2.marginal ( y );
-	CHECK ( nm2mrg );
-	shared_ptr<epdf> Mg ( nm2mrg );
-	mpdf *Cn = M2.condition ( x );
-	CHECK ( Cn );
-
-#if false
-	it_file it ( "emix_test.it" );
-	it << Name ( "Smp" ) << Smp;
-#endif
+	shared_ptr<epdf> Mg = M2.marginal ( y );
+	CHECK ( Mg.get() );
+	shared_ptr<mpdf> Cn = M2.condition ( x );
+	CHECK ( Cn.get() );
 
 	// marginal mean
 	CHECK_CLOSE ( vec ( "1.0" ), Mg->mean(), 0.1 );
-
-#if false
-	// putting them back together
-	mepdf mMg ( Mg );
-	Array<mpdf*> AA ( 2 );
-	AA ( 0 ) = Cn;
-	AA ( 1 ) = &mMg;
-	mprod mEp ( AA );
-
-	for ( int j = 0; j < N; j++ ) {
-		ll2 ( j ) = mEp.evallogcond ( Smp.get_col ( j ), vec ( 0 ) );
-	}
-	it << Name ( "ll" ) << ll;
-	it << Name ( "ll2" ) << ll2;
-#endif
 }
Index: library/tests/merger_2d_test.cpp
===================================================================
--- library/tests/merger_2d_test.cpp (revision 477)
+++ library/tests/merger_2d_test.cpp (revision 504)
@@ -70,4 +70,4 @@
 	it << Name ( "S1" ) << f1.evallog_m ( Grid );
 	it << Name ( "S2" ) << f2.evallog_m ( Grid );
-	cout << ( ( enorm<ldmat>* ) ( MP->_Coms ( 0 ) ) )->_R().to_mat() << endl;
+	cout << ( ( enorm<ldmat>* ) ( MP->_Coms ( 0 ).get() ) )->_R().to_mat() << endl;
 }
Index: library/tests/testSmp.cpp
===================================================================
--- library/tests/testSmp.cpp (revision 488)
+++ library/tests/testSmp.cpp (revision 504)
@@ -32,7 +32,7 @@
 
 	cout << "====== ENorm ====== " << endl;
-	enorm<ldmat> eN;
-	eN.set_parameters ( mu0, R );
-	mat Smp = eN.sample_m ( N );
+	shared_ptr<enorm<ldmat> > eN = new enorm<ldmat>();
+	eN->set_parameters ( mu0, R );
+	mat Smp = eN->sample_m ( N );
 
 	disp ( mu0, R.to_mat(), Smp );
@@ -49,9 +49,9 @@
 	vec a = "100000,10000";
 	vec b = a / 10.0;
-	egamma eG;
-	eG.set_parameters ( a, b );
+	shared_ptr<egamma> eG = new egamma();
+	eG->set_parameters ( a, b );
 
-	cout << eG.evallog ( a ) << endl;
-	Smp = eG.sample_m ( N );
+	cout << eG->evallog ( a ) << endl;
+	Smp = eG->sample_m ( N );
 
 	vec g_mu = elem_div ( a, b );
@@ -60,48 +60,53 @@
 
 	cout << "====== MGamma ====== " << endl;
-	mgamma mG;
+	shared_ptr<mgamma> mG = new mgamma();
 	double k = 10.0;
-	mG.set_parameters ( k, mu0 );
+	mG->set_parameters ( k, mu0 );
 
-	Smp = mG.samplecond_m ( mu0, N );
+	Smp = mG->samplecond_m ( mu0, N );
 	disp ( mu0, pow ( mu0, 2.0 ) / k, Smp );
 
 	cout << "======= EMix ======== " << endl;
-	emix eMix;
-	Array<epdf*> Coms ( 2 );
-	Coms ( 0 ) = &eG;
-	Coms ( 1 ) = &eN;
+	shared_ptr<emix> eMix = new emix();
+	Array<shared_ptr<epdf> > Coms ( 2 );
+	Coms ( 0 ) = eG;
+	Coms ( 1 ) = eN;
 
-	eMix.set_parameters ( vec_2 ( 0.5, 0.5 ), Coms );
-	vec smp = eMix.sample();
-	Smp = eMix.sample_m ( N );
-	disp ( eMix.mean(), zeros ( 2 ), Smp );
+	eMix->set_parameters ( vec_2 ( 0.5, 0.5 ), Coms );
+	vec smp = eMix->sample();
+	Smp = eMix->sample_m ( N );
+	disp ( eMix->mean(), zeros ( 2 ), Smp );
 
 	cout << "======= MEpdf ======== " << endl;
-	mepdf meMix ( &eMix );
+	mepdf meMix ( eMix );
 
 	Smp = meMix.samplecond_m ( mu0, N );
-	disp ( eMix.mean(), zeros ( 2 ), Smp );
+	disp ( eMix->mean(), zeros ( 2 ), Smp );
 
 	cout << "======= MMix ======== " << endl;
 	mmix mMix;
 	Array<shared_ptr<mpdf> > mComs ( 2 );
-	mComs ( 0 ) = &mG;
-	eN.set_mu ( vec_2 ( 0.0, 0.0 ) );
-	mepdf mEnorm ( &eN );
-	mComs ( 1 ) = &mEnorm;
+
+	// emix::set_parameters requires the first mpdf to be named
+	mG->set_rv(x);
+	mG->set_rvc(y);
+	mComs ( 0 ) = mG;
+
+	eN->set_mu ( vec_2 ( 0.0, 0.0 ) );
+	shared_ptr<mepdf> mEnorm = new mepdf ( eN );
+	mComs ( 1 ) = mEnorm;
 	mMix.set_parameters ( vec_2 ( 0.5, 0.5 ), mComs );
 
 	Smp = mMix.samplecond_m ( mu0, N );
-	disp ( 0.5*eN.mean()+0.4*eG.mean(), zeros ( 2 ), Smp );
+	disp ( 0.5 * eN->mean() + 0.4 * eG->mean(), zeros ( 2 ), Smp );
 
 	cout << "======= EProd ======== " << endl;
-	// we have to change eG.rv to y
-	eN.set_rv ( x );
-	eG.set_rv ( y );
+	// we have to change eG->rv to y
+	eN->set_rv ( x );
+	eG->set_rv ( y );
 	//create array
 	Array<mpdf*> A ( 2 );
-	mepdf meN ( &eN );
-	mepdf meG ( &eG );
+	mepdf meN ( eN.get() );
+	mepdf meG ( eG.get() );
 	A ( 0 ) = &meN;
 	A ( 1 ) = &meG;
@@ -114,5 +119,5 @@
 	vec v0 = vec ( 0 );
 	Smp = eP.samplecond ( v0, N );
-	disp ( concat ( eN.mean(), eG.mean() ), epV, Smp );
+	disp ( concat ( eN->mean(), eG->mean() ), epV, Smp );
 
 	cout << "======= eWishart ======== " << endl;
Index: library/tests/mixtures_test.cpp
===================================================================
--- library/tests/mixtures_test.cpp (revision 477)
+++ library/tests/mixtures_test.cpp (revision 504)
@@ -53,17 +53,17 @@
 	fsqmat V2 ( mat ( "2 -0.1; -0.1 2" ) );
 
-	enorm<fsqmat> C1;
-	C1.set_rv ( x );
-	C1.set_parameters ( m1, V1 );
-	enorm<fsqmat> C2;
-	C2.set_rv ( x );
-	C2.set_parameters ( m2, V2 );
+	shared_ptr<enorm<fsqmat> > C1 = new enorm<fsqmat>();
+	C1->set_rv ( x );
+	C1->set_parameters ( m1, V1 );
+	shared_ptr<enorm<fsqmat> > C2 = new enorm<fsqmat>();
+	C2->set_rv ( x );
+	C2->set_parameters ( m2, V2 );
 
-	Array<epdf*> Sim ( 2 );
-	Sim ( 0 ) = &C1;
-	Sim ( 1 ) = &C2;
+	Array<shared_ptr<epdf> > Sim ( 2 );
+	Sim ( 0 ) = C1;
+	Sim ( 1 ) = C2;
 	emix Simul;
 	Simul.set_rv ( x );
-	Simul.set_parameters ( "0.5 0.6", Sim, false );
+	Simul.set_parameters ( "0.5 0.6", Sim );
 
 	// Sample parameters
