Solved

Can we delete a reference?

Posted on 2004-10-20
9
808 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
Free Tool: Path Explorer

An intuitive utility to help find the CSS path to UI elements on a webpage. These paths are used frequently in a variety of front-end development and QA automation tasks.

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

 
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: SSL Checker

Scans your site and returns information about your SSL implementation and certificate. Helpful for debugging and validating your SSL configuration.

One of a set of tools we are providing to everyone as a way of saying 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

Suggested Solutions

Title # Comments Views Activity
Handle Exceptions during instantiation 28 423
c++ syntax question 9 52
Arduino EDI - Programming language 3 104
Where are the c++ header files to use in Eclipse? 4 24
Often, when implementing a feature, you won't know how certain events should be handled at the point where they occur and you'd rather defer to the user of your function or class. For example, a XML parser will extract a tag from the source code, wh…
IntroductionThis article is the second in a three part article series on the Visual Studio 2008 Debugger.  It provides tips in setting and using breakpoints. If not familiar with this debugger, you can find a basic introduction in the EE article loc…
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.
The viewer will learn additional member functions of the vector class. Specifically, the capacity and swap member functions will be introduced.

830 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