Thread: Regarding Deleteing pointer!!!!!

  1. #1
    Registered User
    Join Date
    Sep 2009
    Posts
    26

    Question Regarding Deleteing pointer!!!!!

    Hi friends...

    Can any one give me the solutions....

    Code:
    int *m_ptr_ptr = new com::att::ib::Core_V1_0::NVPair
    
                    .......
                    .......
                    .......
    
    void
    com::att::ib::Core_V1_0::IT_Gen_NVPairStreamable::replace_nonshareable_arg(
        CORBA::IT_InStream_ptr is,
        unsigned long _direction
    )
    {
        if (*m_ptr_ptr != 0)
        {
            if (m_own_value|| _direction == CORBA::ARG_OUT)
            {
                delete *m_ptr_ptr;
            }
            if (_direction != CORBA::ARG_INOUT)
            {
                *m_ptr_ptr = 0;
            }
            if (_direction == CORBA::ARG_IN)
            {
                m_own_value = IT_TRUE;
            }
        }
        read(is);
    }
    Then I got the error like this......
    ==============================================
    Passing '*this->m_ptr_ptr' as parameter after its value was freed.
    ==============================================

    Also I want to know Can we delete a pointer like this?????

    delete *m_ptr_ptr; // is it correct
    delete m_ptr_ptr; // or is it correct

  2. #2
    Guest Sebastiani's Avatar
    Join Date
    Aug 2001
    Location
    Waterloo, Texas
    Posts
    5,708
    Well, m_ptr_ptr is an int*, so *m_ptr_ptr is an int, right? Which do you think would be valid in a delete expression - the former or the latter?

    Even so, a delete'd pointer should always be reset to NULL, and you should check if it is so before dereferencing it.

    Also, your code violates the RAII principle - use a 'smart' pointer.

  3. #3
    Registered User
    Join Date
    Sep 2009
    Posts
    26
    Sorry the code is like this...

    Code:
    class cATT_BUS
    {
     ....
     ...code//
     ...
    };
    cATT_BUS *m_ptr_ptr = new com::att::ib::Core_V1_0::NVPair
    
                    .......
                    .......
                    .......
    
    void
    com::att::ib::Core_V1_0::IT_Gen_NVPairStreamable::replace_nonshareable_arg(
        CORBA::IT_InStream_ptr is,
        unsigned long _direction
    )
    {
        if (*m_ptr_ptr != 0)
        {
            if (m_own_value|| _direction == CORBA::ARG_OUT)
            {
                delete *m_ptr_ptr;
            }
            if (_direction != CORBA::ARG_INOUT)
            {
                *m_ptr_ptr = 0;
            }
            if (_direction == CORBA::ARG_IN)
            {
                m_own_value = IT_TRUE;
            }
        }
        read(is);
    }
    After Compiling I got the error like this....

    ==============================================
    Passing '*this->m_ptr_ptr' as parameter after its value was freed.
    ==============================================

    Also I want to know Can we delete a pointer like this?????

    delete *m_ptr_ptr; // is it correct
    delete m_ptr_ptr; // or is it correct

  4. #4
    and the hat of sweating
    Join Date
    Aug 2007
    Location
    Toronto, ON
    Posts
    3,545
    I have no idea what com::att::ib::Core_V1_0::NVPair is, but I believe this:
    Code:
        if (*m_ptr_ptr != 0)
        {
            if (m_own_value|| _direction == CORBA::ARG_OUT)
            {
                delete *m_ptr_ptr;
            }
    should be changed to this:
    Code:
        if (m_ptr_ptr != 0)
        {
            if (m_own_value|| _direction == CORBA::ARG_OUT)
            {
                delete m_ptr_ptr;
                m_ptr_ptr = 0;
            }
    "I am probably the laziest programmer on the planet, a fact with which anyone who has ever seen my code will agree." - esbo, 11/15/2008

    "the internet is a scary place to be thats why i dont use it much." - billet, 03/17/2010

  5. #5
    C++まいる!Cをこわせ!
    Join Date
    Oct 2007
    Location
    Inside my computer
    Posts
    24,654
    Also I want to know Can we delete a pointer like this?????

    delete *m_ptr_ptr; // is it correct
    delete m_ptr_ptr; // or is it correct
    We already told you.
    Pass to delete the actual pointer you want to delete. So if you have
    Code:
    int* x = new int;
    Then do delete x, not delete *x, because *x would give you the int it points to, and delete needs its address.
    Quote Originally Posted by Adak View Post
    io.h certainly IS included in some modern compilers. It is no longer part of the standard for C, but it is nevertheless, included in the very latest Pelles C versions.
    Quote Originally Posted by Salem View Post
    You mean it's included as a crutch to help ancient programmers limp along without them having to relearn too much.

    Outside of your DOS world, your header file is meaningless.

  6. #6
    Registered User
    Join Date
    Sep 2009
    Posts
    26
    Thanks a lot.....

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Following CTools
    By EstateMatt in forum C Programming
    Replies: 5
    Last Post: 06-26-2008, 10:10 AM
  2. Quick Pointer Question
    By gwarf420 in forum C Programming
    Replies: 15
    Last Post: 06-01-2008, 03:47 PM
  3. Parameter passing with pointer to pointer
    By notsure in forum C++ Programming
    Replies: 15
    Last Post: 08-12-2006, 07:12 AM
  4. Direct3D problem
    By cboard_member in forum Game Programming
    Replies: 10
    Last Post: 04-09-2006, 03:36 AM
  5. Struct *** initialization
    By Saravanan in forum C Programming
    Replies: 20
    Last Post: 10-09-2003, 12:04 PM