Solved

Can we delete a reference?

Posted on 2004-10-20
9
781 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
 
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
Top 6 Sources for Identifying Threat Actor TTPs

Understanding your enemy is essential. These six sources will help you identify the most popular threat actor tactics, techniques, and procedures (TTPs).

 

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

What Security Threats Are You Missing?

Enhance your security with threat intelligence from the web. Get trending threat insights on hackers, exploits, and suspicious IP addresses delivered to your inbox with our free Cyber Daily.

Join & Write a Comment

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…
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 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…

762 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

Need Help in Real-Time?

Connect with top rated Experts

20 Experts available now in Live!

Get 1:1 Help Now