Memory leak problem(s)

I'm programming a simple application and currently Im trying to get rid of all memory leaks. There are some that I cannot figure out. For example consider the code below:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
wstring getComputerName()
{
	TCHAR nameBuf[MAX_COMPUTERNAME_LENGTH + 2];
	DWORD nameBufSize;

	nameBufSize = sizeof nameBuf - 1;
	if (GetComputerName(nameBuf, &nameBufSize) == TRUE) 
	{
		wstring s(nameBuf);
		return s;
                //Tried "return nameBuf" but not helping.
	}
	else
	{
		return nullptr;
	}
}


Then all I do is create a wstring variable called "pcName" and execute this code:

 
pcName = getComputerName();


Im using Visual Leak Detector and it tells me that the leak is in getComputerName() (the returning wstring) What should I do to avoid this memory leak?

Edit:
Another memory leak reported:
 
clearPathString = (string)"http://somewebsiteurl" + std::wstring_convert<std::codecvt_utf8<wchar_t>>().to_bytes(pcName) + (string)"script.php";

How the hell is this a memory leak?
Last edited on
The wstring does not leak. But you must not return nullptr for a [w]string. Even though I don't think that line 15 will ever be executed.

This code snippet is certainly not the source of the leak. Most likely it is falsely reported due to some undefined behavior somewhere else in your code.
Okay thanks, Ill keep checking, at least i know this code is not a problem.
Topic archived. No new replies allowed.