473,663 Members | 2,694 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 2374
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
3634
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 time the window is closed and reoppend, # there is memory leak of several hundreds 384K
4
1387
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
7164
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 is saved in the variable "uri".Then I try to connect to this uri. I do it with the following line...
30
4683
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
1408
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, char **argv) line 6: {
3
2557
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 = ((s->Length + 1) * 2); errno_t err = 0;
6
1541
by: Diwa | last post by:
// ----------------------------------- class Column { public: string name; vector<int values; }; // -----------------------------------
4
1107
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
8435
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 usage, and What is the difference between ONU and Router. Let’s take a closer look ! Part I. Meaning of...
0
8345
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 synchronization. With a Microsoft account, language settings sync across devices. To prevent any complications,...
0
8857
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. Here is my compilation command: g++-12 -std=c++20 -Wnarrowing bit_field.cpp Here is the code in...
1
8547
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 most users, this new feature is actually very convenient. If you want to control the update process,...
1
6186
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 instead of User Defined Types (UDT). For example, to manage the data in unbound forms. Adolph will...
0
5655
by: conductexam | last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and then checking html paragraph one by one. At the time of converting from word file to html my equations which are in the word document file was convert into image. Globals.ThisAddIn.Application.ActiveDocument.Select();...
0
4181
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 the same network. But I'm wondering if it's possible to do the same thing, with 2 Pfsense firewalls...
0
4348
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
2
1754
bsmnconsultancy
by: bsmnconsultancy | last post by:
In today's digital era, a well-designed website is crucial for businesses looking to succeed. Whether you're a small business owner or a large corporation in Toronto, having a strong online presence can significantly impact your brand's success. BSMN Consultancy, a leader in Website Development in Toronto offers valuable insights into creating effective websites that not only look great but also perform exceptionally well. In this comprehensive...

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.