I have a function to concatenate two LPCWSTRs together by converting them to wstrings, adding them, converting it back, and then returning that value (taken from: How to concatenate a LPCWSTR?)
LPCWSTR addLPCWSTRs(LPCWSTR lpcwstr1, LPCWSTR lpcwstr2) {
//Add the strings together
std::wstring wstringCombined = std::wstring(lpcwstr1) + std::wstring(lpcwstr2);
//Convert from wstring back to LPCWSTR
LPCWSTR lpcwstrCombined = wstringCombined.c_str();
return lpcwstrCombined;
}
LPCWSTR BaseURL = L"https://serpapi.com/search.json?tbm=isch?q=";
LPCWSTR imageQuery = L"baby+animals";
LPCWSTR URL = addLPCWSTRs(BaseURL, imageQuery);
Before the return statement, the lpcwstrCombined value is correct, when I break before the return statement the debugger shows the value also to be correct.
The correct value should be:
When I break on the ending curly brace, the value that lpcwstr turns into a bunch of squares before 1-5 random symbols from other languages, and it's always changing.
Examples:
And this is without changing any code, simply resetting the debugger and running again. I have done hours of research on this and so far haven't found anything. A somewhat similar issue with arrays said to use pointers instead of face values but that didn't make a difference. Why does the variable change value outside of the function as soon as it is returned??
Edit: After reading the comments I changed it to:
std::wstring addLPCWSTRs(LPCWSTR lpcwstr1, LPCWSTR lpcwstr2) {
//Add the strings together
std::wstring wstringCombined = std::wstring(lpcwstr1) + std::wstring(lpcwstr2);
//Convert from wstring back to LPCWSTR
return wstringCombined;
}
LPCWSTR BaseURL = L"https://serpapi.com/search.json?tbm=isch?q=";
LPCWSTR imageQuery = L"baby+animals";
LPCWSTR URL = addLPCWSTRs(BaseURL, imageQuery).c_str();
And the same issue still happens!




