473,569 Members | 2,634 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

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 SStringToBstrin g(System::Strin g ^ s, std::string& os ){

using namespace Runtime::Intero pServices;
pin_ptr<const wchar_twch = PtrToStringChar s(s);
size_t convertedChars = 0;
size_t sizeInBytes = ((s->Length + 1) * 2);
errno_t err = 0;
char *ch = (char *)malloc(sizeIn Bytes);
err = wcstombs_s(&con vertedChars, 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 2366
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 SStringToBstrin g(System::Strin g ^ 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...@g mail.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 SStringToBstrin g(System::Strin g ^ s, std::string& os ){

using namespace Runtime::Intero pServices;
pin_ptr<const wchar_twch = PtrToStringChar s(s);
size_t convertedChars = 0;
size_t sizeInBytes = ((s->Length + 1) * 2);
errno_t err = 0;
char *ch = (char *)malloc(sizeIn Bytes);
err = wcstombs_s(&con vertedChars, 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
3629
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 tested the pure Tkinter, # by modifiing on of the examples in the distribution. # This little guy also exibits the same behaviour. # Namely: every...
4
1382
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 temporary object is called so that the memory allocated for "new CTest( )" doesn't get deallocated.
2
7158
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 there is a memory leak. Here is a small example that reproduces this problem: I read of a File with many different(!) URIs. The next URI to visit...
30
4670
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; namespace MemLeak { class MemLeak
0
1402
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 ccmalloc reports that there is a memory leak: line 1: #include <iostream> line 2: line 3: using namespace std; line 4: line 5: int main(int argc,...
3
2554
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 when i replace the object creation with a plain integer/string, the leak goes away... Here's the code I used as my test: import Queue
0
245
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 ){ using namespace Runtime::InteropServices; pin_ptr<const wchar_twch = PtrToStringChars(s); size_t convertedChars = 0; size_t sizeInBytes =...
6
1533
by: Diwa | last post by:
// ----------------------------------- class Column { public: string name; vector<int values; }; // -----------------------------------
4
1103
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. I created a class-level dictionary (Item._itemById) where I add every new instance. My problem is:
0
7701
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However, people are often confused as to whether an ONU can Work As a Router. In this blog post, we’ll explore What is ONU, What Is Router, ONU & Router’s main...
0
7615
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can effortlessly switch the default language on Windows 10 without reinstalling. I'll walk you through it. First, let's disable language...
0
7924
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, it seems that the internal comparison operator "<=>" tries to promote arguments from unsigned to signed. This is as boiled down as I can make it. ...
0
8130
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 tapestry of website design and digital marketing. It's not merely about having a website; it's about crafting an immersive digital experience that...
1
7677
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows Update option using the Control Panel or Settings app; it automatically checks for updates and installs any it finds, whether you like it or not. For...
0
6284
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing, and deployment—without human intervention. Imagine an AI that can take a project description, break it down, write the code, debug it, and then...
1
5514
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new presenter, Adolph Dupré who will be discussing some powerful techniques for using class modules. He will explain when you may want to use classes...
0
3653
by: TSSRALBI | last post by:
Hello I'm a network technician in training and I need your help. I am currently learning how to create and manage the different types of VPNs and I have a question about LAN-to-LAN VPNs. The last exercise I practiced was to create a LAN-to-LAN VPN between two Pfsense firewalls, by using IPSEC protocols. I succeeded, with both firewalls in...
1
1223
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.

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.