Does this function memory leak?

D.D.K-2637 966 Reputation points
2023-05-01T17:56:33.66+00:00

Hi,

I'm using the following function to get the filename (with extension).

However when putting it in a loop (in a thread) my program crashed.

How should I fix this code?

namespace fs = boost::filesystem;
std::wstring FileName(const std::wstring& filePath, bool withExtension=1)
{
	filesys::path pathObj(filePath);
	if (withExtension == false)
	{
		if (pathObj.has_stem())
		{
			return pathObj.stem().wstring();
		}
		return {};
	}
	else
	{
		return pathObj.filename().wstring(); <--crash
	}
}

=FileName(L"!Person-Haley::Bittet");

User's image

C++
C++
A high-level, general-purpose programming language, created as an extension of the C programming language, that has object-oriented, generic, and functional features in addition to facilities for low-level memory manipulation.
3,784 questions
{count} votes

Your answer

Answers can be marked as Accepted Answers by the question author, which helps users to know the answer solved the author's problem.