I am trying to generate a JSON string from my input arrays . I have used "new" inorder to allocate the memory for this JSON. But I am not sure where to deallocate this memory or if there is a better way to write this function .
wchar_t* SetExpectedTabsData(_In_ PCWSTR tabUrls[], _In_ PCWSTR tabTitles[], _In_ UINT tabsCount)
{
wchar_t* tabsDataJSON = new wchar_t[JSONTABDATASIZE], jsonPerTab[256];
StringCchPrintf(tabsDataJSON, JSONTABDATASIZE, L"\"tabs\":[");
bool isActiveTab = true;
for (int i = 1; i <= tabsCount; ++i)
{
StringCchPrintf(jsonPerTab, ARRAYSIZE(jsonPerTab), L"{\"id\":%i,\"index\":%i,\"windowId\":1,\"active\":%s,\"status\":\"complete\",\"title\":\"%s\",\"url\":\"%s\"}", i, (i - 1), isActiveTab ? L"true" : L"false", tabTitles[i - 1], tabUrls[i - 1]);
StringCchCat(tabsDataJSON, JSONTABDATASIZE, jsonPerTab);
isActiveTab = false;
if (i != tabsCount)
{
StringCchCat(tabsDataJSON, JSONTABDATASIZE, L",");
}
}
StringCchCat(tabsDataJSON, JSONTABDATASIZE, L"],");
return tabsDataJSON;
}
Please suggest.