Solved

string sequence memory leak

Posted on 2002-07-17
22
692 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
22 Comments
 
LVL 30

Expert Comment

by:Axter
Comment Utility
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
Comment Utility
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
Comment Utility
>>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
 
LVL 30

Expert Comment

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

Expert Comment

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


0
 
LVL 30

Expert Comment

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

Author Comment

by:klf
Comment Utility
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
Comment Utility
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
Comment Utility
Axter,

Do you know what CORBA is?  
0
 
LVL 30

Expert Comment

by:Axter
Comment Utility
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
Comment Utility
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
IT, Stop Being Called Into Every Meeting

Highfive is so simple that setting up every meeting room takes just minutes and every employee will be able to start or join a call from any room with ease. Never be called into a meeting just to get it started again. This is how video conferencing should work!

 
LVL 30

Expert Comment

by:Axter
Comment Utility
>>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
Comment Utility
>>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
Comment Utility
Disregard previous post.
I just notice len is still equal to zero, in  list[len]=
0
 
LVL 30

Expert Comment

by:Axter
Comment Utility
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
Comment Utility
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
Comment Utility
>>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
Comment Utility
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
Comment Utility
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
Comment Utility
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
Comment Utility
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
Comment Utility
As requested, I will placet this question in PAQ.

Regards,
ComTech
CS Admin @ EE
0

Featured Post

What Is Threat Intelligence?

Threat intelligence is often discussed, but rarely understood. Starting with a precise definition, along with clear business goals, is essential.

Join & Write a Comment

What is C++ STL?: STL stands for Standard Template Library and is a part of standard C++ libraries. It contains many useful data structures (containers) and algorithms, which can spare you a lot of the time. Today we will look at the STL Vector. …
Many modern programming languages support the concept of a property -- a class member that combines characteristics of both a data member and a method.  These are sometimes called "smart fields" because you can add logic that is applied automaticall…
The goal of the video will be to teach the user the concept of local variables and scope. An example of a locally defined variable will be given as well as an explanation of what scope is in C++. The local variable and concept of scope will be relat…
The viewer will learn additional member functions of the vector class. Specifically, the capacity and swap member functions will be introduced.

728 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

Need Help in Real-Time?

Connect with top rated Experts

10 Experts available now in Live!

Get 1:1 Help Now