diff --git a/doc/contributions.txt b/doc/contributions.txt
index 236112334391e167ed8a7563476533103b1fea73..4d2584f1eb3675bb2fd7ff494758264e50995599 100644
--- a/doc/contributions.txt
+++ b/doc/contributions.txt
@@ -79,6 +79,7 @@ Dzonatas Sol
 	VWR-1704
 	VWR-1705
 	VWR-1729
+	VWR-1812
 Eddy Stryker
 	VWR-15
 	VWR-23
@@ -224,6 +225,9 @@ Nicholaz Beresford
 	VWR-2142
 	VWR-2152
 	VWR-2614
+	VWR-2411
+	VWR-2412
+	VWR-2684
 Nounouch Hapmouche
 	VWR-238
 Paul Churchill
diff --git a/indra/llcharacter/lleditingmotion.cpp b/indra/llcharacter/lleditingmotion.cpp
index 6e878429fa829ed52e54384b2eaa5a3fbca3eeef..34fb6c423ad4079bd6eabfc7158fe735a347db02 100644
--- a/indra/llcharacter/lleditingmotion.cpp
+++ b/indra/llcharacter/lleditingmotion.cpp
@@ -64,6 +64,12 @@ LLEditingMotion::LLEditingMotion( const LLUUID &id) : LLMotion(id)
 	mElbowJoint.addChild( &mWristJoint );
 
 	mName = "editing";
+
+	mParentState = new LLJointState;
+	mShoulderState = new LLJointState;
+	mElbowState = new LLJointState;
+	mWristState = new LLJointState;
+	mTorsoState = new LLJointState;
 }
 
 
@@ -93,13 +99,13 @@ LLMotion::LLMotionInitStatus LLEditingMotion::onInitialize(LLCharacter *characte
 	}
 
 	// get the shoulder, elbow, wrist joints from the character
-	mParentState.setJoint( mCharacter->getJoint("mShoulderLeft")->getParent() );
-	mShoulderState.setJoint( mCharacter->getJoint("mShoulderLeft") );
-	mElbowState.setJoint( mCharacter->getJoint("mElbowLeft") );
-	mWristState.setJoint( mCharacter->getJoint("mWristLeft") );
-	mTorsoState.setJoint( mCharacter->getJoint("mTorso"));
+	mParentState->setJoint( mCharacter->getJoint("mShoulderLeft")->getParent() );
+	mShoulderState->setJoint( mCharacter->getJoint("mShoulderLeft") );
+	mElbowState->setJoint( mCharacter->getJoint("mElbowLeft") );
+	mWristState->setJoint( mCharacter->getJoint("mWristLeft") );
+	mTorsoState->setJoint( mCharacter->getJoint("mTorso"));
 
-	if ( ! mParentState.getJoint() )
+	if ( ! mParentState->getJoint() )
 	{
 		llinfos << getName() << ": Can't get parent joint." << llendl;
 		return STATUS_FAILURE;
@@ -108,25 +114,25 @@ LLMotion::LLMotionInitStatus LLEditingMotion::onInitialize(LLCharacter *characte
 	mWristOffset = LLVector3(0.0f, 0.2f, 0.0f);
 
 	// add joint states to the pose
-	mShoulderState.setUsage(LLJointState::ROT);
-	mElbowState.setUsage(LLJointState::ROT);
-	mTorsoState.setUsage(LLJointState::ROT);
-	mWristState.setUsage(LLJointState::ROT);
-	addJointState( &mShoulderState );
-	addJointState( &mElbowState );
-	addJointState( &mTorsoState );
-	addJointState( &mWristState );
+	mShoulderState->setUsage(LLJointState::ROT);
+	mElbowState->setUsage(LLJointState::ROT);
+	mTorsoState->setUsage(LLJointState::ROT);
+	mWristState->setUsage(LLJointState::ROT);
+	addJointState( mShoulderState );
+	addJointState( mElbowState );
+	addJointState( mTorsoState );
+	addJointState( mWristState );
 
 	// propagate joint positions to kinematic chain
-	mParentJoint.setPosition(	mParentState.getJoint()->getWorldPosition() );
-	mShoulderJoint.setPosition(	mShoulderState.getJoint()->getPosition() );
-	mElbowJoint.setPosition(	mElbowState.getJoint()->getPosition() );
-	mWristJoint.setPosition(	mWristState.getJoint()->getPosition() + mWristOffset );
+	mParentJoint.setPosition(	mParentState->getJoint()->getWorldPosition() );
+	mShoulderJoint.setPosition(	mShoulderState->getJoint()->getPosition() );
+	mElbowJoint.setPosition(	mElbowState->getJoint()->getPosition() );
+	mWristJoint.setPosition(	mWristState->getJoint()->getPosition() + mWristOffset );
 
 	// propagate current joint rotations to kinematic chain
-	mParentJoint.setRotation(	mParentState.getJoint()->getWorldRotation() );
-	mShoulderJoint.setRotation(	mShoulderState.getJoint()->getRotation() );
-	mElbowJoint.setRotation(	mElbowState.getJoint()->getRotation() );
+	mParentJoint.setRotation(	mParentState->getJoint()->getWorldRotation() );
+	mShoulderJoint.setRotation(	mShoulderState->getJoint()->getRotation() );
+	mElbowJoint.setRotation(	mElbowState->getJoint()->getRotation() );
 
 	// connect the ikSolver to the chain
 	mIKSolver.setPoleVector( LLVector3( -1.0f, 1.0f, 0.0f ) );
@@ -144,15 +150,15 @@ LLMotion::LLMotionInitStatus LLEditingMotion::onInitialize(LLCharacter *characte
 BOOL LLEditingMotion::onActivate()
 {
 	// propagate joint positions to kinematic chain
-	mParentJoint.setPosition(	mParentState.getJoint()->getWorldPosition() );
-	mShoulderJoint.setPosition(	mShoulderState.getJoint()->getPosition() );
-	mElbowJoint.setPosition(	mElbowState.getJoint()->getPosition() );
-	mWristJoint.setPosition(	mWristState.getJoint()->getPosition() + mWristOffset );
+	mParentJoint.setPosition(	mParentState->getJoint()->getWorldPosition() );
+	mShoulderJoint.setPosition(	mShoulderState->getJoint()->getPosition() );
+	mElbowJoint.setPosition(	mElbowState->getJoint()->getPosition() );
+	mWristJoint.setPosition(	mWristState->getJoint()->getPosition() + mWristOffset );
 
 	// propagate current joint rotations to kinematic chain
-	mParentJoint.setRotation(	mParentState.getJoint()->getWorldRotation() );
-	mShoulderJoint.setRotation(	mShoulderState.getJoint()->getRotation() );
-	mElbowJoint.setRotation(	mElbowState.getJoint()->getRotation() );
+	mParentJoint.setRotation(	mParentState->getJoint()->getWorldRotation() );
+	mShoulderJoint.setRotation(	mShoulderState->getJoint()->getRotation() );
+	mElbowJoint.setRotation(	mElbowState->getJoint()->getRotation() );
 
 	return TRUE;
 }
@@ -182,15 +188,15 @@ BOOL LLEditingMotion::onUpdate(F32 time, U8* joint_mask)
 	focus_pt += mCharacter->getCharacterPosition();
 
 	// propagate joint positions to kinematic chain
-	mParentJoint.setPosition(	mParentState.getJoint()->getWorldPosition() );
-	mShoulderJoint.setPosition(	mShoulderState.getJoint()->getPosition() );
-	mElbowJoint.setPosition(	mElbowState.getJoint()->getPosition() );
-	mWristJoint.setPosition(	mWristState.getJoint()->getPosition() + mWristOffset );
+	mParentJoint.setPosition(	mParentState->getJoint()->getWorldPosition() );
+	mShoulderJoint.setPosition(	mShoulderState->getJoint()->getPosition() );
+	mElbowJoint.setPosition(	mElbowState->getJoint()->getPosition() );
+	mWristJoint.setPosition(	mWristState->getJoint()->getPosition() + mWristOffset );
 
 	// propagate current joint rotations to kinematic chain
-	mParentJoint.setRotation(	mParentState.getJoint()->getWorldRotation() );
-	mShoulderJoint.setRotation(	mShoulderState.getJoint()->getRotation() );
-	mElbowJoint.setRotation(	mElbowState.getJoint()->getRotation() );
+	mParentJoint.setRotation(	mParentState->getJoint()->getWorldRotation() );
+	mShoulderJoint.setRotation(	mShoulderState->getJoint()->getRotation() );
+	mElbowJoint.setRotation(	mElbowState->getJoint()->getRotation() );
 
 	// update target position from character
 	LLVector3 target = focus_pt - mParentJoint.getPosition();
@@ -199,7 +205,7 @@ BOOL LLEditingMotion::onUpdate(F32 time, U8* joint_mask)
 	LLVector3 edit_plane_normal(1.f / F_SQRT2, 1.f / F_SQRT2, 0.f);
 	edit_plane_normal.normVec();
 
-	edit_plane_normal.rotVec(mTorsoState.getJoint()->getWorldRotation());
+	edit_plane_normal.rotVec(mTorsoState->getJoint()->getWorldRotation());
 	
 	F32 dot = edit_plane_normal * target;
 
@@ -236,9 +242,9 @@ BOOL LLEditingMotion::onUpdate(F32 time, U8* joint_mask)
 		// now put blended values back into joints
 		llassert(shoulderRot.isFinite());
 		llassert(elbowRot.isFinite());
-		mShoulderState.setRotation(shoulderRot);
-		mElbowState.setRotation(elbowRot);
-		mWristState.setRotation(LLQuaternion::DEFAULT);
+		mShoulderState->setRotation(shoulderRot);
+		mElbowState->setRotation(elbowRot);
+		mWristState->setRotation(LLQuaternion::DEFAULT);
 	}
 
 	mCharacter->setAnimationData("Hand Pose", &sHandPose);
diff --git a/indra/llcharacter/lleditingmotion.h b/indra/llcharacter/lleditingmotion.h
index 18bfd7f88ab5c1d8b221bb74860ca6ac430e802e..60ec4a34d9df873000c586d5d42f1f5286b9bd27 100644
--- a/indra/llcharacter/lleditingmotion.h
+++ b/indra/llcharacter/lleditingmotion.h
@@ -116,11 +116,11 @@ class LLEditingMotion :
 	LLCharacter			*mCharacter;
 	LLVector3			mWristOffset;
 
-	LLJointState		mParentState;
-	LLJointState		mShoulderState;
-	LLJointState		mElbowState;
-	LLJointState		mWristState;
-	LLJointState		mTorsoState;
+	LLPointer<LLJointState> mParentState;
+	LLPointer<LLJointState> mShoulderState;
+	LLPointer<LLJointState> mElbowState;
+	LLPointer<LLJointState> mWristState;
+	LLPointer<LLJointState> mTorsoState;
 
 	LLJoint				mParentJoint;
 	LLJoint				mShoulderJoint;
diff --git a/indra/llcharacter/llheadrotmotion.cpp b/indra/llcharacter/llheadrotmotion.cpp
index 55af2693769fe1a959a84a3f079aff354766bdf6..7e25cc457e4a5bda87a03d5b95a1a6795ab703b5 100644
--- a/indra/llcharacter/llheadrotmotion.cpp
+++ b/indra/llcharacter/llheadrotmotion.cpp
@@ -82,6 +82,10 @@ LLHeadRotMotion::LLHeadRotMotion(const LLUUID &id) :
 	mHeadJoint(NULL)
 {
 	mName = "head_rot";
+
+	mTorsoState = new LLJointState;
+	mNeckState = new LLJointState;
+	mHeadState = new LLJointState;
 }
 
 
@@ -130,34 +134,34 @@ LLMotion::LLMotionInitStatus LLHeadRotMotion::onInitialize(LLCharacter *characte
 		return STATUS_FAILURE;
 	}
 
-	mTorsoState.setJoint( character->getJoint("mTorso") );
-	if ( ! mTorsoState.getJoint() )
+	mTorsoState->setJoint( character->getJoint("mTorso") );
+	if ( ! mTorsoState->getJoint() )
 	{
 		llinfos << getName() << ": Can't get torso joint." << llendl;
 		return STATUS_FAILURE;
 	}
 
-	mNeckState.setJoint( character->getJoint("mNeck") );
-	if ( ! mNeckState.getJoint() )
+	mNeckState->setJoint( character->getJoint("mNeck") );
+	if ( ! mNeckState->getJoint() )
 	{
 		llinfos << getName() << ": Can't get neck joint." << llendl;
 		return STATUS_FAILURE;
 	}
 
-	mHeadState.setJoint( character->getJoint("mHead") );
-	if ( ! mHeadState.getJoint() )
+	mHeadState->setJoint( character->getJoint("mHead") );
+	if ( ! mHeadState->getJoint() )
 	{
 		llinfos << getName() << ": Can't get head joint." << llendl;
 		return STATUS_FAILURE;
 	}
 
-	mTorsoState.setUsage(LLJointState::ROT);
-	mNeckState.setUsage(LLJointState::ROT);
-	mHeadState.setUsage(LLJointState::ROT);
+	mTorsoState->setUsage(LLJointState::ROT);
+	mNeckState->setUsage(LLJointState::ROT);
+	mHeadState->setUsage(LLJointState::ROT);
 
-	addJointState( &mTorsoState );
-	addJointState( &mNeckState );
-	addJointState( &mHeadState );
+	addJointState( mTorsoState );
+	addJointState( mNeckState );
+	addJointState( mHeadState );
 
 	mLastHeadRot.loadIdentity();
 
@@ -240,16 +244,16 @@ BOOL LLHeadRotMotion::onUpdate(F32 time, U8* joint_mask)
 	// Set torso target rotation such that it lags behind the head rotation
 	// by a fixed amount.
 	LLQuaternion torso_rot_local = nlerp(TORSO_LAG, LLQuaternion::DEFAULT, head_rot_local );
-	mTorsoState.setRotation( nlerp(torso_slerp_amt, mTorsoState.getRotation(), torso_rot_local) );
+	mTorsoState->setRotation( nlerp(torso_slerp_amt, mTorsoState->getRotation(), torso_rot_local) );
 
 	head_rot_local = nlerp(head_slerp_amt, mLastHeadRot, head_rot_local);
 	mLastHeadRot = head_rot_local;
 
 	// Set the head rotation.
-	LLQuaternion torsoRotLocal =  mNeckState.getJoint()->getParent()->getWorldRotation() * currentInvRootRotWorld;
+	LLQuaternion torsoRotLocal =  mNeckState->getJoint()->getParent()->getWorldRotation() * currentInvRootRotWorld;
 	head_rot_local = head_rot_local * ~torsoRotLocal;
-	mNeckState.setRotation( nlerp(NECK_LAG, LLQuaternion::DEFAULT, head_rot_local) );
-	mHeadState.setRotation( nlerp(1.f - NECK_LAG, LLQuaternion::DEFAULT, head_rot_local));
+	mNeckState->setRotation( nlerp(NECK_LAG, LLQuaternion::DEFAULT, head_rot_local) );
+	mHeadState->setRotation( nlerp(1.f - NECK_LAG, LLQuaternion::DEFAULT, head_rot_local));
 
 	return TRUE;
 }
@@ -284,6 +288,9 @@ LLEyeMotion::LLEyeMotion(const LLUUID &id) : LLMotion(id)
 	mHeadJoint = NULL;
 
 	mName = "eye_rot";
+
+	mLeftEyeState = new LLJointState;
+	mRightEyeState = new LLJointState;
 }
 
 
