why memory leak here?

V

Vivienne

Below is a function that convert a System::String to basic string. VC+
+ reports memory leaking in this function. But I don't know why?

void SStringToBstring(System::String ^ s, std::string& os ){
using namespace Runtime::InteropServices;
pin_ptr<const wchar_t> wch = PtrToStringChars(s);

size_t convertedChars = 0;
size_t sizeInBytes = ((s->Length + 1) * 2);
errno_t err = 0;
char *ch = (char *)malloc(sizeInBytes);

err = wcstombs_s(&convertedChars,
ch, sizeInBytes,
wch, sizeInBytes);

std::string native_os(ch);
os = native_os;
free(ch);
// delete wch;
}
 
V

Vivienne

sorry, please skip this post! I wrongly tapped the enter key before I
finished my question.
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,744
Messages
2,569,484
Members
44,904
Latest member
HealthyVisionsCBDPrice

Latest Threads

Top