?
Solved

Can we delete a reference?

Posted on 2004-10-20
9
Medium Priority
?
829 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
Concerto Cloud for Software Providers & ISVs

Can Concerto Cloud Services help you focus on evolving your application offerings, while delivering the best cloud experience to your customers? From DevOps to revenue models and customer support, the answer is yes!

Learn how Concerto can help you.

 
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 2000 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: Site Down Detector

Helpful to verify reports of your own downtime, or to double check a downed website you are trying to access.

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

Article by: SunnyDark
This article's goal is to present you with an easy to use XML wrapper for C++ and also present some interesting techniques that you might use with MS C++. The reason I built this class is to ease the pain of using XML files with C++, since there is…
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…
The goal of the tutorial is to teach the user how to use functions in C++. The video will cover how to define functions, how to call functions and how to create functions prototypes. Microsoft Visual C++ 2010 Express will be used as a text editor an…
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.

718 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