diff --git a/Ref.h b/Ref.h index 4e7b6d9aad42f92d64e7db4871837b94e5656108..053353285be2914d04a68d40e17a65bf5651ff6f 100644 --- a/Ref.h +++ b/Ref.h @@ -38,7 +38,12 @@ protected: }; template <class T> -Ref<T>::Ref( T * object ) : _object(object) {} +Ref<T>::Ref( T * object ) : _object(object) { + //If object isn't null, increment reference count + if ( _object != NULL ) { + _object->AddRef(); + } +} template <class T> Ref<T>::Ref(const Ref & ref_to_copy ) { diff --git a/obj/NiNode.cpp b/obj/NiNode.cpp index 947040a83426830815cdd8a3b99175b4390e2a5d..681e9824c537f29e8b1901443a2b7f5c87374f25 100644 --- a/obj/NiNode.cpp +++ b/obj/NiNode.cpp @@ -70,7 +70,7 @@ void NiNode::RemoveChild( Ref<NiAVObject> obj ) { void NiNode::ClearChildren() { for ( vector< NiAVObjectRef >::iterator it = children.begin(); it != children.end(); ++it) { - (*it)->SetParent(NULL); + if (*it) (*it)->SetParent(NULL); } children.clear(); } diff --git a/obj/NiObject.cpp b/obj/NiObject.cpp index 2bf16bec2d48b3d4e62fddde32b4a359d6a2fcc0..5a0c4c0cddedef1bb4a1d841a0377e935ac548f9 100644 --- a/obj/NiObject.cpp +++ b/obj/NiObject.cpp @@ -33,9 +33,7 @@ void NiObject::AddRef() { } void NiObject::SubtractRef() { - --_ref_count; - - if ( _ref_count < 0 ) { + if ( --_ref_count == 0 ) { delete this; } } diff --git a/obj/NiObject.h b/obj/NiObject.h index 104a4603a600c11a062fa74bcca8880cf8c4565f..65599444f7bb50c293f36f75c4bd2ffc3fd4ce5f 100644 --- a/obj/NiObject.h +++ b/obj/NiObject.h @@ -151,7 +151,7 @@ template <class T> Ref<T> StaticCast( NiObject * object ) { return (T*)object; } -template <class T> const Ref<T> SaticCast (const NiObject * object) { +template <class T> Ref<const T> StaticCast (const NiObject * object) { return (const T*)object; } @@ -163,7 +163,7 @@ template <class T> Ref<T> DynamicCast( NiObject * object ) { } } -template <class T> const Ref<T> DynamicCast( const NiObject * object ) { +template <class T> Ref<const T> DynamicCast( const NiObject * object ) { if ( object->IsDerivedType(T::TYPE) ) { return (const T*)object; } else {