how to delete?

Collapse
X
 
  • Time
  • Show
Clear All
new posts
  • rasmidas
    New Member
    • Jun 2007
    • 56

    how to delete?

    I have a small function as below:

    Code:
    int dmgCreateFilter( DmgString ListOfParam[NUMBERPARAMMAX],
                         sTRADE_FILTER& aFilter,
                         int aMode  )
    {
       try
       {
          DmgPtrList<Token> tokens;
    
          for ( int i=0; i<NUMBERPARAMMAX/2; ++i )
          {
             if ( ListOfParam[2*i].IsBlank() != sYES )
             {
                Token* token = new Token( ListOfParam[2*i], ListOfParam[2*i+1] );
                tokens.insert( token );
             }
          }
    
          DmgFilter filter( &aFilter );
          filter.ReInit();
    
          filter.Create( tokens );
    
    
          if ( aMode == 01 )
          {
             filter.DoSTDAction( NULL, sACT_SAVE, 00 );
          }
    
          return sSUCCESS;
       }
       catch( DmgException& listEx )
       {
          listEx.ReThrowGpo(" Exception in dmgCreateFilter ", sSERIOUS );
       }
       catch ( ... )
       {
          DmgException::ThrowGpo( "dmgCreateFilter::dmgCreateFilter() : Unknown Exception", sSERIOUS );
       }
    }
    token has been new'ed in this function and getting insert into the tokens list, which is a pointer list. If I will delete token will it be a problem? If I have to delete how to delete this token?

    FYI,

    typedef struct TOKEN
    {
    char * Name; /* Name of token */
    int Type; /* Type of token */
    /* 0 - String */
    /* 1 - Enumeration */
    /* 2 - Numeric floating */
    /* 3 - Numeric integer */
    /* 4 - Timeband */
    /* 5 - Date */
    /* 6 - Numeric floating / sBP */
    /* 7 - Numeric floating / 100 */
    char * EType; /* Enumeration string type (if applicable) */
    int Size; /* Size of the variable pointed to by this token */
    int Offset; /* Offset of variable into structure */
    }Token ;



    Thanks in advance.
  • gpraghuram
    Recognized Expert Top Contributor
    • Mar 2007
    • 1275

    #2
    Originally posted by rasmidas
    I have a small function as below:

    Code:
    int dmgCreateFilter( DmgString ListOfParam[NUMBERPARAMMAX],
                         sTRADE_FILTER& aFilter,
                         int aMode  )
    {
       try
       {
          DmgPtrList<Token> tokens;
    
          for ( int i=0; i<NUMBERPARAMMAX/2; ++i )
          {
             if ( ListOfParam[2*i].IsBlank() != sYES )
             {
                Token* token = new Token( ListOfParam[2*i], ListOfParam[2*i+1] );
                tokens.insert( token );
             }
          }
    
          DmgFilter filter( &aFilter );
          filter.ReInit();
    
          filter.Create( tokens );
    
    
          if ( aMode == 01 )
          {
             filter.DoSTDAction( NULL, sACT_SAVE, 00 );
          }
    
          return sSUCCESS;
       }
       catch( DmgException& listEx )
       {
          listEx.ReThrowGpo(" Exception in dmgCreateFilter ", sSERIOUS );
       }
       catch ( ... )
       {
          DmgException::ThrowGpo( "dmgCreateFilter::dmgCreateFilter() : Unknown Exception", sSERIOUS );
       }
    }
    token has been new'ed in this function and getting insert into the tokens list, which is a pointer list. If I will delete token will it be a problem? If I have to delete how to delete this token?

    FYI,

    typedef struct TOKEN
    {
    char * Name; /* Name of token */
    int Type; /* Type of token */
    /* 0 - String */
    /* 1 - Enumeration */
    /* 2 - Numeric floating */
    /* 3 - Numeric integer */
    /* 4 - Timeband */
    /* 5 - Date */
    /* 6 - Numeric floating / sBP */
    /* 7 - Numeric floating / 100 */
    char * EType; /* Enumeration string type (if applicable) */
    int Size; /* Size of the variable pointed to by this token */
    int Offset; /* Offset of variable into structure */
    }Token ;



    Thanks in advance.

    [code=cpp]
    DmgPtrList<Toke n> tokens; //This says the list stores stack objects

    for ( int i=0; i<NUMBERPARAMMA X/2; ++i )
    {
    if ( ListOfParam[2*i].IsBlank() != sYES )
    {
    Token* token = new Token( ListOfParam[2*i], ListOfParam[2*i+1] );
    tokens.insert( token ); //Here u are storing pointer object
    }
    }

    [/code]

    How this is compiling?(see my comments in the above code)
    If u want to delete the objects stored in the list, try to use an iterator and get object one by one and then delete the object.

    Raghuram

    Comment

    • rasmidas
      New Member
      • Jun 2007
      • 56

      #3
      Please correct me if I am wrong.
      Here tokens is the name of the static list whose elements are pointers.
      So I think we can insert pointers into this list.

      Now my question is if I will delete the new'ed variables, it will not affect my function anyways?

      Thanks.

      Comment

      • rasmidas
        New Member
        • Jun 2007
        • 56

        #4
        Also I am not clear how to delete this. could anyone please write here that portion how to delete.

        Thanks.

        Comment

        • gpraghuram
          Recognized Expert Top Contributor
          • Mar 2007
          • 1275

          #5
          Originally posted by rasmidas
          Also I am not clear how to delete this. could anyone please write here that portion how to delete.

          Thanks.
          You can delete like this
          [code=cpp]
          DmgPtrList<Toke n>::iterator;
          for(iterator=to kens.begin();it erator!=tokens. end();iterator+ +)
          {
          Tokens *tok = (*iterator);
          delete tok;
          }
          [/code]

          Hope this helps.
          Raghuram

          Comment

          • weaknessforcats
            Recognized Expert Expert
            • Mar 2007
            • 9214

            #6
            Originally posted by rasmidas
            If I will delete token will it be a problem? If I have to delete how to delete this token?
            Yes there will be a problem because you don't know how many iterators are pointing at that token. You can onlydelete when you know you have the only pointer. I do not see any reference counting in your code.

            The single worst thing you can do in a C++ program is pass pointers and iterators around. If you delete and there is more than one pointer to the data, you crash trying to access the data with the other pointer. If you don't delete, then you leak. It's a lose-lose scenario.

            You must use smart pointers. That is, you must use handles. Go to the C/C++ Articles formum and read the article on Handle Classes. In that article is a template for a handle. Copy that template and implement in your own code.

            Then you will not ask questions about how to delete a token. You will do it in the destructor of the handle.

            Comment

            • rasmidas
              New Member
              • Jun 2007
              • 56

              #7
              please let me know how to delete the token in the existing code.
              Should I take a counter, to count the number of token allocated and then delete? Please let me know. Its very very urgent to fix this for me.
              Thanks.

              Comment

              • xoinki
                New Member
                • Apr 2007
                • 110

                #8
                hi,
                you are right, put a counter inside the "if" where u r allocating,
                then loop thru that many number of times and
                just do delete token it will work..

                Comment

                • rasmidas
                  New Member
                  • Jun 2007
                  • 56

                  #9
                  Other than using delete, if I will use
                  tokens.clear();

                  will this work?

                  Thanks,
                  Rasmi.

                  Comment

                  Working...