View Single Post
Old 02-07-2005, 08:54 PM   #1 (permalink)
wishknew
Wisnu Widiarta
 
wishknew's Avatar
 
Join Date: Feb 2005
Location: Indonesia
Posts: 14
wishknew is on a distinguished road
Send a message via Yahoo to wishknew
Memory Leak Question

Hello Friends,

First of all, I am using g++ compiler under linux.
I have a question whether my code prone to memory leak.

Here is the prototype:


Code:
#ifndef __DELIMITEDSTRING_H__
#define __DELIMITEDSTRING_H__
#include <iostream>
#include <vector>

/* ****************************************
 *  Class  : DelimitedString 
 *  Desc   : This class is intended to
 *           perform string split, with delimiter ','
 *           hrmh,wisnu,widiarta --> getElement(0) returns hrmh
                                 --> getElement(2) returns widiarta
 *****************************************/

using namespace std;

class DelimitedString {
   public:
     DelimitedString();
     DelimitedString(string delimited);
     DelimitedString(string delimited, string delimiter);
     ~DelimitedString();
     
     string getText();
     void setDelimitedString(string input);
     void setDelimiter(string delimiter);
          
     int getCount();
     string getElement(int index);
   private:
     string input;
          
     int count;
     string delimiter;
     vector<string> elements; 
     string trim(const string inputString);
     
     void parse();
};

//The method parse and destructor are below

void DelimitedString::parse() { 
   string* token = new string("");     
   elements.clear();   
   input = trim(input);

   int index;
   string newInput(input + delimiter);
   index = (int)newInput.find(delimiter); 
   
   for(;index != -1; index = (int)newInput.find(delimiter)) {
        try { 
          token = new string(trim(newInput.substr(0, index)));                    
          elements.push_back(*token);            
        }
        catch (const exception& error) {
          cerr << "Error in DelimitedString::Parse : " << error.what() << endl;
        }
       
        try {
           newInput = newInput.substr(index + 1) + "";           
        }
        catch (const exception& error) {
          cerr << "Error in DelimitedString::Parse : " << error.what() << endl;
        }           
   }
}

DelimitedString::~DelimitedString() {
   elements.~vector<string>();
}


My questions are :

1. Can the statement string* token = new string(""); cause memory leak? Can I just use string? (I have tried but since it is local, it caused segmentaton fault)
2. If yes, how to avoid that?
3. I have tried delete token at the bottom, but since it still being used by vector<string>, it caused segmentation fault.
4. Should I use vector<string*> instead of vector<string>? Before I call the statement in the current destructor, I iterate the vector and delete the element one by one. Is it better or even worse?
5. What does the statement elements.~vector<string>(); mean?

Thank you in advance.

Wisnu Widiarta
wishknew is offline   Reply With Quote