Index: pjproject/trunk/pjmedia/src/pjmedia-audiodev/android_jni_dev.c
===================================================================
--- pjproject/trunk/pjmedia/src/pjmedia-audiodev/android_jni_dev.c	(revision 5356)
+++ pjproject/trunk/pjmedia/src/pjmedia-audiodev/android_jni_dev.c	(revision 5357)
@@ -60,5 +60,5 @@
     pjmedia_dir         dir;
     pjmedia_aud_param   param;
-    
+
     int                 bytes_per_sample;
     pj_uint32_t         samples_per_sec;
@@ -77,6 +77,6 @@
     pj_thread_t        *rec_thread;
     pj_sem_t           *rec_sem;
-    pj_timestamp	rec_timestamp;
-    
+    pj_timestamp        rec_timestamp;    
+
     /* Track */
     jobject             track;
@@ -87,5 +87,5 @@
     pj_thread_t        *play_thread;
     pj_sem_t           *play_sem;
-    pj_timestamp	play_timestamp;
+    pj_timestamp        play_timestamp;
 };
 
@@ -664,4 +664,5 @@
     if (stream->dir & PJMEDIA_DIR_CAPTURE) {
         jthrowable exc;
+        jobject record_obj;
         int mic_source = 0; /* DEFAULT: default audio source */
 
@@ -695,13 +696,13 @@
         
         do {
-            stream->record =  (*jni_env)->NewObject(jni_env,
-                                                    stream->record_class,
-                                                    constructor_method,
-                                                    mic_source, 
-                                                    param->clock_rate,
-                                                    channelInCfg,
-                                                    sampleFormat,
-                                                    inputBuffSizeRec);
-            if (stream->record == 0) {
+            record_obj =  (*jni_env)->NewObject(jni_env,
+                                                stream->record_class,
+                                                constructor_method,
+                                                mic_source, 
+                                                param->clock_rate,
+                                                channelInCfg,
+                                                sampleFormat,
+                                                inputBuffSizeRec);
+            if (record_obj == 0) {
                 PJ_LOG(3, (THIS_FILE, "Unable to create audio record object"));
                 status = PJMEDIA_EAUD_INIT;
@@ -732,6 +733,5 @@
                 goto on_error;
             }
-            state = (*jni_env)->CallIntMethod(jni_env, stream->record,
-                                              method_id);
+            state = (*jni_env)->CallIntMethod(jni_env, record_obj, method_id);
             if (state == 0) { /* STATE_UNINITIALIZED */
                 PJ_LOG(3, (THIS_FILE, "Failure in initializing audio record."));
@@ -745,7 +745,14 @@
         } while (state == 0);
         
-        stream->record = (*jni_env)->NewGlobalRef(jni_env, stream->record);
+        stream->record = (*jni_env)->NewGlobalRef(jni_env, record_obj);
         if (stream->record == 0) {
-            PJ_LOG(3, (THIS_FILE, "Unable to create audio record global ref."));
+            jmethodID release_method=0;
+            
+            PJ_LOG(3, (THIS_FILE, "Unable to create audio record global ref."));            
+            release_method = (*jni_env)->GetMethodID(jni_env, 
+                                                     stream->record_class,
+                                                     "release", "()V");
+            (*jni_env)->CallVoidMethod(jni_env, record_obj, release_method);
+            
             status = PJMEDIA_EAUD_INIT;
             goto on_error;
@@ -767,4 +774,5 @@
     if (stream->dir & PJMEDIA_DIR_PLAYBACK) {
         jthrowable exc;
+        jobject track_obj;
         
         /* Get pointer to the constructor */
@@ -778,14 +786,14 @@
         }
         
-        stream->track = (*jni_env)->NewObject(jni_env,
-                                              stream->track_class,
-                                              constructor_method,
-                                              0, /* STREAM_VOICE_CALL */
-                                              param->clock_rate,
-                                              channelOutCfg,
-                                              sampleFormat,
-                                              inputBuffSizePlay,
-                                              1 /* MODE_STREAM */);
-        if (stream->track == 0) {
+        track_obj = (*jni_env)->NewObject(jni_env,
+                                          stream->track_class,
+                                          constructor_method,
+                                          0, /* STREAM_VOICE_CALL */
+                                          param->clock_rate,
+                                          channelOutCfg,
+                                          sampleFormat,
+                                          inputBuffSizePlay,
+                                          1 /* MODE_STREAM */);
+        if (track_obj == 0) {
             PJ_LOG(3, (THIS_FILE, "Unable to create audio track object."));
             status = PJMEDIA_EAUD_INIT;
@@ -802,6 +810,13 @@
         }
         
-        stream->track = (*jni_env)->NewGlobalRef(jni_env, stream->track);
+        stream->track = (*jni_env)->NewGlobalRef(jni_env, track_obj);
         if (stream->track == 0) {
+            jmethodID release_method=0;
+        	
+            release_method = (*jni_env)->GetMethodID(jni_env, 
+                                                     stream->track_class,
+                                                     "release", "()V");
+            (*jni_env)->CallVoidMethod(jni_env, track_obj, release_method);
+            
             PJ_LOG(3, (THIS_FILE, "Unable to create audio track's global ref"));
             status = PJMEDIA_EAUD_INIT;
@@ -973,5 +988,5 @@
     
     PJ_LOG(4,(THIS_FILE, "Destroying Android JNI stream..."));
-    
+
     stream->quit_flag = PJ_TRUE;
     
@@ -980,5 +995,5 @@
     
     attached = attach_jvm(&jni_env);
-    
+
     if (stream->record){
         if (stream->rec_thread) {
@@ -993,14 +1008,18 @@
             stream->rec_sem = NULL;
         }
-        
-        release_method = (*jni_env)->GetMethodID(jni_env, stream->record_class,
-                                                 "release", "()V");
-        (*jni_env)->CallVoidMethod(jni_env, stream->record, release_method);
-        
+        if (stream->record_class) {
+            release_method = (*jni_env)->GetMethodID(jni_env, 
+                                                     stream->record_class,
+                                                     "release", "()V");
+            (*jni_env)->CallVoidMethod(jni_env, stream->record,
+                                       release_method);
+        }
         (*jni_env)->DeleteGlobalRef(jni_env, stream->record);
-        (*jni_env)->DeleteGlobalRef(jni_env, stream->record_class);
         stream->record = NULL;
-        stream->record_class = NULL;
         PJ_LOG(4, (THIS_FILE, "Audio record released"));
+    }
+    if (stream->record_class) {
+    	(*jni_env)->DeleteGlobalRef(jni_env, stream->record_class);
+    	stream->record_class = NULL;
     }
     
@@ -1017,14 +1036,18 @@
             stream->play_sem = NULL;
         }
-        
-        release_method = (*jni_env)->GetMethodID(jni_env, stream->track_class,
-                                                 "release", "()V");
-        (*jni_env)->CallVoidMethod(jni_env, stream->track, release_method);
-        
+        if (stream->track_class) {
+            release_method = (*jni_env)->GetMethodID(jni_env, 
+                                                     stream->track_class,
+                                                     "release", "()V");
+            (*jni_env)->CallVoidMethod(jni_env, stream->track, 
+                                       release_method);
+        }
         (*jni_env)->DeleteGlobalRef(jni_env, stream->track);
-        (*jni_env)->DeleteGlobalRef(jni_env, stream->track_class);
-        stream->track = NULL;
-        stream->track_class = NULL;
-        PJ_LOG(3, (THIS_FILE, "Audio track released"));
+        stream->track = NULL;        
+        PJ_LOG(4, (THIS_FILE, "Audio track released"));
+    }
+    if (stream->track_class) {
+    	(*jni_env)->DeleteGlobalRef(jni_env, stream->track_class);
+    	stream->track_class = NULL;
     }
 
