Fix memory leak due to smart pointer misuse.

bug 11965932

Change-Id: I982b974b3554b0f4dc7a558107703ff605f580e6
diff --git a/cpp/BaseObj.cpp b/cpp/BaseObj.cpp
index 80d9414..2e0a637 100644
--- a/cpp/BaseObj.cpp
+++ b/cpp/BaseObj.cpp
@@ -33,7 +33,7 @@
 
 
 BaseObj::BaseObj(void *id, sp<RS> rs) {
-    mRS = rs;
+    mRS = rs.get();
     mID = id;
 }
 
@@ -44,7 +44,9 @@
 }
 
 BaseObj::~BaseObj() {
-    RS::dispatch->ObjDestroy(mRS->getContext(), mID);
+    if (mRS && mRS->getContext()) {
+        RS::dispatch->ObjDestroy(mRS->getContext(), mID);
+    }
     mRS = NULL;
     mID = NULL;
 }
diff --git a/cpp/Element.cpp b/cpp/Element.cpp
index d3fb29a..ce84699 100644
--- a/cpp/Element.cpp
+++ b/cpp/Element.cpp
@@ -358,7 +358,7 @@
 }
 
 Element::Builder::Builder(android::RSC::sp<RS> rs) {
-    mRS = rs;
+    mRS = rs.get();
     mSkipPadding = false;
 }
 
diff --git a/cpp/Type.cpp b/cpp/Type.cpp
index 07da0c5..d053730 100644
--- a/cpp/Type.cpp
+++ b/cpp/Type.cpp
@@ -116,7 +116,7 @@
 }
 
 Type::Builder::Builder(sp<RS> rs, sp<const Element> e) {
-    mRS = rs;
+    mRS = rs.get();
     mElement = e;
     mDimX = 0;
     mDimY = 0;
diff --git a/cpp/rsCppStructs.h b/cpp/rsCppStructs.h
index 1efd128..8fb18cd 100644
--- a/cpp/rsCppStructs.h
+++ b/cpp/rsCppStructs.h
@@ -260,7 +260,7 @@
 
 protected:
     void *mID;
-    sp<RS> mRS;
+    RS* mRS;
     std::string mName;
 
     BaseObj(void *id, sp<RS> rs);
@@ -1031,7 +1031,7 @@
      */
     class Builder {
     private:
-        sp<RS> mRS;
+        RS* mRS;
         std::vector<sp<Element> > mElements;
         std::vector<std::string> mElementNames;
         std::vector<uint32_t> mArraySizes;
@@ -1285,7 +1285,7 @@
 
     class Builder {
     protected:
-        sp<RS> mRS;
+        RS* mRS;
         uint32_t mDimX;
         uint32_t mDimY;
         uint32_t mDimZ;