fix codes for abnormal result of GetLastResult()
[framework/osp/social.git] / src / FScl_AddressbookManagerImpl.cpp
index 1d77e4b..44e63c6 100644 (file)
@@ -200,6 +200,8 @@ _AddressbookManagerImpl::CreateAddressbookN(AccountId accountId, const String& n
        SysTryReturn(NID_SCL, accountId >= 0, null, E_INVALID_ARG, "[%s] Invalid argument is used. account id is invalid.", GetErrorMessage(E_INVALID_ARG));
        SysTryReturn(NID_SCL, !name.IsEmpty(), null, E_INVALID_ARG, "[%s] Invalid argument is used. The name is empty.", GetErrorMessage(E_INVALID_ARG));
 
+       ClearLastResult();
+
        result r = E_SUCCESS;
        int ret = CONTACTS_ERROR_NONE;
        int recordId = 0;
@@ -271,6 +273,8 @@ _AddressbookManagerImpl::GetAddressbooksByAccountN(AccountId accountId) const
        SysTryReturn(NID_SCL, accountId >= 0, null, E_INVALID_ARG, "[%s] Invalid argument is used. accountId = %d.", GetErrorMessage(E_INVALID_ARG), accountId);
        SysTryReturn(NID_SCL, _ContactDbConnector::EnsureDbConnection() == E_SUCCESS, null, GetLastResult(), "[%s] Propagating.", GetErrorMessage(GetLastResult()));
 
+       ClearLastResult();
+
        __Filter<__ContactsAddressbook> filter;
        filter.Construct();
        filter.AddInt(_contacts_address_book.account_id, CONTACTS_MATCH_EQUAL, accountId);
@@ -290,6 +294,8 @@ _AddressbookManagerImpl::GetAllAddressbooksN(void) const
 {
        SysTryReturn(NID_SCL, _ContactDbConnector::EnsureDbConnection() == E_SUCCESS, null, GetLastResult(), "[%s] Propagating.", GetErrorMessage(GetLastResult()));
 
+       ClearLastResult();
+
        __Query<__ContactsAddressbook> query;
        query.Construct();
        query.SetSort(_contacts_address_book.name, true);
@@ -306,6 +312,8 @@ _AddressbookManagerImpl::GetAddressbookN(AddressbookId addressbookId) const
        SysTryReturn(NID_SCL, addressbookId >= 0, null, E_INVALID_ARG, "[%s] Invalid argument is used. addressbookId.", GetErrorMessage(E_INVALID_ARG), addressbookId);
        SysTryReturn(NID_SCL, _ContactDbConnector::EnsureDbConnection() == E_SUCCESS, null, GetLastResult(), "[%s] Propagating.", GetErrorMessage(GetLastResult()));
 
+       ClearLastResult();
+
        result r = E_SUCCESS;
        int ret = CONTACTS_ERROR_NONE;
        contacts_record_h addressbookHandle = null;
@@ -318,7 +326,6 @@ _AddressbookManagerImpl::GetAddressbookN(AddressbookId addressbookId) const
        r = pAddressbook->Construct();
        SysTryReturn(NID_SCL, !IsFailed(r), null, E_OUT_OF_MEMORY, "[%s] Memory allocation failed.", GetErrorMessage(E_OUT_OF_MEMORY));
 
-
        ret = contacts_db_get_record(_contacts_address_book._uri, addressbookId, &addressbookHandle);
        SysTryReturn(NID_SCL, ret != CONTACTS_ERROR_OUT_OF_MEMORY, null, E_OUT_OF_MEMORY, "[%s] Memory allocation failed.", GetErrorMessage(E_OUT_OF_MEMORY));
        SysTryReturn(NID_SCL, ret != CONTACTS_ERROR_NO_DATA, null, E_OBJ_NOT_FOUND, "[%s] The addressbook %d is not found.", GetErrorMessage(E_OBJ_NOT_FOUND), addressbookId);
@@ -350,7 +357,6 @@ _AddressbookManagerImpl::AddContact(Contact& contact, AddressbookId addressbookI
        SysTryReturn(NID_SCL, !_ContactImpl::GetInstance(contact)->IsEmpty(), E_INVALID_ARG, E_INVALID_ARG, "[%s] Invalid argument is used. The specified contact does not have any property.", GetErrorMessage(E_INVALID_ARG));
        SysTryReturn(NID_SCL, _ContactDbConnector::EnsureDbConnection() == E_SUCCESS, GetLastResult(), GetLastResult(), "[%s] Propagating.", GetErrorMessage(GetLastResult()));
 
-
        int recordId = 0;
        contacts_record_h recordHandle = null;
 
@@ -647,6 +653,7 @@ IList*
 _AddressbookManagerImpl::GetCategoriesByContactN(RecordId contactId) const
 {
        SysTryReturn(NID_SCL, _ContactDbConnector::EnsureDbConnection() == E_SUCCESS, null, GetLastResult(), "[%s] Propagating.", GetErrorMessage(GetLastResult()));
+       SysTryReturn(NID_SCL, contactId != INVALID_RECORD_ID, null, E_INVALID_ARG, "[%s] Invalid argument is used. The specified contact is invalid.", GetErrorMessage(E_INVALID_ARG));
 
        ClearLastResult();
 
@@ -881,9 +888,10 @@ Contact*
 _AddressbookManagerImpl::GetContactN(RecordId contactId) const
 {
        SysTryReturn(NID_SCL, _ContactDbConnector::EnsureDbConnection() == E_SUCCESS, null, GetLastResult(), "[%s] Propagating.", GetErrorMessage(GetLastResult()));
-
        SysTryReturn(NID_SCL, contactId != INVALID_RECORD_ID, null, E_INVALID_ARG, "[%s] Invalid argument is used. contactId = %d.", GetErrorMessage(E_INVALID_ARG), contactId);
 
+       ClearLastResult();
+
        int intValue = 0;
        contacts_record_h contactHandle = null;
 
@@ -909,11 +917,12 @@ Person*
 _AddressbookManagerImpl::GetPersonN(PersonId personId) const
 {
        SysTryReturn(NID_SCL, _ContactDbConnector::EnsureDbConnection() == E_SUCCESS, null, GetLastResult(), "[%s] Propagating.", GetErrorMessage(GetLastResult()));
-
        SysTryReturn(NID_SCL, personId >= 0, null, E_INVALID_ARG, "[%s] Invalid argument is used. personId = %d.", GetErrorMessage(E_INVALID_ARG), personId);
 
        contacts_record_h recordHandle = null;
 
+       ClearLastResult();
+
        int intValue = 0;
        int ret = contacts_db_get_record(_contacts_person._uri, personId, &recordHandle);
        SysTryReturn(NID_SCL, ret != CONTACTS_ERROR_OUT_OF_MEMORY, null, E_OUT_OF_MEMORY, "[%s] Memory allocation failed.", GetErrorMessage(E_OUT_OF_MEMORY));
@@ -937,7 +946,6 @@ _AddressbookManagerImpl::GetCategoryN(RecordId categoryId) const
        SysTryReturn(NID_SCL, categoryId != INVALID_RECORD_ID, null, E_INVALID_ARG, "[E_INVALID_ARG] Invalid argument is used. categoryId = %d.", categoryId);
        SysTryReturn(NID_SCL, _ContactDbConnector::EnsureDbConnection() == E_SUCCESS, null, GetLastResult(), "[%s] Propagating.", GetErrorMessage(GetLastResult()));
 
-
        contacts_record_h recordHandle = null;
 
        ClearLastResult();
@@ -994,6 +1002,7 @@ _AddressbookManagerImpl::GetChangedContactsAfterN(int version, int& latestVersio
        SysTryReturn(NID_SCL, version >= 0, null, E_INVALID_ARG, "[%s] Invalid arguent is used. version %d must be greater that or equal 0.", GetErrorMessage(E_INVALID_ARG), version);
        SysTryReturn(NID_SCL, _ContactDbConnector::EnsureDbConnection() == E_SUCCESS, null, GetLastResult(), "[%s] Propagating.", GetErrorMessage(GetLastResult()));
 
+       ClearLastResult();
 
        IList* pChangedContacts = _AddressbookUtil::SearchWithVersionN<__ContactsContactUpdatedInfo, ContactChangeInfo>(-1, version, latestVersion);
        SysTryReturn(NID_SCL, pChangedContacts != null, null, GetLastResult(), "[%s] Propagating.", GetErrorMessage(GetLastResult()));
@@ -1251,6 +1260,9 @@ IList*
 _AddressbookManagerImpl::SearchPersonsN(const Tizen::Base::String& keyword) const
 {
        SysTryReturn(NID_SCL, _ContactDbConnector::EnsureDbConnection() == E_SUCCESS, null, GetLastResult(), "[%s] Propagating.", GetErrorMessage(GetLastResult()));
+       SysTryReturn(NID_SCL, !keyword.IsEmpty(), null, E_INVALID_ARG, "Invalid argument is used. keyword is empty string.", GetErrorMessage(E_INVALID_ARG));
+
+       ClearLastResult();
 
        contacts_record_h currentRecord = null;
        std::unique_ptr<Person> pPerson(null);
@@ -1341,6 +1353,8 @@ _AddressbookManagerImpl::SearchN(const AddressbookFilter& filter, unsigned long
 {
        SysTryReturn(NID_SCL, _ContactDbConnector::EnsureDbConnection() == E_SUCCESS, null, GetLastResult(), "[%s] Propagating.", GetErrorMessage(GetLastResult()));
 
+       ClearLastResult();
+
        IList* pList = null;
        bool ascending = false;
        unsigned int viewSortPropertyId = 0;
@@ -1486,6 +1500,8 @@ _AddressbookManagerImpl::GetMatchedItemCount(const AddressbookFilter& filter)
 {
        SysTryReturn(NID_SCL, _ContactDbConnector::EnsureDbConnection() == E_SUCCESS, -1, GetLastResult(), "[%s] Propagating.", GetErrorMessage(GetLastResult()));
 
+       ClearLastResult();
+
        int count = 0;
        AddressbookFilterType type = _AddressbookFilterImpl::GetInstance(filter)->GetType();
        contacts_filter_h filterHandle = _AddressbookFilterImpl::GetInstance(filter)->GetFilterHandle();
@@ -1743,6 +1759,8 @@ _AddressbookManagerImpl::ExportContactToVcardStreamN(const Contact& contact)
 {
        SysTryReturn(NID_SCL, _ContactDbConnector::EnsureDbConnection() == E_SUCCESS, null, GetLastResult(), "[%s] Propagating.", GetErrorMessage(GetLastResult()));
 
+       ClearLastResult();
+
        contacts_record_h recordHandle = _ContactImpl::GetInstance(contact)->GetContactRecordHandle();
 
        char* pVcardStream = null;
@@ -1751,18 +1769,28 @@ _AddressbookManagerImpl::ExportContactToVcardStreamN(const Contact& contact)
        ret = contacts_vcard_make_from_contact(recordHandle, &pVcardStream);
        SysTryReturn(NID_SCL, ret != CONTACTS_ERROR_INVALID_PARAMETER, null, E_INVALID_ARG, "[%s] Invalid argument is used.", GetErrorMessage(E_INVALID_ARG));
        SysTryReturn(NID_SCL, ret != CONTACTS_ERROR_SYSTEM, null, E_SYSTEM, "[%s] A system error has been occurred.", GetErrorMessage(E_SYSTEM));
-
        SysTryReturn(NID_SCL, ret == CONTACTS_ERROR_NONE, null, E_OUT_OF_MEMORY, "[%s] Memory allocation failed.", GetErrorMessage(E_OUT_OF_MEMORY));
 
-       std::unique_ptr<char> pChar(pVcardStream);
-
        std::unique_ptr<ByteBuffer> pByteBuffer(new (std::nothrow) ByteBuffer);
-       SysTryReturn(NID_SCL, pByteBuffer != null, null, E_OUT_OF_MEMORY, "[%s] Memory allocation failed.", GetErrorMessage(E_OUT_OF_MEMORY));
+       if (pByteBuffer == null)
+       {
+               free(pVcardStream);
+               SysLogException(NID_SCL, E_OUT_OF_MEMORY, "[%s] Memory allocation failed.", GetErrorMessage(E_OUT_OF_MEMORY));
 
-       result r = pByteBuffer->Construct(strlen(pChar.get()));
-       SysTryReturn(NID_SCL, !IsFailed(r), null, E_OUT_OF_MEMORY, "[%s] Memory allocation failed.", GetErrorMessage(E_OUT_OF_MEMORY));
+               return null;
+       }
+
+       result r = pByteBuffer->Construct(strlen(pVcardStream));
+       if (IsFailed(r))
+       {
+               free(pVcardStream);
+               SysLogException(NID_SCL, E_OUT_OF_MEMORY, "[%s] Memory allocation failed.", GetErrorMessage(E_OUT_OF_MEMORY));
+
+               return null;
+       }
 
-       r = pByteBuffer->SetArray(reinterpret_cast<byte*>(pChar.get()), 0, strlen(pChar.get()));
+       r = pByteBuffer->SetArray(reinterpret_cast<byte*>(pVcardStream), 0, strlen(pVcardStream));
+       free(pVcardStream);
        SysTryReturn(NID_SCL, r != E_OUT_OF_MEMORY, null, E_OUT_OF_MEMORY, "[%s] Memory allocation failed.", GetErrorMessage(E_OUT_OF_MEMORY));
        SysTryReturn(NID_SCL, r == E_SUCCESS, null, E_SYSTEM, "[%s] A system error has been occurred.", GetErrorMessage(E_SYSTEM));
 
@@ -1774,6 +1802,8 @@ _AddressbookManagerImpl::ExportContactsToVcardStreamN(const Tizen::Base::Collect
 {
        SysTryReturn(NID_SCL, _ContactDbConnector::EnsureDbConnection() == E_SUCCESS, null, GetLastResult(), "[%s] Propagating.", GetErrorMessage(GetLastResult()));
 
+       ClearLastResult();
+
        char* pVcardStream = null;
        int ret = CONTACTS_ERROR_NONE;
        Contact* pContact = null;
@@ -1781,7 +1811,6 @@ _AddressbookManagerImpl::ExportContactsToVcardStreamN(const Tizen::Base::Collect
        int capacity = 0;
 
        contacts_record_h recordHandle = null;
-       std::unique_ptr<char> pChar(null);
 
        std::unique_ptr<IEnumerator> pEnum(contactList.GetEnumeratorN());
        SysTryReturn(NID_SCL, pEnum != null, null, E_OUT_OF_MEMORY, "[%s] Memory allocation failed.", GetErrorMessage(E_OUT_OF_MEMORY));
@@ -1806,16 +1835,21 @@ _AddressbookManagerImpl::ExportContactsToVcardStreamN(const Tizen::Base::Collect
                ret = contacts_vcard_make_from_contact(recordHandle, &pVcardStream);
                SysTryReturn(NID_SCL, ret != CONTACTS_ERROR_INVALID_PARAMETER, null, E_INVALID_ARG, "[%s] Invalid argument is used.", GetErrorMessage(E_INVALID_ARG));
                SysTryReturn(NID_SCL, ret != CONTACTS_ERROR_SYSTEM, null, E_SYSTEM, "[%s] A system error has been occurred.", GetErrorMessage(E_SYSTEM));
-
                SysTryReturn(NID_SCL, ret == CONTACTS_ERROR_NONE, null, E_OUT_OF_MEMORY, "[%s] Memory allocation failed.", GetErrorMessage(E_OUT_OF_MEMORY));
 
-               pChar.reset(pVcardStream);
-
-               capacity += strlen(pChar.get());
+               capacity += strlen(pVcardStream);
                r = pByteBuffer->ExpandCapacity(capacity);
-               SysTryReturn(NID_SCL, !IsFailed(r), null, E_OUT_OF_MEMORY, "[%s] Memory allocation failed.", GetErrorMessage(E_OUT_OF_MEMORY));
+               if (IsFailed(r))
+               {
+                       free(pVcardStream);
+                       SysLogException(NID_SCL, E_OUT_OF_MEMORY, "[%s] Memory allocation failed.", GetErrorMessage(E_OUT_OF_MEMORY));
+
+                       return null;
+               }
 
-               r = pByteBuffer->SetArray(reinterpret_cast<byte*>(pChar.get()), 0, strlen(pChar.get()));
+               r = pByteBuffer->SetArray(reinterpret_cast<byte*>(pVcardStream), 0, strlen(pVcardStream));
+               free(pVcardStream);
+               pVcardStream = null;
                SysTryReturn(NID_SCL, r == E_SUCCESS, null, E_SYSTEM, "[%s] A system error has been occurred.: capacity(%d), size(%d)", GetErrorMessage(E_SYSTEM));
        }
 
@@ -1827,6 +1861,8 @@ _AddressbookManagerImpl::ExportPersonToVcardStreamN(const Person& person)
 {
        SysTryReturn(NID_SCL, _ContactDbConnector::EnsureDbConnection() == E_SUCCESS, null, GetLastResult(), "[%s] Propagating.", GetErrorMessage(GetLastResult()));
 
+       ClearLastResult();
+
        int ret = CONTACTS_ERROR_NONE;
 
        contacts_record_h personRecordHandle = null;
@@ -1845,15 +1881,26 @@ _AddressbookManagerImpl::ExportPersonToVcardStreamN(const Person& person)
        SysTryReturn(NID_SCL, ret != CONTACTS_ERROR_INVALID_PARAMETER, null, E_INVALID_ARG, "[%s] Invalid argument is used.", GetErrorMessage(E_INVALID_ARG));
        SysTryReturn(NID_SCL, ret == CONTACTS_ERROR_NONE, null, E_SYSTEM, "[%s] A system error has been occurred.", GetErrorMessage(E_SYSTEM));
 
-       std::unique_ptr<char> pChar(pVcardStream);
-
        std::unique_ptr<ByteBuffer> pByteBuffer(new (std::nothrow) ByteBuffer);
-       SysTryReturn(NID_SCL, pByteBuffer != null, null, E_OUT_OF_MEMORY, "[%s] Memory allocation failed.", GetErrorMessage(E_OUT_OF_MEMORY));
+       if (pByteBuffer == null)
+       {
+               free(pVcardStream);
+               SysLogException(NID_SCL, E_OUT_OF_MEMORY, "[%s] Memory allocation failed.", GetErrorMessage(E_OUT_OF_MEMORY));
 
-       result r = pByteBuffer->Construct(strlen(pChar.get()));
-       SysTryReturn(NID_SCL, !IsFailed(r), null, E_OUT_OF_MEMORY, "[%s] Memory allocation failed.", GetErrorMessage(E_OUT_OF_MEMORY));
+               return null;
+       }
+
+       result r = pByteBuffer->Construct(strlen(pVcardStream));
+       if (IsFailed(r))
+       {
+               free(pVcardStream);
+               SysLogException(NID_SCL, E_OUT_OF_MEMORY, "[%s] Memory allocation failed.", GetErrorMessage(E_OUT_OF_MEMORY));
+
+               return null;
+       }
 
-       r = pByteBuffer->SetArray(reinterpret_cast<byte*>(pChar.get()), 0, strlen(pChar.get()));
+       r = pByteBuffer->SetArray(reinterpret_cast<byte*>(pVcardStream), 0, strlen(pVcardStream));
+       free(pVcardStream);
        SysTryReturn(NID_SCL, r == E_SUCCESS, null, E_SYSTEM, "[%s] A system error has been occurred.", GetErrorMessage(E_SYSTEM));
 
        return pByteBuffer.release();
@@ -1864,6 +1911,8 @@ _AddressbookManagerImpl::ExportPersonsToVcardStreamN(const Tizen::Base::Collecti
 {
        SysTryReturn(NID_SCL, _ContactDbConnector::EnsureDbConnection() == E_SUCCESS, null, GetLastResult(), "[%s] Propagating.", GetErrorMessage(GetLastResult()));
 
+       ClearLastResult();
+
        int ret = CONTACTS_ERROR_NONE;
        Person* pPerson = null;
        char* pVcardStream = null;
@@ -1872,7 +1921,6 @@ _AddressbookManagerImpl::ExportPersonsToVcardStreamN(const Tizen::Base::Collecti
 
        contacts_record_h personRecordHandle = null;
        __ContactsRecordHandle recordHandle(null);
-       std::unique_ptr<char> pChar(null);
 
        std::unique_ptr<IEnumerator> pEnum(personList.GetEnumeratorN());
        SysTryReturn(NID_SCL, pEnum != null, null, E_OUT_OF_MEMORY, "[%s] Memory allocation failed.", GetErrorMessage(E_OUT_OF_MEMORY));
@@ -1904,13 +1952,19 @@ _AddressbookManagerImpl::ExportPersonsToVcardStreamN(const Tizen::Base::Collecti
                SysTryReturn(NID_SCL, ret != CONTACTS_ERROR_INVALID_PARAMETER, null, E_INVALID_ARG, "[%s] Invalid argument is used.", GetErrorMessage(E_INVALID_ARG));
                SysTryReturn(NID_SCL, ret == CONTACTS_ERROR_NONE, null, E_SYSTEM, "[%s] A system error has been occurred.", GetErrorMessage(E_SYSTEM));
 
-               pChar.reset(pVcardStream);
-
-               capacity += strlen(pChar.get());
+               capacity += strlen(pVcardStream);
                r = pByteBuffer->ExpandCapacity(capacity);
-               SysTryReturn(NID_SCL, !IsFailed(r), null, E_OUT_OF_MEMORY, "[%s] Memory allocation failed.", GetErrorMessage(E_OUT_OF_MEMORY));
+               if (IsFailed(r))
+               {
+                       free(pVcardStream);
+                       SysLogException(NID_SCL, E_OUT_OF_MEMORY, "[%s] Memory allocation failed.", GetErrorMessage(E_OUT_OF_MEMORY));
+
+                       return null;
+               }
 
-               r = pByteBuffer->SetArray(reinterpret_cast<byte*>(pChar.get()), 0, strlen(pChar.get()));
+               r = pByteBuffer->SetArray(reinterpret_cast<byte*>(pVcardStream), 0, strlen(pVcardStream));
+               free(pVcardStream);
+               pVcardStream = null;
                SysTryReturn(NID_SCL, r == E_SUCCESS, null, E_SYSTEM, "[%s] A system error has been occurred.", GetErrorMessage(E_SYSTEM));
        }
 
@@ -1922,6 +1976,8 @@ _AddressbookManagerImpl::ParseVcardStreamN(const Tizen::Base::ByteBuffer& vcardS
 {
        SysTryReturn(NID_SCL, _ContactDbConnector::EnsureDbConnection() == E_SUCCESS, null, GetLastResult(), "[%s] Propagating.", GetErrorMessage(GetLastResult()));
 
+       ClearLastResult();
+
        contacts_list_h listHandle = null;
        result r = E_SUCCESS;
 
@@ -1934,39 +1990,70 @@ _AddressbookManagerImpl::ParseVcardStreamN(const Tizen::Base::ByteBuffer& vcardS
        contacts_list_get_count(listHandle, &count);
 
        std::unique_ptr<ArrayList, AllElementsDeleter> pList(new (std::nothrow) ArrayList());
-       SysTryReturn(NID_SCL, pList != null, null, E_OUT_OF_MEMORY, "[%s] Memory allocation failed.", GetErrorMessage(E_OUT_OF_MEMORY));
+       if (pList == null)
+       {
+               contacts_list_destroy(listHandle, true);
+               SysLogException(NID_SCL, E_OUT_OF_MEMORY, "[%s] Memory allocation failed.", GetErrorMessage(E_OUT_OF_MEMORY));
+
+               return null;
+       }
 
        r = pList->Construct(count);
-       SysTryReturn(NID_SCL, !IsFailed(r), null, E_OUT_OF_MEMORY, "[%s] Memory allocation failed.", GetErrorMessage(E_OUT_OF_MEMORY));
+       if (IsFailed(r))
+       {
+               contacts_list_destroy(listHandle, true);
+               SysLogException(NID_SCL, E_OUT_OF_MEMORY, "[%s] Memory allocation failed.", GetErrorMessage(E_OUT_OF_MEMORY));
 
-       for (int i = 0; i < count; i++)
+               return null;
+       }
+
+       for (unsigned int i = 0; i < count; i++)
        {
                std::unique_ptr<Contact> pContact(new (std::nothrow) Contact());
-               SysTryReturn(NID_SCL, pContact != null, null, E_OUT_OF_MEMORY, "[%s] Memory allocation failed.", GetErrorMessage(E_OUT_OF_MEMORY));
+               if (pContact == null)
+               {
+                       contacts_list_destroy(listHandle, true);
+                       SysLogException(NID_SCL, E_OUT_OF_MEMORY, "[%s] Memory allocation failed.", GetErrorMessage(E_OUT_OF_MEMORY));
 
-               contacts_list_get_current_record_p(listHandle, &recordHandle);
+                       return null;
+               }
 
-               _ContactImpl::GetInstance(*pContact)->SetContactRecordHandle(recordHandle);
                r = pList->Add(pContact.get());
-               SysTryReturn(NID_SCL, !IsFailed(r), null, E_OUT_OF_MEMORY, "[%s] Memory allocation failed.", GetErrorMessage(E_OUT_OF_MEMORY));
+               if (IsFailed(r))
+               {
+                       contacts_list_destroy(listHandle, true);
+                       SysLogException(NID_SCL, E_OUT_OF_MEMORY, "[%s] Memory allocation failed.", GetErrorMessage(E_OUT_OF_MEMORY));
+
+                       return null;
+               }
 
                pContact.release();
+       }
+
+       std::unique_ptr<IEnumerator> pEnum(pList->GetEnumeratorN());
+
+       while (pEnum->MoveNext() == E_SUCCESS)
+       {
+               Contact* pContact = static_cast <Contact*> (pEnum->GetCurrent());
+
+               contacts_list_get_current_record_p(listHandle, &recordHandle);
+               _ContactImpl::GetInstance(*pContact)->SetContactRecordHandle(recordHandle);
 
                ret = contacts_list_next(listHandle);
-               if (ret != CONTACTS_ERROR_NONE)
-               {
-                       break;
-               }
        }
 
+       contacts_list_destroy(listHandle, false);
+
        return pList.release();
 }
 
-Tizen::Base::ByteBuffer*
+ByteBuffer*
 _AddressbookManagerImpl::ExportUserProfileToVcardStreamN(const UserProfile& userProfile)
 {
        SysTryReturn(NID_SCL, _ContactDbConnector::EnsureDbConnection() == E_SUCCESS, null, GetLastResult(), "[%s] Propagating.", GetErrorMessage(GetLastResult()));
 
+       ClearLastResult();
+
        char* pVcardStream = null;
        int ret = CONTACTS_ERROR_NONE;
        contacts_record_h recordHandle = null;
@@ -1978,34 +2065,46 @@ _AddressbookManagerImpl::ExportUserProfileToVcardStreamN(const UserProfile& user
        SysTryReturn(NID_SCL, ret != CONTACTS_ERROR_SYSTEM, null, E_SYSTEM, "[%s] A system error has been occurred.", GetErrorMessage(E_SYSTEM));
        SysTryReturn(NID_SCL, ret == CONTACTS_ERROR_NONE, null, E_OUT_OF_MEMORY, "[%s] Memory allocation failed.", GetErrorMessage(E_OUT_OF_MEMORY));
 
-       std::unique_ptr<char> pChar(pVcardStream);
-
        std::unique_ptr<ByteBuffer> pByteBuffer(new (std::nothrow) ByteBuffer);
+       if (pByteBuffer == null)
+       {
+               free(pVcardStream);
+               SysLogException(NID_SCL, E_OUT_OF_MEMORY, "[%s] Memory allocation failed.", GetErrorMessage(E_OUT_OF_MEMORY));
+
+               return null;
+       }
        SysTryReturn(NID_SCL, pByteBuffer != null, null, E_OUT_OF_MEMORY, "[%s] Memory allocation failed.", GetErrorMessage(E_OUT_OF_MEMORY));
 
-       result r = pByteBuffer->Construct(strlen(pChar.get()));
-       SysTryReturn(NID_SCL, !IsFailed(r), null, E_OUT_OF_MEMORY, "[%s] Memory allocation failed.", GetErrorMessage(E_OUT_OF_MEMORY));
+       result r = pByteBuffer->Construct(strlen(pVcardStream));
+       if (IsFailed(r))
+       {
+               free(pVcardStream);
+               SysLogException(NID_SCL, E_OUT_OF_MEMORY, "[%s] Memory allocation failed.", GetErrorMessage(E_OUT_OF_MEMORY));
+
+               return null;
+       }
 
-       r = pByteBuffer->SetArray(reinterpret_cast<byte*>(pChar.get()), 0, strlen(pChar.get()));
+       r = pByteBuffer->SetArray(reinterpret_cast<byte*>(pVcardStream), 0, strlen(pVcardStream));
+       free(pVcardStream);
        SysTryReturn(NID_SCL, r != E_OUT_OF_MEMORY, null, E_OUT_OF_MEMORY, "[%s] Memory allocation failed.", GetErrorMessage(E_OUT_OF_MEMORY));
        SysTryReturn(NID_SCL, r == E_SUCCESS, null, E_SYSTEM, "[%s] A system error has been occurred.", GetErrorMessage(E_SYSTEM));
 
        return pByteBuffer.release();
 }
 
-Tizen::Base::ByteBuffer*
+ByteBuffer*
 _AddressbookManagerImpl::ExportUserProfilesToVcardStreamN(const Tizen::Base::Collection::IList& userProfileList)
 {
        SysTryReturn(NID_SCL, _ContactDbConnector::EnsureDbConnection() == E_SUCCESS, null, GetLastResult(), "[%s] Propagating.", GetErrorMessage(GetLastResult()));
 
+       ClearLastResult();
+
        char* pVcardStream = null;
        int ret = CONTACTS_ERROR_NONE;
        UserProfile* pProfile = null;
        result r = E_SUCCESS;
        int capacity = 0;
 
-       std::unique_ptr<char> pChar(null);
-
        std::unique_ptr<IEnumerator> pEnum(userProfileList.GetEnumeratorN());
        SysTryReturn(NID_SCL, pEnum != null, null, E_OUT_OF_MEMORY, "[%s] Memory allocation failed.", GetErrorMessage(E_OUT_OF_MEMORY));
 
@@ -2033,13 +2132,18 @@ _AddressbookManagerImpl::ExportUserProfilesToVcardStreamN(const Tizen::Base::Col
                SysTryReturn(NID_SCL, ret != CONTACTS_ERROR_SYSTEM, null, E_SYSTEM, "[%s] A system error has been occurred.", GetErrorMessage(E_SYSTEM));
                SysTryReturn(NID_SCL, ret == CONTACTS_ERROR_NONE, null, E_OUT_OF_MEMORY, "[%s] Memory allocation failed.", GetErrorMessage(E_OUT_OF_MEMORY));
 
-               pChar.reset(pVcardStream);
-
-               capacity += strlen(pChar.get());
+               capacity += strlen(pVcardStream);
                r = pByteBuffer->ExpandCapacity(capacity);
-               SysTryReturn(NID_SCL, !IsFailed(r), null, E_OUT_OF_MEMORY, "[%s] Memory allocation failed.", GetErrorMessage(E_OUT_OF_MEMORY));
+               if (IsFailed(r))
+               {
+                       free(pVcardStream);
+                       SysLogException(NID_SCL, E_OUT_OF_MEMORY, "[%s] Memory allocation failed.", GetErrorMessage(E_OUT_OF_MEMORY));
 
-               r = pByteBuffer->SetArray(reinterpret_cast<byte*>(pChar.get()), 0, strlen(pChar.get()));
+                       return null;
+               }
+
+               r = pByteBuffer->SetArray(reinterpret_cast<byte*>(pVcardStream), 0, strlen(pVcardStream));
+               free(pVcardStream);
                SysTryReturn(NID_SCL, r == E_SUCCESS, null, E_SYSTEM, "[%s] A system error has been occurred.: capacity(%d), size(%d)", GetErrorMessage(E_SYSTEM));
        }
 
@@ -2119,7 +2223,7 @@ _AddressbookManagerImpl::ExportUserProfilesToVcard(const Tizen::Base::Collection
        return E_SUCCESS;
 }
 
-Tizen::Base::Collection::IList*
+IList*
 _AddressbookManagerImpl::GetAllUserProfilesN(void) const
 {
        SysTryReturn(NID_SCL, _ContactDbConnector::EnsureDbConnection() == E_SUCCESS, null, GetLastResult(), "[%s] Propagating.", GetErrorMessage(GetLastResult()));