Solved

Can we delete a reference?

Posted on 2004-10-20
9
812 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
[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
  • 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
Independent Software Vendors: 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 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

Free Tool: ZipGrep

ZipGrep is a utility that can list and search zip (.war, .ear, .jar, etc) archives for text patterns, without the need to extract the archive's contents.

One of a set of tools we're offering as a way to say thank you for being a part of the community.

Question has a verified solution.

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

Unlike C#, C++ doesn't have native support for sealing classes (so they cannot be sub-classed). At the cost of a virtual base class pointer it is possible to implement a pseudo sealing mechanism The trick is to virtually inherit from a base class…
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 be introduced to the member functions push_back and pop_back of the vector class. The video will teach the difference between the two as well as how to use each one along with its functionality.

751 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