Fix memory bug and prevent potential leak. 39/57939/1
authorjoonbum.ko <joonbum.ko@samsung.com>
Fri, 22 Jan 2016 03:49:43 +0000 (12:49 +0900)
committerjoonbum.ko <joonbum.ko@samsung.com>
Tue, 26 Jan 2016 07:17:50 +0000 (16:17 +0900)
Change-Id: Iadeb173177b09b21fb3674b1ed726e1da7b3d994

src/modules/fastpath/coregl_fastpath.c
src/modules/fastpath/coregl_fastpath_gl.c

index 0c61c31..85cc12a 100644 (file)
@@ -1107,23 +1107,35 @@ fastpath_ostate_create_object(GL_Object_State *ostate, GL_Object_Type type, GLui
 
        {
                GL_Object *newobj = (GL_Object *)calloc(1, sizeof(GL_Object));
-               AST(newobj != NULL);
+               if (newobj == NULL)
+                       goto finish;
                newobj->id = (int)type + newid;
                newobj->real_id = real_name;
                newobj->ref_count = 1;
-               ret = newobj->id;
+
 
                GL_Object_Hash *newobj_hash = (GL_Object_Hash *)calloc(1, sizeof(GL_Object_Hash));
-               AST(newobj_hash != NULL);
+               if (newobj_hash == NULL)
+               {
+                       free(newobj);
+                       goto finish;
+               }
                newobj_hash->item = newobj;
                newobj_hash->hash_key = newid;
                _add_hash(hash_base, newobj_hash);
 
                GL_Object_Hash *newobj_hash_real = (GL_Object_Hash *)calloc(1, sizeof(GL_Object_Hash));
-               AST(newobj_hash_real != NULL);
+               if (newobj_hash_real == NULL)
+               {
+                       free(newobj);
+                       free(newobj_hash);
+                       goto finish;
+               }
                newobj_hash_real->item = newobj;
                newobj_hash_real->hash_key = real_name;
                _add_hash(hash_base_real, newobj_hash_real);
+
+               ret = newobj->id;
        }
 
        _ostate_hash_check(hash_base);
@@ -1212,7 +1224,6 @@ finish:
        return ret;
 }
 
-
 /* Check if the context's state contains object of a given type */
 GLuint
 fastpath_ostate_has_object_type(GL_Object_State *ostate, GL_Object_Type type)
index 0fe6928..887dcec 100644 (file)
@@ -137,15 +137,21 @@ _create_program_object(GL_Object_State *ostate, int is_program, GLenum shader_ty
        if (real_obj != 0)
        {
                ret = fastpath_ostate_create_object(ostate, GL_OBJECT_TYPE_PROGRAM, real_obj);
-
                Program_object_attached_tag *poat = NULL;
-               poat = (Program_object_attached_tag *)calloc(1, sizeof(Program_object_attached_tag));
-               AST(poat != NULL);
 
-               poat->is_deleting = 0;
-               poat->shader_count = 0;
+               if (ret != _COREGL_INT_INIT_VALUE)
+                {
+                        poat = (Program_object_attached_tag *)calloc(1, sizeof(Program_object_attached_tag));
+                        if (poat == NULL)
+                        {
+                                AST(poat != NULL);
+                                fastpath_ostate_remove_object(ostate, GL_OBJECT_TYPE_PROGRAM, ret);
+                        }
+                        poat->is_deleting = 0;
+                        poat->shader_count = 0;
 
-               fastpath_ostate_set_object_tag(ostate, GL_OBJECT_TYPE_PROGRAM, ret, poat);
+                        fastpath_ostate_set_object_tag(ostate, GL_OBJECT_TYPE_PROGRAM, ret, poat);
+                }
        }
 
        return ret;
@@ -203,7 +209,11 @@ _detach_program_object(GL_Object_State *ostate, GLuint real_object, int is_progr
                        if (poat->is_deleting == 0)
                        {
                                poat->is_deleting = 1;
+                                /* Ref count increased when glCreateProgram/initial attach */
                                fastpath_ostate_remove_object(ostate, GL_OBJECT_TYPE_PROGRAM, object);
+                                /* Ref count increased when glCreateProgram/create object */
+                                /* So, we have to call the under function twice.*/
+                                fastpath_ostate_remove_object(ostate, GL_OBJECT_TYPE_PROGRAM, object);
                        }
                }
                else