How are you determining that you have a memory leak?
As written, it seems well behaved.
Code:
#include <iostream>
#include <string>
#include <vector>
std::vector<std::string> splitStringToVectorArray(const std::string &inputString, const std::string &delimiter)
{
std::vector<std::string> vectorArray;
if (inputString.empty() || delimiter.empty())
{
vectorArray.push_back(inputString);
return vectorArray;
}
size_t initiation = 0;
size_t position;
while ((position = inputString.find(delimiter, initiation)) != std::string::npos)
{
std::string stringValue = inputString.substr(initiation, position - initiation);
if (!stringValue.empty())
{
vectorArray.push_back(stringValue);
}
initiation = position + delimiter.length();
}
if (initiation < inputString.length())
{
vectorArray.push_back(inputString.substr(initiation));
}
return vectorArray;
}
int main()
{
std::vector<std::string> parts = splitStringToVectorArray("String1;String2;String3", ";");
std::cout << parts[0] << std::endl;
}
$ g++ -g baz.cpp
$ valgrind ./a.out
==95652== Memcheck, a memory error detector
==95652== Copyright (C) 2002-2017, and GNU GPL'd, by Julian Seward et al.
==95652== Using Valgrind-3.15.0 and LibVEX; rerun with -h for copyright info
==95652== Command: ./a.out
==95652==
String1
==95652==
==95652== HEAP SUMMARY:
==95652== in use at exit: 0 bytes in 0 blocks
==95652== total heap usage: 6 allocs, 6 frees, 73,976 bytes allocated
==95652==
==95652== All heap blocks were freed -- no leaks are possible
==95652==
==95652== For lists of detected and suppressed errors, rerun with: -s
==95652== ERROR SUMMARY: 0 errors from 0 contexts (suppressed: 0 from 0)