@@ -309,25 +316,25 @@ LLMotion::LLMotionInitStatus LLEyeMotion::onInitialize(LLCharacter *character)
 		return STATUS_FAILURE;
 	}
 
-	mLeftEyeState.setJoint( character->getJoint("mEyeLeft") );
-	if ( ! mLeftEyeState.getJoint() )
+	mLeftEyeState->setJoint( character->getJoint("mEyeLeft") );
+	if ( ! mLeftEyeState->getJoint() )
 	{
 		llinfos << getName() << ": Can't get left eyeball joint." << llendl;
 		return STATUS_FAILURE;
 	}
 
-	mRightEyeState.setJoint( character->getJoint("mEyeRight") );
-	if ( ! mRightEyeState.getJoint() )
+	mRightEyeState->setJoint( character->getJoint("mEyeRight") );
+	if ( ! mRightEyeState->getJoint() )
 	{
 		llinfos << getName() << ": Can't get Right eyeball joint." << llendl;
 		return STATUS_FAILURE;
 	}
 
-	mLeftEyeState.setUsage(LLJointState::ROT);
-	mRightEyeState.setUsage(LLJointState::ROT);
+	mLeftEyeState->setUsage(LLJointState::ROT);
+	mRightEyeState->setUsage(LLJointState::ROT);
 
-	addJointState( &mLeftEyeState );
-	addJointState( &mRightEyeState );
+	addJointState( mLeftEyeState );
+	addJointState( mRightEyeState );
 
 	return STATUS_SUCCESS;
 }
@@ -443,11 +450,15 @@ BOOL LLEyeMotion::onUpdate(F32 time, U8* joint_mask)
 		target_eye_rot = LLQuaternion(eye_look_at, left, up);
 		// convert target rotation to head-local coordinates
 		target_eye_rot *= ~mHeadJoint->getWorldRotation();
+		// eliminate any Euler roll - we're lucky that roll is applied last.
+		F32 roll, pitch, yaw;
+		target_eye_rot.getEulerAngles(&roll, &pitch, &yaw);
+		target_eye_rot.setQuat(0.0f, pitch, yaw);
 		// constrain target orientation to be in front of avatar's face
 		target_eye_rot.constrain(EYE_ROT_LIMIT_ANGLE);
 
 		// calculate vergence
-		F32 interocular_dist = (mLeftEyeState.getJoint()->getWorldPosition() - mRightEyeState.getJoint()->getWorldPosition()).magVec();
+		F32 interocular_dist = (mLeftEyeState->getJoint()->getWorldPosition() - mRightEyeState->getJoint()->getWorldPosition()).magVec();
 		vergence = -atan2((interocular_dist / 2.f), lookAtDistance);
 		llclamp(vergence, -F_PI_BY_TWO, 0.f);
 	}
@@ -495,8 +506,8 @@ BOOL LLEyeMotion::onUpdate(F32 time, U8* joint_mask)
 	vergence_quat.transQuat();
 	right_eye_rot = vergence_quat * eye_jitter_rot * right_eye_rot;
 
-	mLeftEyeState.setRotation( left_eye_rot );
-	mRightEyeState.setRotation( right_eye_rot );
+	mLeftEyeState->setRotation( left_eye_rot );
+	mRightEyeState->setRotation( right_eye_rot );
 
 	return TRUE;
 }
@@ -507,13 +518,13 @@ BOOL LLEyeMotion::onUpdate(F32 time, U8* joint_mask)
 //-----------------------------------------------------------------------------
 void LLEyeMotion::onDeactivate()
 {
-	LLJoint* joint = mLeftEyeState.getJoint();
+	LLJoint* joint = mLeftEyeState->getJoint();
 	if (joint)
 	{
 		joint->setRotation(LLQuaternion::DEFAULT);
 	}
 
-	joint = mRightEyeState.getJoint();
+	joint = mRightEyeState->getJoint();
 	if (joint)
 	{
 		joint->setRotation(LLQuaternion::DEFAULT);
diff --git a/indra/llcharacter/llheadrotmotion.h b/indra/llcharacter/llheadrotmotion.h
index efb9c3e0be996db1455f2fa938e66d9cf5f4108d..57c4db5dadd70829cc3303c0652abe113b34af11 100644
--- a/indra/llcharacter/llheadrotmotion.h
+++ b/indra/llcharacter/llheadrotmotion.h
@@ -117,9 +117,9 @@ class LLHeadRotMotion :
 	LLJoint				*mRootJoint;
 	LLJoint				*mPelvisJoint;
 
-	LLJointState		mTorsoState;
-	LLJointState		mNeckState;
-	LLJointState		mHeadState;
+	LLPointer<LLJointState> mTorsoState;
+	LLPointer<LLJointState> mNeckState;
+	LLPointer<LLJointState> mHeadState;
 
 	LLQuaternion		mLastHeadRot;
 };
@@ -196,8 +196,8 @@ class LLEyeMotion :
 	LLCharacter			*mCharacter;
 
 	LLJoint				*mHeadJoint;
-	LLJointState		mLeftEyeState;
-	LLJointState		mRightEyeState;
+	LLPointer<LLJointState> mLeftEyeState;
+	LLPointer<LLJointState> mRightEyeState;
 
 	LLFrameTimer		mEyeJitterTimer;
 	F32					mEyeJitterTime;
diff --git a/indra/llcharacter/lljoint.h b/indra/llcharacter/lljoint.h
index 167598fc70e0101f087704c15151628e2b902b64..8ca8118e4c827b1cc9ceb3de0d73b8a0d98f6456 100644
--- a/indra/llcharacter/lljoint.h
+++ b/indra/llcharacter/lljoint.h
@@ -115,7 +115,7 @@ class LLJoint
 	void touch(U32 flags = ALL_DIRTY);
 
 	// get/set name
-	const std::string &getName() { return mName; }
+	const std::string& getName() const { return mName; }
 	void setName( const std::string &name ) { mName = name; }
 
 	// getParent
@@ -175,7 +175,7 @@ class LLJoint
 
 	virtual BOOL isAnimatable() { return TRUE; }
 
-	S32 getJointNum() { return mJointNum; }
+	S32 getJointNum() const { return mJointNum; }
 	void setJointNum(S32 joint_num) { mJointNum = joint_num; }
 };
 #endif // LL_LLJOINT_H
diff --git a/indra/llcharacter/lljointstate.h b/indra/llcharacter/lljointstate.h
index 202232243b1be09799f8ffa438bf3e59ac6fb73c..a1723b6dd989e2a7c20620c50b0a69407e5d1820 100644
--- a/indra/llcharacter/lljointstate.h
+++ b/indra/llcharacter/lljointstate.h
@@ -36,11 +36,12 @@
 // Header Files
 //-----------------------------------------------------------------------------
 #include "lljoint.h"
+#include "llmemory.h"
 
 //-----------------------------------------------------------------------------
 // class LLJointState
 //-----------------------------------------------------------------------------
-class LLJointState
+class LLJointState : public LLRefCount
 {
 public:
 	enum BlendPhase
@@ -85,13 +86,9 @@ class LLJointState
 		mPriority = LLJoint::USE_MOTION_PRIORITY;
 	}
 
-	// Destructor
-	virtual ~LLJointState()
-	{
-	}
-
 	// joint that this state is applied to
-	LLJoint *getJoint()				{ return mJoint; }
+	LLJoint* getJoint()				{ return mJoint; }
+	const LLJoint* getJoint() const	{ return mJoint; }
 	BOOL setJoint( LLJoint *joint )	{ mJoint = joint; return mJoint != NULL; }
 
 	// transform type (bitwise flags can be combined)
@@ -103,26 +100,33 @@ class LLJointState
 		ROT		= 2,
 		SCALE	= 4,
 	};
-	U32 getUsage()			{ return mUsage; }
-	void setUsage( U32 usage )	{ mUsage = usage; }
-	F32 getWeight()			{ return mWeight; }
+	U32 getUsage() const			{ return mUsage; }
+	void setUsage( U32 usage )		{ mUsage = usage; }
+	F32 getWeight() const			{ return mWeight; }
 	void setWeight( F32 weight )	{ mWeight = weight; }
 
 	// get/set position
-	const LLVector3& getPosition()				{ return mPosition; }
+	const LLVector3& getPosition() const		{ return mPosition; }
 	void setPosition( const LLVector3& pos )	{ llassert(mUsage & POS); mPosition = pos; }
 
 	// get/set rotation
-	const LLQuaternion& getRotation()			{ return mRotation; }
+	const LLQuaternion& getRotation() const		{ return mRotation; }
 	void setRotation( const LLQuaternion& rot )	{ llassert(mUsage & ROT); mRotation = rot; }
 
 	// get/set scale
-	const LLVector3& getScale()				{ return mScale; }
-	void setScale( const LLVector3& scale )	{ llassert(mUsage & SCALE); mScale = scale; }
+	const LLVector3& getScale() const			{ return mScale; }
+	void setScale( const LLVector3& scale )		{ llassert(mUsage & SCALE); mScale = scale; }
 
 	// get/set priority
-	LLJoint::JointPriority getPriority()		{ return mPriority; }
-	void setPriority( const LLJoint::JointPriority priority ) { mPriority = priority; }
+	LLJoint::JointPriority getPriority() const	{ return mPriority; }
+	void setPriority( LLJoint::JointPriority priority ) { mPriority = priority; }
+
+private:
+	// Destructor
+	virtual ~LLJointState()
+	{
+	}
+	
 };
 
 #endif // LL_LLJOINTSTATE_H
diff --git a/indra/llcharacter/llkeyframefallmotion.cpp b/indra/llcharacter/llkeyframefallmotion.cpp
index bcef7a3fdaea6065430f9009c66f891016cd02da..13308f4e4ae6c7c33ffe8ba39bebf1e07dca6144 100644
--- a/indra/llcharacter/llkeyframefallmotion.cpp
+++ b/indra/llcharacter/llkeyframefallmotion.cpp
@@ -75,13 +75,13 @@ LLMotion::LLMotionInitStatus LLKeyframeFallMotion::onInitialize(LLCharacter *cha
 	// load keyframe data, setup pose and joint states
 	LLMotion::LLMotionInitStatus result = LLKeyframeMotion::onInitialize(character);
 
-	for (U32 jm=0; jm<mJointMotionList->mNumJointMotions; jm++)
+	for (U32 jm=0; jm<mJointMotionList->getNumJointMotions(); jm++)
 	{
-		if (!mJointStates[jm].getJoint())
+		if (!mJointStates[jm]->getJoint())
 			continue;
-		if (mJointStates[jm].getJoint()->getName() == std::string("mPelvis"))
+		if (mJointStates[jm]->getJoint()->getName() == std::string("mPelvis"))
 		{
-			mPelvisStatep = &mJointStates[jm];
+			mPelvisState = mJointStates[jm];
 		}
 	}
 
@@ -124,8 +124,11 @@ BOOL LLKeyframeFallMotion::onUpdate(F32 activeTime, U8* joint_mask)
 	BOOL result = LLKeyframeMotion::onUpdate(activeTime, joint_mask);
 	F32  slerp_amt = clamp_rescale(activeTime / getDuration(), 0.5f, 0.75f, 0.f, 1.f);
 
-	mPelvisStatep->setRotation(mPelvisStatep->getRotation() * slerp(slerp_amt, mRotationToGroundNormal, LLQuaternion()));
-
+	if (mPelvisState.notNull())
+	{
+		mPelvisState->setRotation(mPelvisState->getRotation() * slerp(slerp_amt, mRotationToGroundNormal, LLQuaternion()));
+	}
+	
 	return result;
 }
 
diff --git a/indra/llcharacter/llkeyframefallmotion.h b/indra/llcharacter/llkeyframefallmotion.h
index 4307c511302c3c5247f189fb0cf00ab968248e17..618a1693242d0f9fe73186c454041b97feba98b0 100644
--- a/indra/llcharacter/llkeyframefallmotion.h
+++ b/indra/llcharacter/llkeyframefallmotion.h
@@ -75,7 +75,7 @@ class LLKeyframeFallMotion :
 	//-------------------------------------------------------------------------
 	LLCharacter*	mCharacter;
 	F32				mVelocityZ;
-	LLJointState*	mPelvisStatep;
+	LLPointer<LLJointState>	mPelvisState;
 	LLQuaternion	mRotationToGroundNormal;
 };
 
diff --git a/indra/llcharacter/llkeyframemotion.cpp b/indra/llcharacter/llkeyframemotion.cpp
index 6efe243a8fe43df54cee2e8c83d2567323b19c83..44a2425b1b91bf1e77c2407b5430d892816ca2b0 100644
--- a/indra/llcharacter/llkeyframemotion.cpp
+++ b/indra/llcharacter/llkeyframemotion.cpp
@@ -71,24 +71,22 @@ static F32 MAX_CONSTRAINTS = 10;
 // JointMotionList
 //-----------------------------------------------------------------------------
 LLKeyframeMotion::JointMotionList::JointMotionList()
