473,385 Members | 2,013 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,385 software developers and data experts.

why memory leak here?

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_twch = 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

Sep 18 '07 #1
5 2342
Vivienne wrote:
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.

--
Ian Collins.
Sep 18 '07 #2
sorry, I made a mistake.
it should be
os = ch;
free(ch);

however it also report memory leak.

Sep 18 '07 #3
Vivienne wrote:
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.

--
Ian Collins.
Sep 18 '07 #4
On Sep 18, 3:47 pm, Vivienne <zhoudan.b...@gmail.comwrote:
+ 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_twch = 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.

Sep 18 '07 #5
Ian Collins wrote:
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.
Sep 18 '07 #6

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

2
by: Elbert Lev | last post by:
#When I'm running this script on my windows NT4.0 box, #every time dialog box is reopened there is memory growth 384K. #Bellow is the text I sent to Stephen Ferg (author of easygui) # I have...
4
by: Felix Sima | last post by:
Hi. Does the following line generate a memory leak or doesn't ? *** foo( new CTest( ) ); *** //See code below. Watching the output of the program seems that only the constructor of the...
2
by: Andreas Lennartz | last post by:
Hello, I have some problems with the class WebRequest and its childs. I am developing a bot that visits a lot of different urls and returns the content. But every time an URL-Request is finished...
30
by: MAG1301 | last post by:
I've detected memory leaks in our huge .NET 1.1 C# application but couldn't localize them directly. So I've reduced the code to the following console application: using System; using System.IO;...
0
by: aton1 | last post by:
Hi, i have some questions regarding the use of datatype string in C++. I use g++ compiler and also use .ccmalloc to test for the memory leak. Here is a simple program that i have wrote which...
3
by: Godzilla | last post by:
Hello, I have a program that create and pop an object off a queue, but it is experiencing some memory leakage. I have been unable to detect where the memory leakage occur. The strange thing is...
0
by: Vivienne | last post by:
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...
6
by: Diwa | last post by:
// ----------------------------------- class Column { public: string name; vector<int values; }; // -----------------------------------
4
by: Dan Barbus | last post by:
Hi all, I'm a python beginner, and I have a problem with no solution I can see: I want to index all instances of an object by an internal id (not the hash) and be able to retrieve them later....
0
by: taylorcarr | last post by:
A Canon printer is a smart device known for being advanced, efficient, and reliable. It is designed for home, office, and hybrid workspace use and can also be used for a variety of purposes. However,...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: aa123db | last post by:
Variable and constants Use var or let for variables and const fror constants. Var foo ='bar'; Let foo ='bar';const baz ='bar'; Functions function $name$ ($parameters$) { } ...
0
by: ryjfgjl | last post by:
If we have dozens or hundreds of excel to import into the database, if we use the excel import function provided by database editors such as navicat, it will be extremely tedious and time-consuming...
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.