Pointer deletion question

Discussion in 'C++' started by Marcus, Feb 16, 2007.

  1. Marcus

    Marcus Guest

    Hi, when I try and delete a pointer in another class I get out of
    memory errors. Why does this fail? Is it not ok to do this?

    A.cpp
    void A::foo(){
    new MyObject *pObj = new MyObject;
    B b;
    b.foo2(pObj);
    }
    ///
    B.cpp
    void B:foo2(MyObject *pObj){
    //do something
    delete pObj;
    }
     
    Marcus, Feb 16, 2007
    #1
    1. Advertising

  2. Marcus wrote:
    > Hi, when I try and delete a pointer in another class I get out of
    > memory errors. Why does this fail? Is it not ok to do this?
    >
    > A.cpp
    > void A::foo(){
    > new MyObject *pObj = new MyObject;
    > B b;
    > b.foo2(pObj);
    > }
    > ///
    > B.cpp
    > void B:foo2(MyObject *pObj){
    > //do something
    > delete pObj;
    > }
    >


    It's legal to do it, although it's not usually considered good style.
    But it fails for you because you have a bug somewhere in your program.

    As is commonly the case the bug is in the code you didn't post. See
    Thomas Tutone's post in the thread 'Multiple substrings = Error' for a
    very good explanation of how to post programming problems (and look at
    the rest of the thread for how much time gets wasted when you don't
    follow these guidelines).

    john
     
    John Harrison, Feb 16, 2007
    #2
    1. Advertising

  3. Marcus

    Marcus Guest

    On Feb 15, 11:39 pm, John Harrison <>
    wrote:
    > Marcus wrote:
    > > Hi, when I try and delete a pointer in another class I get out of
    > > memory errors. Why does this fail? Is it not ok to do this?

    >
    > > A.cpp
    > > void A::foo(){
    > > new MyObject *pObj = new MyObject;
    > > B b;
    > > b.foo2(pObj);
    > > }
    > > ///
    > > B.cpp
    > > void B:foo2(MyObject *pObj){
    > > //do something
    > > delete pObj;
    > > }

    >
    > It's legal to do it, although it's not usually considered good style.
    > But it fails for you because you have a bug somewhere in your program.
    >
    > As is commonly the case the bug is in the code you didn't post. See
    > Thomas Tutone's post in the thread 'Multiple substrings = Error' for a
    > very good explanation of how to post programming problems (and look at
    > the rest of the thread for how much time gets wasted when you don't
    > follow these guidelines).
    >
    > john- Hide quoted text -
    >
    > - Show quoted text -


    Sorry if you misunderstood... my intention wasn't to have the group
    debug my code, I was just wondering if it was legal to do that. Now
    that I know that it's ok, I can look elsewhere for my problem. Thank
    you for the information, very much appreciated.
     
    Marcus, Feb 16, 2007
    #3
  4. "Marcus" <> writes:

    > A.cpp
    > void A::foo(){
    > new MyObject *pObj = new MyObject;

    ^^^
    Is this "new" a typo or have you used it deliberately?

    Thomas
     
    =?iso-8859-1?Q?Thomas_H=FChn?=, Feb 16, 2007
    #4
  5. Marcus

    Marcus Guest

    On Feb 16, 8:29 am, Thomas Hühn <> wrote:
    > "Marcus" <> writes:
    > > A.cpp
    > > void A::foo(){
    > > new MyObject *pObj = new MyObject;

    >
    > ^^^
    > Is this "new" a typo or have you used it deliberately?
    >
    > Thomas


    Yeah, sorry that's a typo.
     
    Marcus, Feb 16, 2007
    #5
    1. Advertising

Want to reply to this thread or ask your own question?

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. Christopher Pisz

    safe deletion of pointer, trees

    Christopher Pisz, Sep 3, 2004, in forum: C++
    Replies:
    2
    Views:
    373
    John Harrison
    Sep 4, 2004
  2. Replies:
    10
    Views:
    717
    Chris Torek
    Feb 4, 2005
  3. Aff@n
    Replies:
    1
    Views:
    370
    Ian Collins
    Oct 16, 2006
  4. Replies:
    5
    Views:
    4,962
    Stuart Redmann
    Aug 9, 2007
  5. Edsoncv

    JNI and pointer deletion

    Edsoncv, Apr 23, 2008, in forum: Java
    Replies:
    2
    Views:
    558
    Gordon Beaton
    Apr 24, 2008
Loading...

Share This Page