?
Solved

string sequence memory leak

Posted on 2002-07-17
22
Medium Priority
?
723 Views
Last Modified: 2007-12-19
Why does this leak in VisiBroker 4.5 and how do I fix it.

idl file contains

typedef sequence<string> StringList

c++ code the does something like the following

StringList list;
list.length(100);
for (;;) {
list.length(0);
for (CORBA::ULong i = 0; i < 100; i++) {
   int len = list.length();
   list.length(len + 1);
   list[len] = CORBA::string_dup("0123456789");
}
}

0
Comment
Question by:klf
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
22 Comments
 
LVL 30

Expert Comment

by:Axter
ID: 7161256
I don't know about your StringList object, but I would think you would want your code to look like the following:

StringList list;
list.length(100);
for (CORBA::ULong i = 0; i < 100; ++i)
{
  list[i] = CORBA::string_dup("0123456789");
}


0
 
LVL 1

Author Comment

by:klf
ID: 7161788
Axter,

The code in the question is contrived the real code uses the StringList object repeatedly and will put varying amounts of data into it.  That is why the size is reset to 0 each time.  StringList is just a typedef of sequence<string>.
0
 
LVL 30

Expert Comment

by:Axter
ID: 7161795
>>typedef of sequence<string>.

Are you saying StringList is a typedef of std::string?

std::string does not have a length member variable that takes an argument.
0
Technology Partners: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

 
LVL 30

Expert Comment

by:Axter
ID: 7161796
Correction:
std::string does not have a length member function that takes an argument.
0
 
LVL 30

Expert Comment

