Solved

Can we delete a reference?

Posted on 2004-10-20
9
802 Views
Last Modified: 2012-06-21
Hi,
     Can we delete a reference?
Below is the code.

   ...
    RWTValSlist<RWCString> fileList;
    RWTValSlistIterator<RWCString> fileListIter(fileList);
   ...
            const char* dirFileName;
            while(dirFileName = directory.getNextMatchingFile(subDirRegExp))
            {
                RWCString* temp_fileName = new RWCString(dirFileName);
                fileList.append(*temp_fileName);
             }
   ...
            while(fileListIter() == TRUE)
            {
                char* inFileName = (char*) malloc(PATH_MAX);
                inFileName = (char *)fileListIter.key().data();
                .....
                free(inFileName);
           }
          fileListIter.reset();
          for(;fileListIter();)
          {
               delete fileListIter.key();
          }
        ...

     

In the above code, I want to store filenames in a list and process each of them. I am using Rogue wave library. RWTValSlist::append() takes a reference as parameter and RWTValSlistIterator::key() returns a reference for the stored value. I want to free the memory allocated for RWCString in the first while loop, and what I am doing at the end (i.e deleting them) is right? If not then what is the correct way of doing it.

Thanks.
0
Comment
Question by:dkamdar
  • 4
  • 3
  • 2
9 Comments
 
LVL 3

Expert Comment

by:aviadbd
ID: 12361224

You can use

// RWTValSlist< RWCString > fileList;
RWTValSlist< RWCString* > fileList;

and then

// fileList.append(*temp_fileName);
fileList.append(temp_fileName);


AviadBD.
0
 
LVL 3

Expert Comment

by:aviadbd
ID: 12361261
A thing I dont get though:

                char* inFileName = (char*) malloc(PATH_MAX);
                inFileName = (char *)fileListIter.key().data();
                .....
                free(inFileName);

It seems like you're allocating data (dont know why by malloc() and not new(), if you're using c++), then Not using the allocated space, causing a leak of memory, and then releasing data that is not yours?

AviadBD.
0
 
LVL 86

Expert Comment

by:jkr
ID: 12361283
>> Can we delete a reference?

In general: No. However, if you have a reference to a pointer, you may 'delete' that (or, actually free the memorey the pointer points to). According to the RW docs (http://www.roguewave.com/support/docs/sourcepro/toolsref/rwcstring.html), the proper thing would be to

          for(;fileListIter();)
         {
              fileListIter.key().resize(0);
         }

0
Problems using Powershell and Active Directory?

Managing Active Directory does not always have to be complicated.  If you are spending more time trying instead of doing, then it's time to look at something else. For nearly 20 years, AD admins around the world have used one tool for day-to-day AD management: Hyena. Discover why

 
LVL 86

Expert Comment

by:jkr
ID: 12361302
>>                inFileName = (char *)fileListIter.key().data();
>>               .....
>>               free(inFileName);

is *absolutely* illegal and will certainly lead to a crash - quote from http://www.roguewave.com/support/docs/sourcepro/toolsref/rwcstring.html#idx1046 

const char*
data() const;

Access to the RWCString's data as a null terminated string. This datum is owned by the RWCString and may not be deleted or changed
0
 

Author Comment

by:dkamdar
ID: 12361589
>>>char* inFileName = (char*) malloc(PATH_MAX);    
        I allocated that memory for a different purpose earlier(there was some problem earlier and this allocation removed it), and some how I messed with it.

Hey Jkr,

>>                inFileName = (char *)fileListIter.key().data();
>>               .....
>>               free(inFileName);


the above code is not causing any crash.

And will this code
for(;fileListIter();)
         {
              fileListIter.key().resize(0);
         }
avoid memory leak ?????
( for the memory I allocated at the beginning.RWCString* temp_fileName = new RWCString(dirFileName);)

 Thanks.
0
 
LVL 86

Expert Comment

by:jkr
ID: 12361650
If you want to avoid memory leaks, why don't you use

           const char* dirFileName;
           while(dirFileName = directory.getNextMatchingFile(subDirRegExp))
           {
               RWCString temp_fileName (dirFileName);
               fileList.append(temp_fileName);
            }

There's no need to create a temporary RWCString pointer using 'new', just use the constructor that takes a char*

>>the above code is not causing any crash

But, as RogueWave states: "This datum is owned by the RWCString and may not be deleted or changed"
0
 

Author Comment

by:dkamdar
ID: 12361728
Hi jkr,
      I tried doing that

>>          const char* dirFileName;
>>          while(dirFileName = directory.getNextMatchingFile(subDirRegExp))
>>           {
>>               RWCString temp_fileName (dirFileName);
>>               fileList.append(temp_fileName);
>>            }

but that caused some memory problem at a different place. It was weird but when I allocated memory dynamically, it solved the problem. I thought since the temp_fileName is destructed immediately at the end of the iteration, probably that was causing my other problem. I couldn't see any relation, but allocating dynamically did solve the problem.

Thanks.
0
 
LVL 86

Accepted Solution

by:
jkr earned 500 total points
ID: 12361806
>>that caused some memory problem at a different place

Well, it shouldn't. The only *real* problem I see that could cause that is here:

               char* inFileName = (char*) malloc(PATH_MAX);
               inFileName = (char *)fileListIter.key().data();
               .....
               free(inFileName);

Apart from that you shalst not use 'free()' here, that should be

               char* inFileName = (char*) malloc(PATH_MAX);
               strcpy(inFileName,fileListIter.key().data()); // <-----------!!!
               .....
               free(inFileName);
0
 
LVL 3

Expert Comment

by:aviadbd
ID: 12362032


dkamdar,

The memory problem you're having is because you're allocating the memory of the RWCString inside a scope of while, and when the loop ends it deallocates automatically.

Thats why using heap works - it doesnt get deallocated. However, you should use a pointer to RWCString with your list, so you will be the one deallocating them yourself.

I agree with jkr on the strcpy part - Like I said, what you did is a leak and then freeing memory which isnt yours - It will result in a crash, if not immediatly then sometime soon along the way.

AviadBD.
0

Featured Post

Windows Server 2016: All you need to know

Learn about Hyper-V features that increase functionality and usability of Microsoft Windows Server 2016. Also, throughout this eBook, you’ll find some basic PowerShell examples that will help you leverage the scripts in your environments!

Question has a verified solution.

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

Suggested Solutions

Errors will happen. It is a fact of life for the programmer. How and when errors are detected have a great impact on quality and cost of a product. It is better to detect errors at compile time, when possible and practical. Errors that make their wa…
This article will show you some of the more useful Standard Template Library (STL) algorithms through the use of working examples.  You will learn about how these algorithms fit into the STL architecture, how they work with STL containers, and why t…
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 how to pass data into a function in C++. This is one step further in using functions. Instead of only printing text onto the console, the function will be able to perform calculations with argumentents given by the user.

770 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