Want to protect your cyber security and still get fast solutions? Ask a secure question today.Go Premium

x
  • Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 734
  • Last Modified:

string sequence memory leak

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
klf
Asked:
klf
1 Solution
 
AxterCommented:
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
 
klfAuthor Commented:
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
 
AxterCommented:
>>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!

 
AxterCommented:
Correction:
std::string does not have a length member function that takes an argument.
0
 
AxterCommented:
The code you posted has an endless loop.
for (;;) { //Nothing to break out of this loop


0
 
AxterCommented:
Can you post the header that defines sequence type?
0
 
klfAuthor Commented:
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
 
AxterCommented:
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
 
klfAuthor Commented:
Axter,

Do you know what CORBA is?  
0
 
AxterCommented:
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
 
klfAuthor Commented:
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
 
AxterCommented:
>>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
 
AxterCommented:
>>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
 
AxterCommented:
Disregard previous post.
I just notice len is still equal to zero, in  list[len]=
0
 
AxterCommented:
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
 
klfAuthor Commented:
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
 
AxterCommented:
>>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
 
klfAuthor Commented:
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
 
klfAuthor Commented:
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
 
klfAuthor Commented:
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
 
Asta CuCommented:
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
 
ComTechCommented:
As requested, I will placet this question in PAQ.

Regards,
ComTech
CS Admin @ EE
0

Featured Post

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!

Tackle projects and never again get stuck behind a technical roadblock.
Join Now