-	: mNumJointMotions(0),
-	  mJointMotionArray(NULL)
 {
 }
 
 LLKeyframeMotion::JointMotionList::~JointMotionList()
 {
 	for_each(mConstraints.begin(), mConstraints.end(), DeletePointer());
-	delete [] mJointMotionArray;
+	for_each(mJointMotionArray.begin(), mJointMotionArray.end(), DeletePointer());
 }
 
 U32 LLKeyframeMotion::JointMotionList::dumpDiagInfo()
 {
 	S32	total_size = sizeof(JointMotionList);
 
-	for (U32 i = 0; i < mNumJointMotions; i++)
+	for (U32 i = 0; i < getNumJointMotions(); i++)
 	{
-		LLKeyframeMotion::JointMotion* joint_motion_p = &mJointMotionArray[i];
+		LLKeyframeMotion::JointMotion* joint_motion_p = mJointMotionArray[i];
 
 		llinfos << "\tJoint " << joint_motion_p->mJointName << llendl;
 		if (joint_motion_p->mUsage & LLJointState::SCALE)
@@ -385,10 +383,10 @@ void LLKeyframeMotion::JointMotion::update(LLJointState* joint_state, F32 time,
 {
 	// this value being 0 is the cause of https://jira.lindenlab.com/browse/SL-22678 but I haven't 
 	// managed to get a stack to see how it got here. Testing for 0 here will stop the crash.
-	if ( joint_state == 0 )
+	if ( joint_state == NULL )
 	{
 		return;
-	};
+	}
 
 	U32 usage = joint_state->getUsage();
 
@@ -431,7 +429,6 @@ void LLKeyframeMotion::JointMotion::update(LLJointState* joint_state, F32 time,
 LLKeyframeMotion::LLKeyframeMotion(const LLUUID &id) 
 	: LLMotion(id),
 		mJointMotionList(NULL),
-		mJointStates(NULL),
 		mPelvisp(NULL),
 		mLastSkeletonSerialNum(0),
 		mLastUpdateTime(0.f),
@@ -448,10 +445,6 @@ LLKeyframeMotion::LLKeyframeMotion(const LLUUID &id)
 //-----------------------------------------------------------------------------
 LLKeyframeMotion::~LLKeyframeMotion()
 {
-	if (mJointStates)
-	{
-		delete [] mJointStates;
-	}
 	for_each(mConstraints.begin(), mConstraints.end(), DeletePointer());
 }
 
@@ -463,6 +456,26 @@ LLMotion *LLKeyframeMotion::create(const LLUUID &id)
 	return new LLKeyframeMotion(id);
 }
 
+//-----------------------------------------------------------------------------
+// getJointState()
+//-----------------------------------------------------------------------------
+LLPointer<LLJointState>& LLKeyframeMotion::getJointState(U32 index)
+{
+	llassert_always (index < (S32)mJointStates.size());
+	return mJointStates[index];
+}
+
+//-----------------------------------------------------------------------------
+// getJoin()
+//-----------------------------------------------------------------------------
+LLJoint* LLKeyframeMotion::getJoint(U32 index)
+{
+	llassert_always (index < (S32)mJointStates.size());
+	LLJoint* joint = mJointStates[index]->getJoint();
+	llassert_always (joint);
+	return joint;
+}
+
 //-----------------------------------------------------------------------------
 // LLKeyframeMotion::onInitialize(LLCharacter *character)
 //-----------------------------------------------------------------------------
@@ -506,17 +519,20 @@ LLMotion::LLMotionInitStatus LLKeyframeMotion::onInitialize(LLCharacter *charact
 		// motion already existed in cache, so grab it
 		mJointMotionList = joint_motion_list;
 
+		mJointStates.reserve(mJointMotionList->getNumJointMotions());
+		
 		// don't forget to allocate joint states
-		mJointStates = new LLJointState[mJointMotionList->mNumJointMotions];
-
 		// set up joint states to point to character joints
-		for(U32 i = 0; i < mJointMotionList->mNumJointMotions; i++)
+		for(U32 i = 0; i < mJointMotionList->getNumJointMotions(); i++)
 		{
-			if (LLJoint *jointp = mCharacter->getJoint(mJointMotionList->mJointMotionArray[i].mJointName))
+			JointMotion* joint_motion = mJointMotionList->getJointMotion(i);
+			if (LLJoint *joint = mCharacter->getJoint(joint_motion->mJointName))
 			{
-				mJointStates[i].setJoint(jointp);
-				mJointStates[i].setUsage(mJointMotionList->mJointMotionArray[i].mUsage);
-				mJointStates[i].setPriority(joint_motion_list->mJointMotionArray[i].mPriority);
+				LLPointer<LLJointState> joint_state = new LLJointState;
+				mJointStates.push_back(joint_state);
+				joint_state->setJoint(joint);
+				joint_state->setUsage(joint_motion->mUsage);
+				joint_state->setPriority(joint_motion->mPriority);
 			}
 		}
 		mAssetStatus = ASSET_LOADED;
@@ -587,11 +603,12 @@ LLMotion::LLMotionInitStatus LLKeyframeMotion::onInitialize(LLCharacter *charact
 BOOL LLKeyframeMotion::setupPose()
 {
 	// add all valid joint states to the pose
-	for (U32 jm=0; jm<mJointMotionList->mNumJointMotions; jm++)
+	for (U32 jm=0; jm<mJointMotionList->getNumJointMotions(); jm++)
 	{
-		if ( mJointStates[jm].getJoint() )
+		LLPointer<LLJointState> joint_state = getJointState(jm);
+		if ( joint_state->getJoint() )
 		{
-			addJointState( &mJointStates[jm] );
+			addJointState( joint_state );
 		}
 	}
 
@@ -692,13 +709,12 @@ BOOL LLKeyframeMotion::onUpdate(F32 time, U8* joint_mask)
 //-----------------------------------------------------------------------------
 void LLKeyframeMotion::applyKeyframes(F32 time)
 {
-	U32 i;
-	for (i=0; i<mJointMotionList->mNumJointMotions; i++)
+	llassert_always (mJointMotionList->getNumJointMotions() <= mJointStates.size());
+	for (U32 i=0; i<mJointMotionList->getNumJointMotions(); i++)
 	{
-		mJointMotionList->mJointMotionArray[i].update(
-			&mJointStates[i], 
-			time, 
-			mJointMotionList->mDuration );
+		mJointMotionList->getJointMotion(i)->update(mJointStates[i],
+													  time, 
+													  mJointMotionList->mDuration );
 	}
 
 	LLJoint::JointPriority* pose_priority = (LLJoint::JointPriority* )mCharacter->getAnimationData("Hand Pose Priority");
@@ -793,7 +809,7 @@ void LLKeyframeMotion::initializeConstraint(JointConstraint* constraint)
 
 	S32 joint_num;
 	LLVector3 source_pos = mCharacter->getVolumePos(shared_data->mSourceConstraintVolume, shared_data->mSourceConstraintOffset);
-	LLJoint* cur_joint = mJointStates[shared_data->mJointStateIndices[0]].getJoint();
+	LLJoint* cur_joint = getJoint(shared_data->mJointStateIndices[0]);
 
 	F32 source_pos_offset = dist_vec(source_pos, cur_joint->getWorldPosition());
 
@@ -802,7 +818,7 @@ void LLKeyframeMotion::initializeConstraint(JointConstraint* constraint)
 	// grab joint lengths
 	for (joint_num = 1; joint_num < shared_data->mChainLength; joint_num++)
 	{
-		cur_joint = mJointStates[shared_data->mJointStateIndices[joint_num]].getJoint();
+		cur_joint = getJointState(shared_data->mJointStateIndices[joint_num])->getJoint();
 		if (!cur_joint)
 		{
 			return;
@@ -844,7 +860,7 @@ void LLKeyframeMotion::activateConstraint(JointConstraint* constraint)
 
 	for (joint_num = 1; joint_num < shared_data->mChainLength; joint_num++)
 	{
-		LLJoint* cur_joint = mJointStates[shared_data->mJointStateIndices[joint_num]].getJoint();
+		LLJoint* cur_joint = getJoint(shared_data->mJointStateIndices[joint_num]);
 		constraint->mPositions[joint_num] = (cur_joint->getWorldPosition() - mPelvisp->getWorldPosition()) * ~mPelvisp->getWorldRotation();
 	}
 
@@ -884,7 +900,6 @@ void LLKeyframeMotion::applyConstraint(JointConstraint* constraint, F32 time, U8
 	LLVector3		velocities[MAX_CHAIN_LENGTH - 1];
 	LLQuaternion	old_rots[MAX_CHAIN_LENGTH];
 	S32				joint_num;
-	LLJoint*		cur_joint;
 
 	if (time < shared_data->mEaseInStartTime)
 	{
@@ -905,7 +920,7 @@ void LLKeyframeMotion::applyConstraint(JointConstraint* constraint, F32 time, U8
 		activateConstraint(constraint);
 	}
 
-	LLJoint* root_joint = mJointStates[shared_data->mJointStateIndices[shared_data->mChainLength]].getJoint();
+	LLJoint* root_joint = getJoint(shared_data->mJointStateIndices[shared_data->mChainLength]);
 	LLVector3 root_pos = root_joint->getWorldPosition();
 //	LLQuaternion root_rot = 
 	root_joint->getParent()->getWorldRotation();
@@ -916,14 +931,14 @@ void LLKeyframeMotion::applyConstraint(JointConstraint* constraint, F32 time, U8
 	//apply underlying keyframe animation to get nominal "kinematic" joint positions
 	for (joint_num = 0; joint_num <= shared_data->mChainLength; joint_num++)
 	{
-		cur_joint = mJointStates[shared_data->mJointStateIndices[joint_num]].getJoint();
+		LLJoint* cur_joint = getJoint(shared_data->mJointStateIndices[joint_num]);
 		if (joint_mask[cur_joint->getJointNum()] >= (0xff >> (7 - getPriority())))
 		{
 			// skip constraint
 			return;
 		}
 		old_rots[joint_num] = cur_joint->getRotation();
-		cur_joint->setRotation(mJointStates[shared_data->mJointStateIndices[joint_num]].getRotation());
+		cur_joint->setRotation(getJointState(shared_data->mJointStateIndices[joint_num])->getRotation());
 	}
 
 
@@ -1007,7 +1022,7 @@ void LLKeyframeMotion::applyConstraint(JointConstraint* constraint, F32 time, U8
 
 	if (shared_data->mChainLength)
 	{
-		LLQuaternion end_rot = mJointStates[shared_data->mJointStateIndices[0]].getJoint()->getWorldRotation();
+		LLQuaternion end_rot = getJoint(shared_data->mJointStateIndices[0])->getWorldRotation();
 
 		// slam start and end of chain to the proper positions (rest of chain stays put)
 		positions[0] = lerp(keyframe_source_pos, target_pos, weight);
@@ -1016,7 +1031,7 @@ void LLKeyframeMotion::applyConstraint(JointConstraint* constraint, F32 time, U8
 		// grab keyframe-specified positions of joints	
 		for (joint_num = 1; joint_num < shared_data->mChainLength; joint_num++)
 		{
-			LLVector3 kinematic_position = mJointStates[shared_data->mJointStateIndices[joint_num]].getJoint()->getWorldPosition() + 
+			LLVector3 kinematic_position = getJoint(shared_data->mJointStateIndices[joint_num])->getWorldPosition() + 
 				(source_to_target * constraint->mJointLengthFractions[joint_num]);
 
 			// convert intermediate joint positions to world coordinates
@@ -1061,9 +1076,9 @@ void LLKeyframeMotion::applyConstraint(JointConstraint* constraint, F32 time, U8
 
 		for (joint_num = shared_data->mChainLength; joint_num > 0; joint_num--)
 		{
-			LLQuaternion parent_rot = mJointStates[shared_data->mJointStateIndices[joint_num]].getJoint()->getParent()->getWorldRotation();
-			cur_joint = mJointStates[shared_data->mJointStateIndices[joint_num]].getJoint();
-			LLJoint* child_joint = mJointStates[shared_data->mJointStateIndices[joint_num - 1]].getJoint();
+			LLJoint* cur_joint = getJoint(shared_data->mJointStateIndices[joint_num]);
+			LLJoint* child_joint = getJoint(shared_data->mJointStateIndices[joint_num - 1]);
+			LLQuaternion parent_rot = cur_joint->getParent()->getWorldRotation();
 
 			LLQuaternion cur_rot = cur_joint->getWorldRotation();
 			LLQuaternion fixup_rot;
@@ -1088,25 +1103,25 @@ void LLKeyframeMotion::applyConstraint(JointConstraint* constraint, F32 time, U8
 
 			if (weight != 1.f)
 			{
-				LLQuaternion cur_rot = mJointStates[shared_data->mJointStateIndices[joint_num]].getRotation();
+				LLQuaternion cur_rot = getJointState(shared_data->mJointStateIndices[joint_num])->getRotation();
 				target_rot = nlerp(weight, cur_rot, target_rot);
 			}
 
-			mJointStates[shared_data->mJointStateIndices[joint_num]].setRotation(target_rot);
+			getJointState(shared_data->mJointStateIndices[joint_num])->setRotation(target_rot);
 			cur_joint->setRotation(target_rot);
 		}
 
-		LLJoint* end_joint = mJointStates[shared_data->mJointStateIndices[0]].getJoint();
+		LLJoint* end_joint = getJoint(shared_data->mJointStateIndices[0]);
 		LLQuaternion end_local_rot = end_rot * ~end_joint->getParent()->getWorldRotation();
 
 		if (weight == 1.f)
 		{
-			mJointStates[shared_data->mJointStateIndices[0]].setRotation(end_local_rot);
+			getJointState(shared_data->mJointStateIndices[0])->setRotation(end_local_rot);
 		}
 		else
 		{
-			LLQuaternion cur_rot = mJointStates[shared_data->mJointStateIndices[0]].getRotation();
-			mJointStates[shared_data->mJointStateIndices[0]].setRotation(nlerp(weight, cur_rot, end_local_rot));
+			LLQuaternion cur_rot = getJointState(shared_data->mJointStateIndices[0])->getRotation();
+			getJointState(shared_data->mJointStateIndices[0])->setRotation(nlerp(weight, cur_rot, end_local_rot));
 		}
 
 		// save simulated positions in pelvis-space and calculate total fixup distance
@@ -1124,17 +1139,17 @@ void LLKeyframeMotion::applyConstraint(JointConstraint* constraint, F32 time, U8
 		//reset old joint rots
 		for (joint_num = 0; joint_num <= shared_data->mChainLength; joint_num++)
 		{
-			mJointStates[shared_data->mJointStateIndices[joint_num]].getJoint()->setRotation(old_rots[joint_num]);
+			getJoint(shared_data->mJointStateIndices[joint_num])->setRotation(old_rots[joint_num]);
 		}
 	}
 	// simple positional constraint (pelvis only)
-	else if (mJointStates[shared_data->mJointStateIndices[0]].getUsage() & LLJointState::POS)
+	else if (getJointState(shared_data->mJointStateIndices[0])->getUsage() & LLJointState::POS)
 	{
 		LLVector3 delta = source_to_target * weight;
-		LLJointState* current_joint_statep = &mJointStates[shared_data->mJointStateIndices[0]];
-		LLQuaternion parent_rot = current_joint_statep->getJoint()->getParent()->getWorldRotation();
+		LLPointer<LLJointState> current_joint_state = getJointState(shared_data->mJointStateIndices[0]);
+		LLQuaternion parent_rot = current_joint_state->getJoint()->getParent()->getWorldRotation();
 		delta = delta * ~parent_rot;
-		current_joint_statep->setPosition(current_joint_statep->getJoint()->getPosition() + delta);
+		current_joint_state->setPosition(current_joint_state->getJoint()->getPosition() + delta);
 	}
 }
 
@@ -1145,7 +1160,6 @@ BOOL LLKeyframeMotion::deserialize(LLDataPacker& dp)
 {
 	BOOL old_version = FALSE;
 	mJointMotionList = new LLKeyframeMotion::JointMotionList;
-	mJointMotionList->mNumJointMotions = 0;
 
 	//-------------------------------------------------------------------------
 	// get base priority
@@ -1261,40 +1275,38 @@ BOOL LLKeyframeMotion::deserialize(LLDataPacker& dp)
 	//-------------------------------------------------------------------------
 	// get number of joint motions
 	//-------------------------------------------------------------------------
-	if (!dp.unpackU32(mJointMotionList->mNumJointMotions, "num_joints"))
+	U32 num_motions = 0;
+	if (!dp.unpackU32(num_motions, "num_joints"))
 	{
 		llwarns << "can't read number of joints" << llendl;
 		return FALSE;
 	}
 
-	if (mJointMotionList->mNumJointMotions == 0)
+	if (num_motions == 0)
 	{
 		llwarns << "no joints in animation" << llendl;
 		return FALSE;
 	}
-	else if (mJointMotionList->mNumJointMotions > LL_CHARACTER_MAX_JOINTS)
+	else if (num_motions > LL_CHARACTER_MAX_JOINTS)
 	{
 		llwarns << "too many joints in animation" << llendl;
 		return FALSE;
 	}
 
-	mJointMotionList->mJointMotionArray = new JointMotion[mJointMotionList->mNumJointMotions];
-	mJointStates = new LLJointState[mJointMotionList->mNumJointMotions];
-
-	if (!mJointMotionList->mJointMotionArray)
-	{
-		mJointMotionList->mDuration = 0.0f;
-		mJointMotionList->mEaseInDuration = 0.0f;
-		mJointMotionList->mEaseOutDuration = 0.0f;
-		return FALSE;
-	}
+	mJointMotionList->mJointMotionArray.clear();
+	mJointMotionList->mJointMotionArray.reserve(num_motions);
+	mJointStates.clear();
+	mJointStates.reserve(num_motions);
 
 	//-------------------------------------------------------------------------
 	// initialize joint motions
 	//-------------------------------------------------------------------------
-	S32 k;
-	for(U32 i=0; i<mJointMotionList->mNumJointMotions; ++i)
+
+	for(U32 i=0; i<num_motions; ++i)
 	{
+		JointMotion* joint_motion = new JointMotion;		
+		mJointMotionList->mJointMotionArray.push_back(joint_motion);
+		
 		std::string joint_name;
 		if (!dp.unpackString(joint_name, "joint_name"))
 		{
@@ -1316,9 +1328,12 @@ BOOL LLKeyframeMotion::deserialize(LLDataPacker& dp)
 			//return FALSE;
 		}
 
-		mJointMotionList->mJointMotionArray[i].mJointName = joint_name;
-		mJointStates[i].setJoint( joint );
-		mJointStates[i].setUsage( 0 );
+		joint_motion->mJointName = joint_name;
+		
+		LLPointer<LLJointState> joint_state = new LLJointState;
+		mJointStates.push_back(joint_state);
+		joint_state->setJoint( joint );
+		joint_state->setUsage( 0 );
 
 		//---------------------------------------------------------------------
 		// get joint priority
@@ -1330,36 +1345,36 @@ BOOL LLKeyframeMotion::deserialize(LLDataPacker& dp)
 			return FALSE;
 		}
 		
-		mJointMotionList->mJointMotionArray[i].mPriority = (LLJoint::JointPriority)joint_priority;
+		joint_motion->mPriority = (LLJoint::JointPriority)joint_priority;
 		if (joint_priority != LLJoint::USE_MOTION_PRIORITY &&
 			joint_priority > mJointMotionList->mMaxPriority)
 		{
 			mJointMotionList->mMaxPriority = (LLJoint::JointPriority)joint_priority;
 		}
 
-		mJointStates[i].setPriority((LLJoint::JointPriority)joint_priority);
+		joint_state->setPriority((LLJoint::JointPriority)joint_priority);
 
 		//---------------------------------------------------------------------
 		// scan rotation curve header
 		//---------------------------------------------------------------------
-		if (!dp.unpackS32(mJointMotionList->mJointMotionArray[i].mRotationCurve.mNumKeys, "num_rot_keys"))
+		if (!dp.unpackS32(joint_motion->mRotationCurve.mNumKeys, "num_rot_keys"))
 		{
 			llwarns << "can't read number of rotation keys" << llendl;
 			return FALSE;
 		}
 
-		mJointMotionList->mJointMotionArray[i].mRotationCurve.mInterpolationType = IT_LINEAR;
-		if (mJointMotionList->mJointMotionArray[i].mRotationCurve.mNumKeys != 0)
+		joint_motion->mRotationCurve.mInterpolationType = IT_LINEAR;
+		if (joint_motion->mRotationCurve.mNumKeys != 0)
 		{
-			mJointStates[i].setUsage(mJointStates[i].getUsage() | LLJointState::ROT );
+			joint_state->setUsage(joint_state->getUsage() | LLJointState::ROT );
 		}
 
 		//---------------------------------------------------------------------
 		// scan rotation curve keys
 		//---------------------------------------------------------------------
-		RotationCurve *rCurve = &mJointMotionList->mJointMotionArray[i].mRotationCurve;
+		RotationCurve *rCurve = &joint_motion->mRotationCurve;
 
-		for (k = 0; k < mJointMotionList->mJointMotionArray[i].mRotationCurve.mNumKeys; k++)
+		for (S32 k = 0; k < joint_motion->mRotationCurve.mNumKeys; k++)
 		{
 			F32 time;
 			U16 time_short;
@@ -1424,24 +1439,24 @@ BOOL LLKeyframeMotion::deserialize(LLDataPacker& dp)
 		//---------------------------------------------------------------------
 		// scan position curve header
 		//---------------------------------------------------------------------
-		if (!dp.unpackS32(mJointMotionList->mJointMotionArray[i].mPositionCurve.mNumKeys, "num_pos_keys"))
+		if (!dp.unpackS32(joint_motion->mPositionCurve.mNumKeys, "num_pos_keys"))
 		{
 			llwarns << "can't read number of position keys" << llendl;
 			return FALSE;
 		}
 
-		mJointMotionList->mJointMotionArray[i].mPositionCurve.mInterpolationType = IT_LINEAR;
-		if (mJointMotionList->mJointMotionArray[i].mPositionCurve.mNumKeys != 0)
+		joint_motion->mPositionCurve.mInterpolationType = IT_LINEAR;
+		if (joint_motion->mPositionCurve.mNumKeys != 0)
 		{
-			mJointStates[i].setUsage(mJointStates[i].getUsage() | LLJointState::POS );
+			joint_state->setUsage(joint_state->getUsage() | LLJointState::POS );
 		}
 
 		//---------------------------------------------------------------------
 		// scan position curve keys
 		//---------------------------------------------------------------------
-		PositionCurve *pCurve = &mJointMotionList->mJointMotionArray[i].mPositionCurve;
-		BOOL is_pelvis = mJointMotionList->mJointMotionArray[i].mJointName == "mPelvis";
-		for (k = 0; k < mJointMotionList->mJointMotionArray[i].mPositionCurve.mNumKeys; k++)
+		PositionCurve *pCurve = &joint_motion->mPositionCurve;
+		BOOL is_pelvis = joint_motion->mJointName == "mPelvis";
+		for (S32 k = 0; k < joint_motion->mPositionCurve.mNumKeys; k++)
 		{
 			U16 time_short;
 			PositionKey* pos_key = new PositionKey;
@@ -1501,7 +1516,7 @@ BOOL LLKeyframeMotion::deserialize(LLDataPacker& dp)
 			}
 		}
 
-		mJointMotionList->mJointMotionArray[i].mUsage = mJointStates[i].getUsage();
+		joint_motion->mUsage = joint_state->getUsage();
 	}
 
 	//-------------------------------------------------------------------------
@@ -1655,9 +1670,9 @@ BOOL LLKeyframeMotion::deserialize(LLDataPacker& dp)
 				}
 				joint = parent;
 				constraintp->mJointStateIndices[i] = -1;
-				for (U32 j = 0; j < mJointMotionList->mNumJointMotions; j++)
+				for (U32 j = 0; j < mJointMotionList->getNumJointMotions(); j++)
 				{
-					if(mJointStates[j].getJoint() == joint)
+					if(getJoint(j) == joint)
 					{
 						constraintp->mJointStateIndices[i] = (S32)j;
 						break;
@@ -1695,11 +1710,11 @@ BOOL LLKeyframeMotion::serialize(LLDataPacker& dp) const
 	success &= dp.packF32(mJointMotionList->mEaseInDuration, "ease_in_duration");
 	success &= dp.packF32(mJointMotionList->mEaseOutDuration, "ease_out_duration");
 	success &= dp.packU32(mJointMotionList->mHandPose, "hand_pose");
-	success &= dp.packU32(mJointMotionList->mNumJointMotions, "num_joints");
+	success &= dp.packU32(mJointMotionList->getNumJointMotions(), "num_joints");
 
-	for (U32 i = 0; i < mJointMotionList->mNumJointMotions; i++)
+	for (U32 i = 0; i < mJointMotionList->getNumJointMotions(); i++)
 	{
-		JointMotion* joint_motionp = &mJointMotionList->mJointMotionArray[i];
+		JointMotion* joint_motionp = mJointMotionList->getJointMotion(i);
 		success &= dp.packString(joint_motionp->mJointName.c_str(), "joint_name");
 		success &= dp.packS32(joint_motionp->mPriority, "joint_priority");
 		success &= dp.packS32(joint_motionp->mRotationCurve.mNumKeys, "num_rot_keys");
@@ -1806,13 +1821,14 @@ void LLKeyframeMotion::setPriority(S32 priority)
 		mJointMotionList->mBasePriority = (LLJoint::JointPriority)priority;
 		mJointMotionList->mMaxPriority = mJointMotionList->mBasePriority;
 
-		for (U32 i = 0; i < mJointMotionList->mNumJointMotions; i++)
+		for (U32 i = 0; i < mJointMotionList->getNumJointMotions(); i++)
 		{
-			mJointMotionList->mJointMotionArray[i].mPriority = (LLJoint::JointPriority)llclamp(
-				(S32)mJointMotionList->mJointMotionArray[i].mPriority + priority_delta,
+			JointMotion* joint_motion = mJointMotionList->getJointMotion(i);			
+			joint_motion->mPriority = (LLJoint::JointPriority)llclamp(
+				(S32)joint_motion->mPriority + priority_delta,
 				(S32)LLJoint::LOW_PRIORITY, 
 				(S32)LLJoint::HIGHEST_PRIORITY);
-			mJointStates[i].setPriority(mJointMotionList->mJointMotionArray[i].mPriority);
+			getJointState(i)->setPriority(joint_motion->mPriority);
 		}
 	}
 }
@@ -1888,11 +1904,13 @@ void LLKeyframeMotion::setLoopIn(F32 in_point)
 		mJointMotionList->mLoopInPoint = in_point; 
 		
 		// set up loop keys
-		for (U32 i = 0; i < mJointMotionList->mNumJointMotions; i++)
+		for (U32 i = 0; i < mJointMotionList->getNumJointMotions(); i++)
 		{
-			PositionCurve* pos_curve = &mJointMotionList->mJointMotionArray[i].mPositionCurve;
-			RotationCurve* rot_curve = &mJointMotionList->mJointMotionArray[i].mRotationCurve;
-			ScaleCurve* scale_curve = &mJointMotionList->mJointMotionArray[i].mScaleCurve;
+			JointMotion* joint_motion = mJointMotionList->getJointMotion(i);
+			
+			PositionCurve* pos_curve = &joint_motion->mPositionCurve;
+			RotationCurve* rot_curve = &joint_motion->mRotationCurve;
+			ScaleCurve* scale_curve = &joint_motion->mScaleCurve;
 			
 			pos_curve->mLoopInKey.mTime = mJointMotionList->mLoopInPoint;
 			rot_curve->mLoopInKey.mTime = mJointMotionList->mLoopInPoint;
@@ -1915,11 +1933,13 @@ void LLKeyframeMotion::setLoopOut(F32 out_point)
 		mJointMotionList->mLoopOutPoint = out_point; 
 		
 		// set up loop keys
-		for (U32 i = 0; i < mJointMotionList->mNumJointMotions; i++)
+		for (U32 i = 0; i < mJointMotionList->getNumJointMotions(); i++)
 		{
-			PositionCurve* pos_curve = &mJointMotionList->mJointMotionArray[i].mPositionCurve;
-			RotationCurve* rot_curve = &mJointMotionList->mJointMotionArray[i].mRotationCurve;
-			ScaleCurve* scale_curve = &mJointMotionList->mJointMotionArray[i].mScaleCurve;
+			JointMotion* joint_motion = mJointMotionList->getJointMotion(i);
+			
+			PositionCurve* pos_curve = &joint_motion->mPositionCurve;
+			RotationCurve* rot_curve = &joint_motion->mRotationCurve;
+			ScaleCurve* scale_curve = &joint_motion->mScaleCurve;
 			
 			pos_curve->mLoopOutKey.mTime = mJointMotionList->mLoopOutPoint;
 			rot_curve->mLoopOutKey.mTime = mJointMotionList->mLoopOutPoint;
@@ -2020,10 +2040,10 @@ void LLKeyframeMotion::writeCAL3D(apr_file_t* fp)
 //			</TRACK>
 //	</ANIMATION>
 
-	apr_file_printf(fp, "<ANIMATION VERSION=\"1000\" DURATION=\"%.5f\" NUMTRACKS=\"%d\">\n",  getDuration(), mJointMotionList->mNumJointMotions);
-	for (U32 joint_index = 0; joint_index < mJointMotionList->mNumJointMotions; joint_index++)
+	apr_file_printf(fp, "<ANIMATION VERSION=\"1000\" DURATION=\"%.5f\" NUMTRACKS=\"%d\">\n",  getDuration(), mJointMotionList->getNumJointMotions());
+	for (U32 joint_index = 0; joint_index < mJointMotionList->getNumJointMotions(); joint_index++)
 	{
-		JointMotion* joint_motionp = &mJointMotionList->mJointMotionArray[joint_index];
+		JointMotion* joint_motionp = mJointMotionList->getJointMotion(joint_index);
 		LLJoint* animated_joint = mCharacter->getJoint(joint_motionp->mJointName);
 		S32 joint_num = animated_joint->mJointNum + 1;
 
diff --git a/indra/llcharacter/llkeyframemotion.h b/indra/llcharacter/llkeyframemotion.h
index 57123fadea51470dbb0c4842d929053b2850fb99..9cea7f3a590cbc3e807b8286ec3d877e1484d9d3 100644
--- a/indra/llcharacter/llkeyframemotion.h
+++ b/indra/llcharacter/llkeyframemotion.h
@@ -73,6 +73,11 @@ class LLKeyframeMotion :
 	// Destructor
 	virtual ~LLKeyframeMotion();
 
+private:
+	// private helper functions to wrap some asserts
+	LLPointer<LLJointState>& getJointState(U32 index);
+	LLJoint* getJoint(U32 index);
+	
 public:
 	//-------------------------------------------------------------------------
 	// functions to support MotionController and MotionRegistry
@@ -388,7 +393,7 @@ class LLKeyframeMotion :
 		U32				mUsage;
 		LLJoint::JointPriority	mPriority;
 
-		void update(LLJointState *joint_state, F32 time, F32 duration);
+		void update(LLJointState* joint_state, F32 time, F32 duration);
 	};
 	
 	//-------------------------------------------------------------------------
@@ -397,8 +402,7 @@ class LLKeyframeMotion :
 	class JointMotionList
 	{
 	public:
-		U32						mNumJointMotions;
-		JointMotion*			mJointMotionArray;
+		std::vector<JointMotion*> mJointMotionArray;
 		F32						mDuration;
 		BOOL					mLoop;
 		F32						mLoopInPoint;
@@ -415,6 +419,8 @@ class LLKeyframeMotion :
 		JointMotionList();
 		~JointMotionList();
 		U32 dumpDiagInfo();
+		JointMotion* getJointMotion(U32 index) const { llassert(index < mJointMotionArray.size()); return mJointMotionArray[index]; }
+		U32 getNumJointMotions() const { return mJointMotionArray.size(); }
 	};
 
 
@@ -425,7 +431,7 @@ class LLKeyframeMotion :
 	// Member Data
 	//-------------------------------------------------------------------------
 	JointMotionList*				mJointMotionList;
-	LLJointState*					mJointStates;
+	std::vector<LLPointer<LLJointState> > mJointStates;
 	LLJoint*						mPelvisp;
 	LLCharacter*					mCharacter;
 	std::string						mEmoteName;
diff --git a/indra/llcharacter/llkeyframemotionparam.cpp b/indra/llcharacter/llkeyframemotionparam.cpp
index dd64ef2135515fd4e0a11af0fd27fb42a9602a28..f54d1f4a839d7b96031bc88447e6608df2113b9b 100644
--- a/indra/llcharacter/llkeyframemotionparam.cpp
+++ b/indra/llcharacter/llkeyframemotionparam.cpp
@@ -61,7 +61,6 @@ BOOL LLKeyframeMotionParam::sortFunc(ParameterizedMotion *new_motion, Parameteri
 //-----------------------------------------------------------------------------
 LLKeyframeMotionParam::LLKeyframeMotionParam( const LLUUID &id) : LLMotion(id)
 {
-	mJointStates = NULL;
 	mDefaultKeyframeMotion = NULL;
 	mCharacter = NULL;
 
diff --git a/indra/llcharacter/llkeyframemotionparam.h b/indra/llcharacter/llkeyframemotionparam.h
index 204a475dd36da80d655e502d8e61376b4e949adc..39a02aea251a2abce9a8472fe90063a88ee0b224 100644
--- a/indra/llcharacter/llkeyframemotionparam.h
+++ b/indra/llcharacter/llkeyframemotionparam.h
@@ -145,7 +145,6 @@ class LLKeyframeMotionParam :
 
 	typedef LLLinkedList < ParameterizedMotion >	motion_list_t;
 	LLAssocList <std::string, motion_list_t* > mParameterizedMotions;
-	LLJointState*		mJointStates;
 	LLMotion*			mDefaultKeyframeMotion;
 	LLCharacter*		mCharacter;
 	LLPoseBlender		mPoseBlender;
diff --git a/indra/llcharacter/llkeyframestandmotion.h b/indra/llcharacter/llkeyframestandmotion.h
index 48496af917a380e19b2494be7d1e1c381c9f9714..f98a7e6a844602cb4c450846971bbc49e932826a 100644
--- a/indra/llcharacter/llkeyframestandmotion.h
+++ b/indra/llcharacter/llkeyframestandmotion.h
@@ -78,15 +78,15 @@ class LLKeyframeStandMotion :
 
 	BOOL				mFlipFeet;
 
-	LLJointState		*mPelvisState;
+	LLPointer<LLJointState>	mPelvisState;
 
-	LLJointState		*mHipLeftState;
-	LLJointState		*mKneeLeftState;
-	LLJointState		*mAnkleLeftState;
+	LLPointer<LLJointState>	mHipLeftState;
+	LLPointer<LLJointState>	mKneeLeftState;
+	LLPointer<LLJointState>	mAnkleLeftState;
 
-	LLJointState		*mHipRightState;
-	LLJointState		*mKneeRightState;
-	LLJointState		*mAnkleRightState;
+	LLPointer<LLJointState>	mHipRightState;
+	LLPointer<LLJointState>	mKneeRightState;
+	LLPointer<LLJointState>	mAnkleRightState;
 
 	LLJoint				mPelvisJoint;
 
diff --git a/indra/llcharacter/llkeyframewalkmotion.cpp b/indra/llcharacter/llkeyframewalkmotion.cpp
index 9a49996ebd195b6f7d24f3298f3a6c6a35bf8a59..0d7e34bb4f3c40dca4ad1175dc0797456db4ff6a 100644
--- a/indra/llcharacter/llkeyframewalkmotion.cpp
+++ b/indra/llcharacter/llkeyframewalkmotion.cpp
@@ -143,6 +143,8 @@ LLWalkAdjustMotion::LLWalkAdjustMotion(const LLUUID &id) : LLMotion(id)
 {
 	mLastTime = 0.f;
 	mName = "walk_adjust";
+
+	mPelvisState = new LLJointState;
 }
 
 //-----------------------------------------------------------------------------
@@ -155,15 +157,15 @@ LLMotion::LLMotionInitStatus LLWalkAdjustMotion::onInitialize(LLCharacter *chara
 	mRightAnkleJoint = mCharacter->getJoint("mAnkleRight");
 
 	mPelvisJoint = mCharacter->getJoint("mPelvis");
-	mPelvisState.setJoint( mPelvisJoint );
+	mPelvisState->setJoint( mPelvisJoint );
 	if ( !mPelvisJoint )
 	{
 		llwarns << getName() << ": Can't get pelvis joint." << llendl;
 		return STATUS_FAILURE;
 	}
 
-	mPelvisState.setUsage(LLJointState::POS);
-	addJointState( &mPelvisState );
+	mPelvisState->setUsage(LLJointState::POS);
+	addJointState( mPelvisState );
 
 	return STATUS_SUCCESS;
 }
@@ -178,7 +180,7 @@ BOOL LLWalkAdjustMotion::onActivate()
 	mAnimSpeed = 0.f;
 	mAvgSpeed = 0.f;
 	mRelativeDir = 1.f;
-	mPelvisState.setPosition(LLVector3::zero);
+	mPelvisState->setPosition(LLVector3::zero);
 	// store ankle positions for next frame
 	mLastLeftAnklePos = mCharacter->getPosGlobalFromAgent(mLeftAnkleJoint->getWorldPosition());
 	mLastRightAnklePos = mCharacter->getPosGlobalFromAgent(mRightAnkleJoint->getWorldPosition());
@@ -271,7 +273,7 @@ BOOL LLWalkAdjustMotion::onUpdate(F32 time, U8* joint_mask)
 
 	// calculate ideal pelvis offset so that foot is glued to ground and damp towards it
 	// the amount of foot slippage this frame + the offset applied last frame
-	mPelvisOffset = mPelvisState.getPosition() + lerp(LLVector3::zero, footCorrection, LLCriticalDamp::getInterpolant(0.2f));
+	mPelvisOffset = mPelvisState->getPosition() + lerp(LLVector3::zero, footCorrection, LLCriticalDamp::getInterpolant(0.2f));
 
 	// pelvis drift (along walk direction)
 	mAvgCorrection = lerp(mAvgCorrection, footCorrection.mV[VX] * mRelativeDir, LLCriticalDamp::getInterpolant(0.1f));
@@ -319,7 +321,7 @@ BOOL LLWalkAdjustMotion::onUpdate(F32 time, U8* joint_mask)
 	F32 drift_comp_max = llclamp(speed, 0.f, DRIFT_COMP_MAX_SPEED) / DRIFT_COMP_MAX_SPEED;
 	drift_comp_max *= DRIFT_COMP_MAX_TOTAL;
 
-	LLVector3 currentPelvisPos = mPelvisState.getJoint()->getPosition();
+	LLVector3 currentPelvisPos = mPelvisState->getJoint()->getPosition();
 
 	// NB: this is an ADDITIVE amount that is accumulated every frame, so clamping it alone won't do the trick
 	// must clamp with absolute position of pelvis in mind
@@ -328,7 +330,7 @@ BOOL LLWalkAdjustMotion::onUpdate(F32 time, U8* joint_mask)
 	mPelvisOffset.mV[VZ] = 0.f;
 
 	// set position
-	mPelvisState.setPosition(mPelvisOffset);
+	mPelvisState->setPosition(mPelvisOffset);
 
 	mCharacter->setAnimationData("Pelvis Offset", &mPelvisOffset);
 
@@ -343,6 +345,17 @@ void LLWalkAdjustMotion::onDeactivate()
 	mCharacter->removeAnimationData("Walk Speed");
 }
 
+//-----------------------------------------------------------------------------
+// LLFlyAdjustMotion::LLFlyAdjustMotion()
+//-----------------------------------------------------------------------------
+LLFlyAdjustMotion::LLFlyAdjustMotion(const LLUUID &id)
+	: LLMotion(id)
+{
+	mName = "fly_adjust";
+
+	mPelvisState = new LLJointState;
+}
+
 //-----------------------------------------------------------------------------
 // LLFlyAdjustMotion::onInitialize()
 //-----------------------------------------------------------------------------
@@ -351,15 +364,15 @@ LLMotion::LLMotionInitStatus LLFlyAdjustMotion::onInitialize(LLCharacter *charac
 	mCharacter = character;
 
 	LLJoint* pelvisJoint = mCharacter->getJoint("mPelvis");
-	mPelvisState.setJoint( pelvisJoint );
+	mPelvisState->setJoint( pelvisJoint );
 	if ( !pelvisJoint )
 	{
 		llwarns << getName() << ": Can't get pelvis joint." << llendl;
 		return STATUS_FAILURE;
 	}
 
-	mPelvisState.setUsage(LLJointState::POS | LLJointState::ROT);
-	addJointState( &mPelvisState );
+	mPelvisState->setUsage(LLJointState::POS | LLJointState::ROT);
+	addJointState( mPelvisState );
 
 	return STATUS_SUCCESS;
 }
@@ -369,8 +382,8 @@ LLMotion::LLMotionInitStatus LLFlyAdjustMotion::onInitialize(LLCharacter *charac
 //-----------------------------------------------------------------------------
 BOOL LLFlyAdjustMotion::onActivate()
 {
-	mPelvisState.setPosition(LLVector3::zero);
-	mPelvisState.setRotation(LLQuaternion::DEFAULT);
+	mPelvisState->setPosition(LLVector3::zero);
+	mPelvisState->setRotation(LLQuaternion::DEFAULT);
 	mRoll = 0.f;
 	return TRUE;
 }
@@ -392,11 +405,11 @@ BOOL LLFlyAdjustMotion::onUpdate(F32 time, U8* joint_mask)
 //	llinfos << mRoll << llendl;
 
 	LLQuaternion roll(mRoll, LLVector3(0.f, 0.f, 1.f));
-	mPelvisState.setRotation(roll);
+	mPelvisState->setRotation(roll);
 
 //	F32 lerp_amt = LLCriticalDamp::getInterpolant(0.2f);
 //	
-//	LLVector3 pelvis_correction = mPelvisState.getPosition() - lerp(LLVector3::zero, mPelvisState.getJoint()->getPosition() + mPelvisState.getPosition(), lerp_amt);
-//	mPelvisState.setPosition(pelvis_correction);
+//	LLVector3 pelvis_correction = mPelvisState->getPosition() - lerp(LLVector3::zero, mPelvisState->getJoint()->getPosition() + mPelvisState->getPosition(), lerp_amt);
+//	mPelvisState->setPosition(pelvis_correction);
 	return TRUE;
 }
diff --git a/indra/llcharacter/llkeyframewalkmotion.h b/indra/llcharacter/llkeyframewalkmotion.h
index f52a8d6e335e8976be4415c325397937e1b73459..bd30b5d11d9a2e7b2451414ed37ed97fd234f5b7 100644
--- a/indra/llcharacter/llkeyframewalkmotion.h
+++ b/indra/llcharacter/llkeyframewalkmotion.h
@@ -123,7 +123,7 @@ class LLWalkAdjustMotion : public LLMotion
 	LLCharacter		*mCharacter;
 	LLJoint*		mLeftAnkleJoint;
 	LLJoint*		mRightAnkleJoint;
-	LLJointState	mPelvisState;
+	LLPointer<LLJointState>	mPelvisState;
 	LLJoint*		mPelvisJoint;
 	LLVector3d		mLastLeftAnklePos;
 	LLVector3d		mLastRightAnklePos;
@@ -141,7 +141,7 @@ class LLFlyAdjustMotion : public LLMotion
 {
 public:
 	// Constructor
-	LLFlyAdjustMotion(const LLUUID &id) : LLMotion(id) {mName = "fly_adjust";}
+	LLFlyAdjustMotion(const LLUUID &id);
 
 public:
 	//-------------------------------------------------------------------------
@@ -173,7 +173,7 @@ class LLFlyAdjustMotion : public LLMotion
 	// Member Data
 	//-------------------------------------------------------------------------
 	LLCharacter		*mCharacter;
-	LLJointState	mPelvisState;
+	LLPointer<LLJointState>	mPelvisState;
 	F32				mRoll;
 };
 
diff --git a/indra/llcharacter/llmotion.cpp b/indra/llcharacter/llmotion.cpp
index e674620d42cd6e4b6b3cd45bfaec1679b3091863..bf332ed83822c8a3880041062b1eb69e51c3b5a0 100644
--- a/indra/llcharacter/llmotion.cpp
+++ b/indra/llcharacter/llmotion.cpp
@@ -106,7 +106,7 @@ void LLMotion::fadeIn()
 //-----------------------------------------------------------------------------
 // addJointState()
 //-----------------------------------------------------------------------------
-void LLMotion::addJointState(LLJointState* jointState)
+void LLMotion::addJointState(const LLPointer<LLJointState>& jointState)
 {
 	mPose.addJointState(jointState);
 	S32 priority = jointState->getPriority();
diff --git a/indra/llcharacter/llmotion.h b/indra/llcharacter/llmotion.h
index 2b5c5aad5ebde175838ed7b9971f110ee45177e5..7669920339bc3e1407ce917de15395f402e169e6 100644
--- a/indra/llcharacter/llmotion.h
+++ b/indra/llcharacter/llmotion.h
@@ -163,7 +163,7 @@ class LLMotion
 	// it will be deactivated
 	virtual BOOL onActivate() = 0;
 
-	void addJointState(LLJointState* jointState);
+	void addJointState(const LLPointer<LLJointState>& jointState);
 
 protected:
 	LLPose		mPose;
diff --git a/indra/llcharacter/llpose.cpp b/indra/llcharacter/llpose.cpp
index 1e637d7e33afb4613e3762f9ed8073d42b101670..557bbd2d56c5091e1ec78e3d88dc7cc7bb612ef1 100644
--- a/indra/llcharacter/llpose.cpp
+++ b/indra/llcharacter/llpose.cpp
@@ -54,7 +54,7 @@ LLPose::~LLPose()
 //-----------------------------------------------------------------------------
 // getFirstJointState()
 //-----------------------------------------------------------------------------
-LLJointState *LLPose::getFirstJointState()
+LLJointState* LLPose::getFirstJointState()
 {
 	mListIter = mJointMap.begin();
 	if (mListIter == mJointMap.end())
@@ -86,7 +86,7 @@ LLJointState *LLPose::getNextJointState()
 //-----------------------------------------------------------------------------
 // addJointState()
 //-----------------------------------------------------------------------------
-BOOL LLPose::addJointState(LLJointState *jointState)
+BOOL LLPose::addJointState(const LLPointer<LLJointState>& jointState)
 {
 	if (mJointMap.find(jointState->getJoint()->getName()) == mJointMap.end())
 	{
@@ -98,7 +98,7 @@ BOOL LLPose::addJointState(LLJointState *jointState)
 //-----------------------------------------------------------------------------
 // removeJointState()
 //-----------------------------------------------------------------------------
-BOOL LLPose::removeJointState(LLJointState *jointState)
+BOOL LLPose::removeJointState(const LLPointer<LLJointState>& jointState)
 {
 	mJointMap.erase(jointState->getJoint()->getName());
 	return TRUE;
@@ -199,7 +199,7 @@ LLJointStateBlender::~LLJointStateBlender()
 //-----------------------------------------------------------------------------
 // addJointState()
 //-----------------------------------------------------------------------------
-BOOL LLJointStateBlender::addJointState(LLJointState *joint_state, S32 priority, BOOL additive_blend)
+BOOL LLJointStateBlender::addJointState(const LLPointer<LLJointState>& joint_state, S32 priority, BOOL additive_blend)
 {
 	llassert(joint_state);
 
@@ -209,7 +209,7 @@ BOOL LLJointStateBlender::addJointState(LLJointState *joint_state, S32 priority,
 
 	for(S32 i = 0; i < JSB_NUM_JOINT_STATES; i++)
 	{
-		if (NULL == mJointStates[i])
+		if (mJointStates[i].isNull())
 		{
 			mJointStates[i] = joint_state;
 			mPriorities[i] = priority;
@@ -246,7 +246,7 @@ void LLJointStateBlender::blendJointStates(BOOL apply_now)
 	// we need at least one joint to blend
 	// if there is one, it will be in slot zero according to insertion logic
 	// instead of resetting joint state to default, just leave it unchanged from last frame
-	if (NULL == mJointStates[0])
+	if (mJointStates[0].isNull())
 	{
 		return;
 	}
@@ -275,7 +275,7 @@ void LLJointStateBlender::blendJointStates(BOOL apply_now)
 	sum_weights[SCALE_WEIGHT] = 0.f;
 
 	for(S32 joint_state_index = 0; 
-		joint_state_index < JSB_NUM_JOINT_STATES && mJointStates[joint_state_index] != NULL;
+		joint_state_index < JSB_NUM_JOINT_STATES && mJointStates[joint_state_index].notNull();
 		joint_state_index++)
 	{
 		LLJointState* jsp = mJointStates[joint_state_index];
@@ -468,7 +468,7 @@ BOOL LLPoseBlender::addMotion(LLMotion* motion)
 {
 	LLPose* pose = motion->getPose();
 
-	for(LLJointState *jsp = pose->getFirstJointState(); jsp; jsp = pose->getNextJointState())
+	for(LLJointState* jsp = pose->getFirstJointState(); jsp; jsp = pose->getNextJointState())
 	{
 		LLJoint *jointp = jsp->getJoint();
 		LLJointStateBlender* joint_blender;
diff --git a/indra/llcharacter/llpose.h b/indra/llcharacter/llpose.h
index fe0a23887de156fe9b14cc62c1d3e374ecc95bec..5ba579a167c2851418f6c14205cceca191e51d97 100644
--- a/indra/llcharacter/llpose.h
+++ b/indra/llcharacter/llpose.h
@@ -52,7 +52,7 @@ class LLPose
 {
 	friend class LLPoseBlender;
 protected:
-	typedef std::map<std::string, LLJointState*> joint_map;
+	typedef std::map<std::string, LLPointer<LLJointState> > joint_map;
 	typedef joint_map::iterator joint_map_iterator;
 	typedef joint_map::value_type joint_map_value_type;
 	
@@ -61,19 +61,19 @@ class LLPose
 	joint_map_iterator			mListIter;
 public:
 	// Iterate through jointStates
-	LLJointState *getFirstJointState();
-	LLJointState *getNextJointState();
-	LLJointState *findJointState(LLJoint *joint);
-	LLJointState *findJointState(const std::string &name);
+	LLJointState* getFirstJointState();
+	LLJointState* getNextJointState();
+	LLJointState* findJointState(LLJoint *joint);
+	LLJointState* findJointState(const std::string &name);
 public:
 	// Constructor
 	LLPose() : mWeight(0.f) {}
 	// Destructor
 	~LLPose();
 	// add a joint state in this pose
-	BOOL addJointState(LLJointState *jointState);
+	BOOL addJointState(const LLPointer<LLJointState>& jointState);
 	// remove a joint state from this pose
-	BOOL removeJointState(LLJointState *jointState);
+	BOOL removeJointState(const LLPointer<LLJointState>& jointState);
 	// removes all joint states from this pose
 	BOOL removeAllJointStates();
 	// set weight for all joint states in this pose
@@ -89,14 +89,14 @@ const S32 JSB_NUM_JOINT_STATES = 6;
 class LLJointStateBlender
 {
 protected:
-	LLJointState*	mJointStates[JSB_NUM_JOINT_STATES];
+	LLPointer<LLJointState>	mJointStates[JSB_NUM_JOINT_STATES];
 	S32				mPriorities[JSB_NUM_JOINT_STATES];
 	BOOL			mAdditiveBlends[JSB_NUM_JOINT_STATES];
 public:
 	LLJointStateBlender();
 	~LLJointStateBlender();
 	void blendJointStates(BOOL apply_now = TRUE);
-	BOOL addJointState(LLJointState *joint_state, S32 priority, BOOL additive_blend);
+	BOOL addJointState(const LLPointer<LLJointState>& joint_state, S32 priority, BOOL additive_blend);
 	void interpolate(F32 u);
 	void clear();
 	void resetCachedJoint();
diff --git a/indra/llcharacter/lltargetingmotion.cpp b/indra/llcharacter/lltargetingmotion.cpp
index d5fe65461b517400313b67c7497b527059b7ca7f..c0ce11cb85b8b978f998882aa02bd2714e688d93 100644
--- a/indra/llcharacter/lltargetingmotion.cpp
+++ b/indra/llcharacter/lltargetingmotion.cpp
@@ -55,6 +55,8 @@ LLTargetingMotion::LLTargetingMotion(const LLUUID &id) : LLMotion(id)
 {
 	mCharacter = NULL;
 	mName = "targeting";
+
+	mTorsoState = new LLJointState;
 }
 
 
@@ -87,11 +89,11 @@ LLMotion::LLMotionInitStatus LLTargetingMotion::onInitialize(LLCharacter *charac
 		return STATUS_FAILURE;
 	}
 
-	mTorsoState.setJoint( mTorsoJoint );
+	mTorsoState->setJoint( mTorsoJoint );
 
 	// add joint states to the pose
-	mTorsoState.setUsage(LLJointState::ROT);
-	addJointState( &mTorsoState );
+	mTorsoState->setUsage(LLJointState::ROT);
+	addJointState( mTorsoState );
 
 	return STATUS_SUCCESS;
 }
@@ -127,7 +129,7 @@ BOOL LLTargetingMotion::onUpdate(F32 time, U8* joint_mask)
 	}
 	
 	//LLVector3 target_plane_normal = LLVector3(1.f, 0.f, 0.f) * mPelvisJoint->getWorldRotation();
-	//LLVector3 torso_dir = LLVector3(1.f, 0.f, 0.f) * (mTorsoJoint->getWorldRotation() * mTorsoState.getRotation());
+	//LLVector3 torso_dir = LLVector3(1.f, 0.f, 0.f) * (mTorsoJoint->getWorldRotation() * mTorsoState->getRotation());
 
 	LLVector3 skyward(0.f, 0.f, 1.f);
 	LLVector3 left(skyward % target);
@@ -151,14 +153,14 @@ BOOL LLTargetingMotion::onUpdate(F32 time, U8* joint_mask)
 	new_torso_rot = new_torso_rot * ~cur_torso_rot;
 
 	// slerp from current additive rotation to ideal additive rotation
-	new_torso_rot = nlerp(slerp_amt, mTorsoState.getRotation(), new_torso_rot);
+	new_torso_rot = nlerp(slerp_amt, mTorsoState->getRotation(), new_torso_rot);
 
 	// constraint overall torso rotation
 	LLQuaternion total_rot = new_torso_rot * mTorsoJoint->getRotation();
 	total_rot.constrain(F_PI_BY_TWO * 0.8f);
 	new_torso_rot = total_rot * ~mTorsoJoint->getRotation();
 
-	mTorsoState.setRotation(new_torso_rot);
+	mTorsoState->setRotation(new_torso_rot);
 
 	return result;
 }
diff --git a/indra/llcharacter/lltargetingmotion.h b/indra/llcharacter/lltargetingmotion.h
index 9877467e4841825b6b7d29adad535a307b9c03d6..d72a8b11ec37924a16cb6f2efc18a73cc47fe7bc 100644
--- a/indra/llcharacter/lltargetingmotion.h
+++ b/indra/llcharacter/lltargetingmotion.h
@@ -111,7 +111,7 @@ class LLTargetingMotion :
 public:
 
 	LLCharacter			*mCharacter;
-	LLJointState		mTorsoState;
+	LLPointer<LLJointState>	mTorsoState;
 	LLJoint*			mPelvisJoint;
 	LLJoint*			mTorsoJoint;
 	LLJoint*			mRightHandJoint;
diff --git a/indra/llcommon/llversionviewer.h b/indra/llcommon/llversionviewer.h
index 1df8a89908ec81685347d9f9cc416246fb937eda..f9f3bf208747b39938c6ab4c70c83e6b652d31b5 100644
--- a/indra/llcommon/llversionviewer.h
+++ b/indra/llcommon/llversionviewer.h
@@ -34,8 +34,8 @@
 
 const S32 LL_VERSION_MAJOR = 1;
 const S32 LL_VERSION_MINOR = 18;
-const S32 LL_VERSION_PATCH = 5;
-const S32 LL_VERSION_BUILD = 3;
+const S32 LL_VERSION_PATCH = 6;
+const S32 LL_VERSION_BUILD = 0;
 
 const char * const LL_CHANNEL = "Second Life Release";
 
diff --git a/indra/llui/llpanel.cpp b/indra/llui/llpanel.cpp
index ca1bc9c52527d875439d9d7d456033e27960273f..6d000f3e7ff0e45a418c5a9c6e935b0ba1572c73 100644
--- a/indra/llui/llpanel.cpp
+++ b/indra/llui/llpanel.cpp
@@ -1194,6 +1194,13 @@ struct LLLayoutStack::LLEmbeddedPanel
 		}
 	}
 
+	~LLEmbeddedPanel()
+	{
+		// probably not necessary, but...
+		delete mResizeBar;
+		mResizeBar = NULL;
+	}
+
 	LLPanel* mPanel;
 	S32 mMinWidth;
 	S32 mMinHeight;
@@ -1212,6 +1219,7 @@ LLLayoutStack::LLLayoutStack(eLayoutOrientation orientation) :
 
 LLLayoutStack::~LLLayoutStack()
 {
+	std::for_each(mPanels.begin(), mPanels.end(), DeletePointer());
 }
 
 void LLLayoutStack::draw()
diff --git a/indra/llwindow/llwindow.h b/indra/llwindow/llwindow.h
index 9a3542d3fbb822864554d14e92a2320b5348b44b..8ef7066a314b5897e59fb1ecc3b8144d8807c234 100644
--- a/indra/llwindow/llwindow.h
+++ b/indra/llwindow/llwindow.h
@@ -355,9 +355,6 @@ extern const char* gURLProtocolWhitelistHandler[];
 // Loads a URL with the user's default browser
 void spawn_web_browser(const char* escaped_url);
 
-// Opens a file with ShellExecute. Security risk!
-void shell_open(const char* file_path);
-
 void simpleEscapeString ( std::string& stringIn  );
 
 #endif // _LL_window_h_
diff --git a/indra/llwindow/llwindowmacosx.cpp b/indra/llwindow/llwindowmacosx.cpp
index 943b98e9d59bd7b3c54f20a9095b5a15f96c1781..0a2f9cfb6c691484ba9c5e2bd94973a30797c0e3 100644
--- a/indra/llwindow/llwindowmacosx.cpp
+++ b/indra/llwindow/llwindowmacosx.cpp
@@ -70,7 +70,6 @@ const S32	MAX_NUM_RESOLUTIONS = 32;
 void show_window_creation_error(const char* title)
 {
 	llwarns << title << llendl;
-	shell_open( "help/window_creation_error.html");
 	/*
 	OSMessageBox(
 	"Second Life is unable to run because it can't set up your display.\n"
@@ -735,7 +734,6 @@ BOOL LLWindowMacOSX::createContext(int x, int y, int width, int height, int bits
 			if (check_for_card(RENDERER, CARD_LIST[i]))
 			{
 				close();
-				shell_open( "help/unsupported_card.html" );
 				return FALSE;
 			}
 		}
@@ -3200,46 +3198,6 @@ void spawn_web_browser(const char* escaped_url)
 	}
 }
 
-void shell_open( const char* file_path )
-{
-	OSStatus			result = noErr;
-
-	llinfos << "Opening " << file_path << llendl;
-	CFURLRef	urlRef = NULL;
-
-	CFStringRef	stringRef = CFStringCreateWithCString(NULL, file_path, kCFStringEncodingUTF8);
-	if (stringRef)
-	{
-		// This will succeed if the string is a full URL, including the http://
-		// Note that URLs specified this way need to be properly percent-escaped.
-		urlRef = CFURLCreateWithString(NULL, stringRef, NULL);
-
-		if(urlRef == NULL)
-		{
-			// This will succeed if the string is a full or partial posix path.
-			// This will work even if the path contains characters that would need to be percent-escaped
-			// in the URL (such as spaces).
-			urlRef = CFURLCreateWithFileSystemPath(NULL, stringRef, kCFURLPOSIXPathStyle, false);
-		}
-
-		CFRelease(stringRef);
-	}
-
-	if (urlRef)
-	{
-		result = LSOpenCFURLRef(urlRef, NULL);
-
-		if (result != noErr)
-		{
-			llinfos << "Error " << result << " on open." << llendl;
-		}
-		CFRelease(urlRef);
-	}
-	else
-	{
-		llinfos << "Error: couldn't create URL." << llendl;
-	}
-}
 
 BOOL LLWindowMacOSX::dialog_color_picker ( F32 *r, F32 *g, F32 *b)
 {
diff --git a/indra/llwindow/llwindowmacosx.h b/indra/llwindow/llwindowmacosx.h
index 2a4cf973088f4f474b788a8ce09191f84069e7ba..2fdf80ee829fe99e7f9e4b4eb24e9be6380ef049 100644
--- a/indra/llwindow/llwindowmacosx.h
+++ b/indra/llwindow/llwindowmacosx.h
@@ -218,6 +218,5 @@ class LLSplashScreenMacOSX : public LLSplashScreen
 S32 OSMessageBoxMacOSX(const char* text, const char* caption, U32 type);
 
 void load_url_external(const char* url);
-void shell_open( const char* file_path );
 
 #endif //LL_LLWINDOWMACOSX_H
diff --git a/indra/llwindow/llwindowsdl.cpp b/indra/llwindow/llwindowsdl.cpp
index ad39c84ac8f7cd68896e16bdcac928b765da9ba1..cbf4b7dc3ab4900974dab074f2bf0fa203726dbb 100644
--- a/indra/llwindow/llwindowsdl.cpp
+++ b/indra/llwindow/llwindowsdl.cpp
@@ -2230,11 +2230,11 @@ static SDL_Cursor *makeSDLCursorFromBMP(const char *filename, int hotx, int hoty
 						   bmpsurface->w,
 						   bmpsurface->h,
 						   32,
-						   0xFFU,
-						   0xFF00U,
-						   0xFF0000U,
-						   0xFF000000U);
-		SDL_FillRect(cursurface, NULL, 0x00000000U);
+						   SDL_SwapLE32(0xFFU),
+						   SDL_SwapLE32(0xFF00U),
+						   SDL_SwapLE32(0xFF0000U),
+						   SDL_SwapLE32(0xFF000000U));
+		SDL_FillRect(cursurface, NULL, SDL_SwapLE32(0x00000000U));
 
 		// Blit the cursor pixel data onto a 32-bit RGBA surface so we
 		// only have to cope with processing one type of pixel format.
@@ -2253,13 +2253,13 @@ static SDL_Cursor *makeSDLCursorFromBMP(const char *filename, int hotx, int hoty
 			// is inferred by color-keying against 200,200,200
 			for (i=0; i<cursurface->h; ++i) {
 				for (j=0; j<cursurface->w; ++j) {
-					unsigned char *pixelp =
-						((unsigned char *)cursurface->pixels)
+					U8 *pixelp =
+						((U8*)cursurface->pixels)
 						+ cursurface->pitch * i
 						+ j*cursurface->format->BytesPerPixel;
-					unsigned char srcred = pixelp[0];
-					unsigned char srcgreen = pixelp[1];
-					unsigned char srcblue = pixelp[2];
+					U8 srcred = pixelp[0];
+					U8 srcgreen = pixelp[1];
+					U8 srcblue = pixelp[2];
 					BOOL mask_bit = (srcred != 200)
 						|| (srcgreen != 200)
 						|| (srcblue != 200);
@@ -2741,11 +2741,6 @@ void spawn_web_browser(const char* escaped_url)
 	llinfos << "spawn_web_browser returning." << llendl;
 }
 
-void shell_open( const char* file_path )
-{
-	// *TODO: This function is deprecated and should probably go away.
-	llwarns << "Deprecated shell_open(): " << file_path << llendl;
-}
 
 void *LLWindowSDL::getPlatformWindow()
 {
diff --git a/indra/llwindow/llwindowsdl.h b/indra/llwindow/llwindowsdl.h
index aa331ed742cd24cf90e6bc55112391ab8cb5ceee..2c411e03fdd6560c3b552fcd007d03e2a3970a52 100644
--- a/indra/llwindow/llwindowsdl.h
+++ b/indra/llwindow/llwindowsdl.h
@@ -37,6 +37,7 @@
 #include "llwindow.h"
 
 #include "SDL/SDL.h"
+#include "SDL/SDL_endian.h"
 
 #if LL_X11
 // get X11-specific headers for use in low-level stuff like copy-and-paste support
@@ -222,7 +223,6 @@ class LLSplashScreenSDL : public LLSplashScreen
 S32 OSMessageBoxSDL(const char* text, const char* caption, U32 type);
 
 void load_url_external(const char* url);
-void shell_open( const char* file_path );
 
 #if LL_GTK
 // Lazily initialize and check the runtime GTK version for goodness.
diff --git a/indra/llwindow/llwindowwin32.cpp b/indra/llwindow/llwindowwin32.cpp
index 4e4bed64859ca20829a46cd7364a17aefacb435c..b8fd9948e3ec7be30ad22a4b963cacf04efcae24 100644
--- a/indra/llwindow/llwindowwin32.cpp
+++ b/indra/llwindow/llwindowwin32.cpp
@@ -84,7 +84,6 @@ LLW32MsgCallback gAsyncMsgCallback = NULL;
 void show_window_creation_error(const char* title)
 {
 	llwarns << title << llendl;
-	shell_open( "help/window_creation_error.html");
 }
 
 //static
@@ -3398,24 +3397,6 @@ void spawn_web_browser(const char* escaped_url )
 	}
 }
 
-void shell_open( const char* file_path )
-{
-	llinfos << "Opening " << file_path << llendl;
-
-	WCHAR wstr[1024];
-	mbstowcs(wstr, file_path, 1024);
-
-	HWND our_window = NULL;
-	int retval = (int) ShellExecute(our_window, L"open", wstr, NULL, NULL, SW_SHOWNORMAL);	/* Flawfinder: ignore */
-	if (retval > 32)
-	{
-		llinfos << "ShellExecute success with " << retval << llendl;
-	}
-	else
-	{
-		llinfos << "ShellExecute failure with " << retval << llendl;
-	}
-}
 
 BOOL LLWindowWin32::dialog_color_picker ( F32 *r, F32 *g, F32 *b )
 {
diff --git a/indra/newview/English.lproj/InfoPlist.strings b/indra/newview/English.lproj/InfoPlist.strings
index da8951428a798ef7375b07836b7eff88493906c8..85f7e2530fac3190c6b58d22f0ceb5dcc105ede9 100644
--- a/indra/newview/English.lproj/InfoPlist.strings
+++ b/indra/newview/English.lproj/InfoPlist.strings
@@ -1,5 +1,5 @@
 /* Localized versions of Info.plist keys */
 
 CFBundleName = "Second Life";
-CFBundleShortVersionString = "Second Life version 1.18.5.3";
-CFBundleGetInfoString = "Second Life version 1.18.5.3, Copyright 2004-2007 Linden Research, Inc.";
+CFBundleShortVersionString = "Second Life version 1.18.6.0";
+CFBundleGetInfoString = "Second Life version 1.18.6.0, Copyright 2004-2007 Linden Research, Inc.";
diff --git a/indra/newview/Info-SecondLife.plist b/indra/newview/Info-SecondLife.plist
index 427651ccba984208974ca54b9bd6c68d2dba6632..59822a1e4c0d83d1bf65a5dbf77d07c8a8d25119 100644
--- a/indra/newview/Info-SecondLife.plist
+++ b/indra/newview/Info-SecondLife.plist
@@ -32,7 +32,7 @@
 		</dict>
 	</array>
 	<key>CFBundleVersion</key>
-	<string>1.18.5.3</string>
+	<string>1.18.6.0</string>
 	<key>CSResourcesFileMapped</key>
 	<true/>
 </dict>
diff --git a/indra/newview/linux_tools/client-readme.txt b/indra/newview/linux_tools/client-readme.txt
index 641705da539de7f486208d2123265f1753a69678..832dff4209445790e27c9b8bfe97f507b470123d 100644
--- a/indra/newview/linux_tools/client-readme.txt
+++ b/indra/newview/linux_tools/client-readme.txt
@@ -50,15 +50,18 @@ Please enjoy!
 
 Minimum requirements:
     * Internet Connection: Cable or DSL
-    * Computer Processor: 800MHz Pentium III or Athlon, or better
-    * Computer Memory: 256MB or better (strongly recommend more!)
+    * Computer Processor: 800MHz Pentium III or Athlon or better
+      (recommended: 1.5GHz or more)
+    * Computer Memory: 512MB (recommended: 768MB or more)
     * Linux Operating System: A reasonably modern 32-bit Linux environment
           is required.  If you are running a 64-bit Linux distribution then
           you will need its 32-bit compatibility environment installed.
     * Video/Graphics Card:
-          o nVidia GeForce 2, GeForce 4mx, or better
+          o nVidia GeForce 2, GeForce 4mx, or better (recommend one of the
+            following: 6700, 6800, 7600, 7800, 7900, 8400, 8500, 8600,
+            8800, Go 7400, Go 7600, Go 7800, Go 7900)
           o OR ATI Radeon 8500, 9250, or better
-          (nVidia cards are strongly recommended for the Linux client)
+          (nVidia cards are recommended for the Linux client)
 
       **NOTE**: Second Life absolutely requires you to have recent, correctly-
       configured OpenGL 3D drivers for your hardware - the graphics drivers
diff --git a/indra/newview/linux_tools/wrapper.sh b/indra/newview/linux_tools/wrapper.sh
index 638a0f0b76edd7952688daf55f6bfc6300540367..35efbc13b87172a6e673a91bdd69f2460aef1aaa 100755
--- a/indra/newview/linux_tools/wrapper.sh
+++ b/indra/newview/linux_tools/wrapper.sh
@@ -59,7 +59,8 @@ fi
 ## Nothing worth editing below this line.
 ##-------------------------------------------------------------------
 
-RUN_PATH=`dirname "$0" || echo .`
+SCRIPTSRC=`readlink -f "$0" || echo "$0"`
+RUN_PATH=`dirname "${SCRIPTSRC}" || echo .`
 cd "${RUN_PATH}"
 
 # Re-register the secondlife:// protocol handler every launch, for now.
diff --git a/indra/newview/lldrawpoolwater.cpp b/indra/newview/lldrawpoolwater.cpp
index 4a7760f15cd53993dd6eb88e96f1a233cf47d689..d7fc716c55e49095d2690d6731f4c5198cdda352 100644
--- a/indra/newview/lldrawpoolwater.cpp
+++ b/indra/newview/lldrawpoolwater.cpp
@@ -567,7 +567,15 @@ void LLDrawPoolWater::shade()
 	LLCubeMap* skyMap = gSky.mVOSkyp->getCubeMap();
 	
 	gWaterProgram.enableTexture(LLShaderMgr::ENVIRONMENT_MAP, GL_TEXTURE_CUBE_MAP_ARB);
-	skyMap->bind();
+
+	if (skyMap)
+	{
+		skyMap->bind();
+	}
+	else
+	{
+		llwarns << "NULL gSky.mVOSkyp->getCubeMap(), not binding." << llendl;
+	}
 
 	//bind normal map
 	S32 bumpTex = gWaterProgram.enableTexture(LLShaderMgr::BUMP_MAP);
diff --git a/indra/newview/llpanellogin.cpp b/indra/newview/llpanellogin.cpp
index 7f846c2791f5c4009ee8557766e9f880433ff934..bc297354f72716a8dc1c9cbb1ae1660c27f7f4fb 100644
--- a/indra/newview/llpanellogin.cpp
+++ b/indra/newview/llpanellogin.cpp
@@ -38,7 +38,6 @@
 #include "llfontgl.h"
 #include "llmd5.h"
 #include "llsecondlifeurls.h"
-#include "llwindow.h"			// shell_open()
 #include "llversionviewer.h"
 #include "v4color.h"
 
diff --git a/indra/newview/llpolymesh.cpp b/indra/newview/llpolymesh.cpp
index 492e590173f4442459baa8c1b91eea1fed0d7958..dc67ff48eac07b2e514a1f16253d6a1001739213 100644
--- a/indra/newview/llpolymesh.cpp
+++ b/indra/newview/llpolymesh.cpp
@@ -862,14 +862,6 @@ void LLPolyMesh::dumpDiagInfo()
 	llinfos << buf << llendl;
 	llinfos << "-----------------------------------------------------" << llendl;
 }
- 
-//-----------------------------------------------------------------------------
-// getCoords()
-//-----------------------------------------------------------------------------
-const LLVector3	*LLPolyMesh::getCoords() const
-{
-	return mCoords;
-}
 
 //-----------------------------------------------------------------------------
 // getWritableCoords()
@@ -984,14 +976,6 @@ void	LLPolyMesh::deleteAllMorphData()
 	mSharedData->mMorphData.deleteAllData();
 }
 
-//-----------------------------------------------------------------------------
-// getWeights()
-//-----------------------------------------------------------------------------
-const F32*	LLPolyMesh::getWeights() const
-{ 
-	return mSharedData->mWeights;
-}
-
 //-----------------------------------------------------------------------------
 // getWritableWeights()
 //-----------------------------------------------------------------------------
diff --git a/indra/newview/llpolymesh.h b/indra/newview/llpolymesh.h
index 3cf4acb0fa8848a1c1ed49d2bb7429ab9d750095..8003c4a89d3ee6c79223e94c3d4444fe2798e414 100644
--- a/indra/newview/llpolymesh.h
+++ b/indra/newview/llpolymesh.h
@@ -224,7 +224,9 @@ class LLPolyMesh
 	}
 
 	// Get coords
-	const LLVector3	*getCoords() const;
+	const LLVector3	*getCoords() const{
+		return mCoords;
+	}
 
 	// non const version
 	LLVector3 *getWritableCoords();
@@ -273,7 +275,10 @@ class LLPolyMesh
 	}
 
 	// Get weights
-	const F32		*getWeights() const;
+	const F32 *getWeights() const {
+		llassert (mSharedData);
+		return mSharedData->mWeights;
+	}
 
 	F32			*getWritableWeights() const;
 
diff --git a/indra/newview/llstartup.cpp b/indra/newview/llstartup.cpp
index 302291ab52bd458fecf40c871c373a8c7fbc6875..f12f5763ef8f45fded9595f2b092e08fa5813abe 100644
--- a/indra/newview/llstartup.cpp
+++ b/indra/newview/llstartup.cpp
@@ -70,7 +70,6 @@
 #include "lluserrelations.h"
 #include "llversionviewer.h"
 #include "llvfs.h"
-#include "llwindow.h"		// for shell_open
 #include "llxorcipher.h"	// saved password, MAC address
 #include "message.h"
 #include "v3math.h"
diff --git a/indra/newview/llviewermenu.cpp b/indra/newview/llviewermenu.cpp
index 7aa25266fed00db2d5418492e712f1b4ba018e29..2fccdcd153c3aad4294693e8c13d69760726c9fb 100644
--- a/indra/newview/llviewermenu.cpp
+++ b/indra/newview/llviewermenu.cpp
@@ -60,7 +60,6 @@
 #include "lltimer.h"
 #include "llvfile.h"
 #include "llvolumemgr.h"
-#include "llwindow.h"	// for shell_open()
 
 // newview includes
 #include "llagent.h"
diff --git a/indra/newview/llviewermessage.cpp b/indra/newview/llviewermessage.cpp
index fa92bfb217baeaed651132a251f578653476df43..ebb0da016fa8495a4dec981e425182afa131e085 100644
--- a/indra/newview/llviewermessage.cpp
+++ b/indra/newview/llviewermessage.cpp
@@ -59,7 +59,6 @@
 #include "llteleportflags.h"
 #include "lltracker.h"
 #include "lltransactionflags.h"
-#include "llwindow.h"			// shell_open()
 #include "llxfermanager.h"
 #include "message.h"
 #include "sound_ids.h"
@@ -1199,7 +1198,22 @@ void inventory_offer_handler(LLOfferInfo* info, BOOL from_task)
 
 	LLString::format_map_t args;
 	args["[OBJECTNAME]"] = info->mDesc;
-	args["[OBJECTTYPE]"] = LLAssetType::lookupHumanReadable(info->mType);
+	// must protect against a NULL return from lookupHumanReadable()
+	const char* typestr = LLAssetType::lookupHumanReadable(info->mType);
+	if (typestr)
+	{
+		args["[OBJECTTYPE]"] = typestr;
+	}
+	else
+	{
+		llwarns << "LLAssetType::lookupHumanReadable() returned NULL - probably bad asset type: " << info->mType << llendl;
+		args["[OBJECTTYPE]"] = "";
+
+		// This seems safest, rather than propagating bogosity
+		llwarns << "Forcing an inventory-decline for probably-bad asset type." << llendl;
+		inventory_offer_callback(IOR_DECLINE, info);
+		return;
+	}
 
 	// Name cache callbacks don't store userdata, so can't save
 	// off the LLOfferInfo.  Argh.  JC
diff --git a/indra/newview/llvoavatar.cpp b/indra/newview/llvoavatar.cpp
index 7a225c8a12bee5b78160d07fcaacb97e3e3fdadf..fc159ddc48ef614132486165ba82665102bc7596 100644
--- a/indra/newview/llvoavatar.cpp
+++ b/indra/newview/llvoavatar.cpp
@@ -311,7 +311,13 @@ class LLBodyNoiseMotion :
 {
 public:
 	// Constructor
-	LLBodyNoiseMotion(const LLUUID &id) : LLMotion(id) {mName = "body_noise";}
+	LLBodyNoiseMotion(const LLUUID &id)
+		: LLMotion(id)
+	{
+		mName = "body_noise";
+
+		mTorsoState = new LLJointState;
+	}
 
 	// Destructor
 	virtual ~LLBodyNoiseMotion() { }
@@ -354,14 +360,14 @@ class LLBodyNoiseMotion :
 	// must return true to indicate success and be available for activation
 	virtual LLMotionInitStatus onInitialize(LLCharacter *character)
 	{
-		if( !mTorsoState.setJoint( character->getJoint("mTorso") ))
+		if( !mTorsoState->setJoint( character->getJoint("mTorso") ))
 		{
 			return STATUS_FAILURE;
 		}
 
-		mTorsoState.setUsage(LLJointState::ROT);
+		mTorsoState->setUsage(LLJointState::ROT);
 
-		addJointState( &mTorsoState );
+		addJointState( mTorsoState );
 		return STATUS_SUCCESS;
 	}
 
@@ -388,7 +394,7 @@ class LLBodyNoiseMotion :
 		F32 ry = TORSO_NOISE_AMOUNT * DEG_TO_RAD * noiseY / 0.42f;
 		LLQuaternion tQn;
 		tQn.setQuat( rx, ry, 0.0f );
-		mTorsoState.setRotation( tQn );
+		mTorsoState->setRotation( tQn );
 
 		return TRUE;
 	}
@@ -400,7 +406,7 @@ class LLBodyNoiseMotion :
 	//-------------------------------------------------------------------------
 	// joint states to be animated
 	//-------------------------------------------------------------------------
-	LLJointState		mTorsoState;
+	LLPointer<LLJointState> mTorsoState;
 };
 
 //-----------------------------------------------------------------------------
@@ -417,6 +423,8 @@ class LLBreatheMotionRot :
 		mCharacter(NULL)
 	{
 		mName = "breathe_rot";
+
+		mChestState = new LLJointState;
 	}
 
 	// Destructor
@@ -463,12 +471,12 @@ class LLBreatheMotionRot :
 		mCharacter = character;
 		bool success = true;
 
-		if ( !mChestState.setJoint( character->getJoint( "mChest" ) ) ) { success = false; }
+		if ( !mChestState->setJoint( character->getJoint( "mChest" ) ) ) { success = false; }
 
 		if ( success )
 		{
-			mChestState.setUsage(LLJointState::ROT);
-			addJointState( &mChestState );
+			mChestState->setUsage(LLJointState::ROT);
+			addJointState( mChestState );
 		}
 
 		if ( success )
@@ -495,7 +503,7 @@ class LLBreatheMotionRot :
 
 		F32 breathe_amt = (sinf(mBreatheRate * time) * BREATHE_ROT_MOTION_STRENGTH);
 
-		mChestState.setRotation(LLQuaternion(breathe_amt, LLVector3(0.f, 1.f, 0.f)));
+		mChestState->setRotation(LLQuaternion(breathe_amt, LLVector3(0.f, 1.f, 0.f)));
 
 		return TRUE;
 	}
@@ -507,7 +515,7 @@ class LLBreatheMotionRot :
 	//-------------------------------------------------------------------------
 	// joint states to be animated
 	//-------------------------------------------------------------------------
-	LLJointState		mChestState;	
+	LLPointer<LLJointState> mChestState;
 	F32					mBreatheRate;
 	LLCharacter*		mCharacter;
 };
@@ -520,7 +528,13 @@ class LLPelvisFixMotion :
 {
 public:
 	// Constructor
-	LLPelvisFixMotion(const LLUUID &id) : LLMotion(id), mCharacter(NULL) {mName = "pelvis_fix";}
+	LLPelvisFixMotion(const LLUUID &id)
+		: LLMotion(id), mCharacter(NULL)
+	{
+		mName = "pelvis_fix";
+
+		mPelvisState = new LLJointState;
+	}
 
 	// Destructor
 	virtual ~LLPelvisFixMotion() { }
@@ -565,14 +579,14 @@ class LLPelvisFixMotion :
 	{
 		mCharacter = character;
 
-		if (!mPelvisState.setJoint( character->getJoint("mPelvis")))
+		if (!mPelvisState->setJoint( character->getJoint("mPelvis")))
 		{
 			return STATUS_FAILURE;
 		}
 
-		mPelvisState.setUsage(LLJointState::POS);
+		mPelvisState->setUsage(LLJointState::POS);
 
-		addJointState( &mPelvisState );
+		addJointState( mPelvisState );
 		return STATUS_SUCCESS;
 	}
 
@@ -586,7 +600,7 @@ class LLPelvisFixMotion :
 	// must return FALSE when the motion is completed.
 	virtual BOOL onUpdate(F32 time, U8* joint_mask)
 	{
-		mPelvisState.setPosition(LLVector3::zero);
+		mPelvisState->setPosition(LLVector3::zero);
 
 		return TRUE;
 	}
@@ -598,7 +612,7 @@ class LLPelvisFixMotion :
 	//-------------------------------------------------------------------------
 	// joint states to be animated
 	//-------------------------------------------------------------------------
-	LLJointState		mPelvisState;
+	LLPointer<LLJointState> mPelvisState;
 	LLCharacter*		mCharacter;
 };
 
diff --git a/indra/newview/macview_Prefix.h b/indra/newview/macview_Prefix.h
index 4d982f18973c50f125c9edce5b3c3a78f898863f..08c59bcedd900f965492f740f58ea98f8dc5ce52 100644
--- a/indra/newview/macview_Prefix.h
+++ b/indra/newview/macview_Prefix.h
@@ -221,7 +221,7 @@
 #include "indra_constants.h"		// for key and mask constants
 #include "llfontgl.h"
 #include "v4color.h"
-#include "llwindow_impl.h"			// shell_open()
+#include "llwindow_impl.h"
 
 #include "llbutton.h"
 #include "llcheckboxctrl.h"
diff --git a/indra/newview/pipeline.cpp b/indra/newview/pipeline.cpp
index e9faccb4da605735bf0c2c892807a8ec057cf258..b16a9fb02ba87133ede3416ae6256fec9431a394 100644
--- a/indra/newview/pipeline.cpp
+++ b/indra/newview/pipeline.cpp
@@ -3667,6 +3667,11 @@ void LLPipeline::clearRenderMap()
 
 void LLPipeline::resetVertexBuffers(LLDrawable* drawable)
 {
+	if (!drawable)
+	{
+		return;
+	}
+
 	for (S32 i = 0; i < drawable->getNumFaces(); i++)
 	{
 		LLFace* facep = drawable->getFace(i);