by:Axter
ID: 7161802
The code you posted has an endless loop.
for (;;) { //Nothing to break out of this loop


0
 
LVL 30

Expert Comment

by:Axter
ID: 7161804
Can you post the header that defines sequence type?
0
 
LVL 1

Author Comment

by:klf
ID: 7161806
Axter.

I did not make this clear.  typedef sequence<string> if a CORBA structure.  This is all CORBA code.  

As for the the "for(;;)" this was done so I could esaly see the memory leak.  This is just example code.  The real code does not have an endless loop.


0
 
LVL 30

Expert Comment

by:Axter
ID: 7161809
What does CORBA::string_dup("0123456789") due?

What is the purpose of string_dup, versus just "0123456789"?

Do you still get a memory leak if you just use "0123456789"?
0
 
LVL 1

Author Comment

by:klf
ID: 7161812
Axter,

Do you know what CORBA is?  
0
 
LVL 30

Expert Comment

by:Axter
ID: 7161813
I'm not sure how sequence functions, but the following might be the problem.

list.length(0); //list is now a zero len array
for (CORBA::ULong i = 0; i < 100; i++) {
  int len = list.length(); // len = 0
  list.length(len + 1); //list is not a 1 len array
  list[len] = CORBA::string_dup("0123456789"); //Here you're setting the second array
}
0
 
LVL 1

Author Comment

by:klf
ID: 7161835
Axter,

No dice.  I know for a fact that the code above does not leak when I use the IONA CORBA orb and idl compiler.
0
 
LVL 30

Expert Comment

by:Axter
ID: 7161844
>>Do you know what CORBA is?  
It's a platform independence and language independence set of objects.

In order for you to use CORBA, there should still be a header for your object, which would be useful to post here.
0
 
LVL 30

Expert Comment

by:Axter
ID: 7161850
>>No dice.  I know for a fact that the code above does not
>>leak when I use the IONA CORBA orb and idl compiler.

Just because it doesn't leak in one or more compilers, doesn't mean that there's nothing wrong with the code.

Did you understand the logic error I posted?

Example:
std::string Data[1];
//The following is assigning data pass the length of the arrray
Data[1] = "bla bla bla"; //Error

This is the samething that your code is doing.
0
 
LVL 30

Expert Comment

by:Axter
ID: 7161863
Disregard previous post.
I just notice len is still equal to zero, in  list[len]=
0
 
LVL 30

Expert Comment

by:Axter
ID: 7161870
You still didn't answer my previous question about using "0123456789" instead of CORBA::string_dup("0123456789").

Have you tried using just "0123456789", and if so, did you still get the memory leak.

I'm asking this because, if it only happens with the use of CORBA::string_dup, then this could be a problem with the std::string referencing implementation.
0
 
LVL 1

Author Comment

by:klf
ID: 7161885
I have to use CORBA::string_dup so the sequence makes a copy of the string which can be modified later by other code.
0
 
LVL 30

Expert Comment

by:Axter
ID: 7161923
>>I have to use CORBA::string_dup so the sequence makes a
>>copy of the string which can be modified later by other
>>code.

But do you still have the memory leak when you use "0123456789" instead of CORBA::string_dup?

Also does CORBA::string_dup return a std::string type, or a char* type?
0
 
LVL 1

Author Comment

by:klf
ID: 7161990
Also does CORBA::string_dup return a std::string type, or a char* type?

char *

I have not changed the code to use just "0123456789". I'll try this when I get to work.  However, the real code will do some thing like

list[i] = CORBA::string_dub(name)

where name is a var passed to a function.  I'll need to make a copy of the string any way.  

0
 
LVL 1

Author Comment

by:klf
ID: 7162255
the borland support teams sent me the answer.

Please let me repeat my understanding of your problem back to
you so as to ensure that we understand each other:
You want to know why setting the length of the sequence to zero
will leak memory.

If what I have repeated is correct, here are my recommendations:
This problem causes memory leaks in Visibroker for C++ 4, whenever downsizing length(n) operation is applied on a sequence with elements that use dynamic memory, and if the "tail" elements are not destroyed before. It is filed as failure to be addressed in future releases of the product.

The practical workaround is:

Before applying to such a sequence the operation length(n) with n being less then previously established length, assign NULL (zero) value to all the items holding elements that would be orphaned by the subsequent length(n) operation. Note that you cannot invoke explicit memory freeing operation without nullifying the pointers (that would cause errors in the destructor), while just assigning NULL does just fine, causing memory be freed properly under the hood.

In other words, if we have a sequence seq, the current length is m, the new will be n, and m > n, then all the items n, ..., m-1 have to be assigned NULL before calling length(n), something like:

for (int i = n; i < m; i++ ) {
  seq[i] = NULL;
}
seq.length(n);
0
 
LVL 1

Author Comment

by:klf
ID: 7162277
the borland support teams sent me the answer.

Please let me repeat my understanding of your problem back to
you so as to ensure that we understand each other:
You want to know why setting the length of the sequence to zero
will leak memory.

If what I have repeated is correct, here are my recommendations:
This problem causes memory leaks in Visibroker for C++ 4, whenever downsizing length(n) operation is applied on a sequence with elements that use dynamic memory, and if the "tail" elements are not destroyed before. It is filed as failure to be addressed in future releases of the product.

The practical workaround is:

Before applying to such a sequence the operation length(n) with n being less then previously established length, assign NULL (zero) value to all the items holding elements that would be orphaned by the subsequent length(n) operation. Note that you cannot invoke explicit memory freeing operation without nullifying the pointers (that would cause errors in the destructor), while just assigning NULL does just fine, causing memory be freed properly under the hood.

In other words, if we have a sequence seq, the current length is m, the new will be n, and m > n, then all the items n, ..., m-1 have to be assigned NULL before calling length(n), something like:

for (int i = n; i < m; i++ ) {
  seq[i] = NULL;
}
seq.length(n);
0
 
LVL 27

Expert Comment

by:Asta Cu
ID: 7162548
Since you posted the solution you found outside this question thread, recommend that you post a zero point question in Community Support and request a REFUND/PAQ rather than a deletion, since it can then help others with similar needs.

Asta
0
 

Accepted Solution

by:
ComTech earned 0 total points
ID: 7162730
As requested, I will placet this question in PAQ.

Regards,
ComTech
CS Admin @ EE
0

Featured Post

On Demand Webinar: Networking for the Cloud Era

Did you know SD-WANs can improve network connectivity? Check out this webinar to learn how an SD-WAN simplified, one-click tool can help you migrate and manage data in the cloud.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

When writing generic code, using template meta-programming techniques, it is sometimes useful to know if a type is convertible to another type. A good example of when this might be is if you are writing diagnostic instrumentation for code to generat…
In days of old, returning something by value from a function in C++ was necessarily avoided because it would, invariably, involve one or even two copies of the object being created and potentially costly calls to a copy-constructor and destructor. A…
The goal of the tutorial is to teach the user how to use functions in C++. The video will cover how to define functions, how to call functions and how to create functions prototypes. Microsoft Visual C++ 2010 Express will be used as a text editor an…
The viewer will learn how to user default arguments when defining functions. This method of defining functions will be contrasted with the non-default-argument of defining functions.
Suggested Courses

764 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question