Question

I'm trying to add two LPCWSTR Variables as in

Shader = L"shader.fx"
Path = L"Source/Shaders/"
return Path + Shader

I've tried a thousand different ways, but my latest has been this

LPCWSTR ShaderFile = GetShader(L"shader.fx");

....

LPCWSTR GetShader(std::wstring _Shader)
{
    std::wstring ShaderPath = static_cast<std::wstring>(SHADER_DIRECTORY) + _Shader;
    LPCWSTR Return = ShaderPath.c_str();
    return Return;
}

Now when I put a break point on the return, the value seems fine, return = Source/Shaders/shader.fx as expected. But when I F10 back into my object, the ShaderFile variable turns out to be something completely random, a bunch of what seems like arabic symbols.

Could anyone point me in the right direction of what to do? As I said, the function seems to work fine, just when i F10 through the breakpoint back into my project the variable equals something completely different

Was it helpful?

Solution

What's happening is that you're returning an address to data that's being invalidated by the return, so everything will seem fine before the function returns, but immediately after the result, it's all (at least potentially) garbage.

If at all possible, just return the std::wstring, and somewhere in the calling code call its c_str() member function when you really need it in the form of a raw buffer.

If you can't do that, and simply must return the result as a raw LPCWSTR, then you'll probably have to allocate the space dynamically:

LPCWSTR *ret = new char [ShaderPath.size()];
strcpy(ret, ShaderPath.c_str());
return ret;

Then, the calling code will need to delete [] the memory when it's no longer needed.

You really want to avoid the latter, and just return an std::wstring though. It's much simpler and cleaner, and will save the nearly inevitable problems with either deleting the buffer before you're finished using it, or else forgetting to delete it when you are done using it (still serious problems in C, but essentially unheard of in decently written C++).

OTHER TIPS

The wstring.c_str() returns the internal pointer of the string.

In your case the local variable is destroyed when you exit the function and hence the pointer returned is deallocated and you get unexpected result.

Possible solution would be to copy the string using the method wcscpy()

The problem is that the c_str() method is returning a pointer into the local variable ShaderPath's memory. When the function exits, ShaderPath is destroyed, along with the data pointed to by your LPCWSTR.

Why don't you just store the variable as a wstring, and whenever you need the LPCWSTR you can call c_str()?

std::wstring GetShader(std::wstring _Shader)
{
    return static_cast<std::wstring>(SHADER_DIRECTORY) + _Shader;
}

Assuming you had a function Foo(LPCWSTR path), you would use it like:

Foo(GetShader(L"shader.fx").c_str());

or

std::wstring ShaderFile = GetShader(L"shader.fx");
Foo(ShaderFile.c_str());
Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top