Fix RS bugs.  We were holding a pointer to the script text from the java vm. Move freeing of objects to before context teardown to allow allocations to clean up their data.
diff --git a/rsAllocation.cpp b/rsAllocation.cpp
index 2cbfe17..38cec64 100644
--- a/rsAllocation.cpp
+++ b/rsAllocation.cpp
@@ -54,6 +54,17 @@
 {
     free(mPtr);
     mPtr = NULL;
+
+    if (mBufferID) {
+        // Causes a SW crash....
+        //LOGV(" mBufferID %i", mBufferID);
+        //glDeleteBuffers(1, &mBufferID);
+        //mBufferID = 0;
+    }
+    if (mTextureID) {
+        glDeleteTextures(1, &mTextureID);
+        mTextureID = 0;
+    }
 }
 
 void Allocation::setCpuWritable(bool)
diff --git a/rsContext.cpp b/rsContext.cpp
index 961ec0b..2e6d7b0 100644
--- a/rsContext.cpp
+++ b/rsContext.cpp
@@ -342,6 +342,9 @@
      rsc->mStateFragmentStore.deinit(rsc);
      ObjectBase::zeroAllUserRef(rsc);
 
+     rsc->mObjDestroy.mNeedToEmpty = true;
+     rsc->objDestroyOOBRun();
+
      glClearColor(0,0,0,0);
      glClear(GL_COLOR_BUFFER_BIT);
      eglSwapBuffers(rsc->mEGL.mDisplay, rsc->mEGL.mSurface);
@@ -350,8 +353,6 @@
      rsc->deinitEGL();
      pthread_mutex_unlock(&gInitMutex);
 
-     rsc->mObjDestroy.mNeedToEmpty = true;
-     rsc->objDestroyOOBRun();
      LOGV("RS Thread exited");
      return NULL;
 }
diff --git a/rsObjectBase.cpp b/rsObjectBase.cpp
index 1b442ba..b7d67cc 100644
--- a/rsObjectBase.cpp
+++ b/rsObjectBase.cpp
@@ -113,12 +113,7 @@
 
 void ObjectBase::setName(const char *name)
 {
-    delete mName;
-    mName = NULL;
-    if (name) {
-        mName = new char[strlen(name) +1];
-        strcpy(mName, name);
-    }
+    setName(name, strlen(name));
 }
 
 void ObjectBase::setName(const char *name, uint32_t len)
diff --git a/rsScript.h b/rsScript.h
index 8aa4542..bc40854 100644
--- a/rsScript.h
+++ b/rsScript.h
@@ -54,7 +54,7 @@
         ObjectBaseRef<ProgramRaster> mRaster;
         ObjectBaseRef<ProgramFragmentStore> mFragmentStore;
         InvokeFunc_t mInvokables[MAX_SCRIPT_BANKS];
-        const char * mScriptText;
+        char * mScriptText;
         uint32_t mScriptTextLength;
     };
     Enviroment_t mEnviroment;
diff --git a/rsScriptC.cpp b/rsScriptC.cpp
index 9da7766..073d98b 100644
--- a/rsScriptC.cpp
+++ b/rsScriptC.cpp
@@ -46,6 +46,8 @@
     if (mAccScript) {
         accDeleteScript(mAccScript);
     }
+    free(mEnviroment.mScriptText);
+    mEnviroment.mScriptText = NULL;
 }
 
 void ScriptC::setupScript()
@@ -404,7 +406,11 @@
 void rsi_ScriptCSetText(Context *rsc, const char *text, uint32_t len)
 {
     ScriptCState *ss = &rsc->mScriptC;
-    ss->mScript->mEnviroment.mScriptText = text;
+
+    char *t = (char *)malloc(len + 1);
+    memcpy(t, text, len);
+    t[len] = 0;
+    ss->mScript->mEnviroment.mScriptText = t;
     ss->mScript->mEnviroment.mScriptTextLength = len;
 }
 
diff --git a/rsScriptC.h b/rsScriptC.h
index ae124b4..69afc18 100644
--- a/rsScriptC.h
+++ b/rsScriptC.h
@@ -41,10 +41,6 @@
     virtual ~ScriptC();
 
     struct Program_t {
-        const char * mScriptText;
-        uint32_t mScriptTextLength;
-
-
         int mVersionMajor;
         int mVersionMinor;