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);

os = native_os; //here is the position where memory leaks!
free(ch);
}

can someone tell me why? and how to edit it?

Vivienne
 
I

Ian Collins

Vivienne said:
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 ){
What's that? It isn't C++.
os = native_os; //here is the position where memory leaks!

You don't appear to have included the declaration of native_os.
 
V

Vivienne

sorry, I made a mistake.
it should be
os = ch;
free(ch);

however it also report memory leak.
 
I

Ian Collins

Vivienne said:
sorry, I made a mistake.
it should be
os = ch;
free(ch);

however it also report memory leak.
Please retain enough context for your reply to make sense.

You haven't explained the bizarre syntax in the function parameters.

Why do you use malloc rather than new?

Please copy and paste the exact code that gives the problem.
 
O

Old Wolf

+ reports memory leaking in this function. But I don't know why?

Well your code uses lots of non-standard features so
it is hard to say anything with certainty. However...
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);

os = native_os; //here is the position where memory leaks!

(I guess you mean, os = ch)

In the standard version of wcstombs(), the output
is not null-terminated if the output buffer is entirely
filled with valid characters. Perhaps this happens in
your example, you might want to ensure 'ch'
is terminated before assigning it to 'os', or
you could pass a length to the std::string constructor.

If you're still stuck then post a complete program
that compiles and doesn't include any system-specific
stuff. If you cannot do this, then asking on a Microsoft
newsgroup would be the way to go.
 
R

Ron Natalie

Ian said:
You haven't explained the bizarre syntax in the function parameters.
It's a microsnotism for "managed" C++.
You need to take this to a .NET related group.
 

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,755
Messages
2,569,536
Members
45,014
Latest member
BiancaFix3

Latest Threads

Top