Getting around garbage collection

Discussion in 'C++' started by Bryan, Dec 30, 2003.

  1. Bryan

    Bryan Guest

    I've been messing around with a C++ application on Xbox, and have been
    encountering problems with my objects getting garbage collected when
    they go out of scope, but before I'm actually done using them. I'm not
    really familiar with how this works in C++, since I first learned C,
    then Java, and never really spent a lot of time learning C++ other
    than applying Java concepts to C++'s syntax. Here's my problem:

    I have a function (doesn't matter if it's a class method or just a
    random function) which instantiates an object of the Rect class by
    calling its constructor ( Rect asdf = Rect(100, 200, ... ); ) and then
    returns a pointer to asdf. However, once the function returns, asdf
    gets garbage collected (at least, I'm assuming that's what happens,
    since it's the only explanation I can think of for...) and my returned
    pointer is useless, and breaks my app if I try to use it. I guess in C
    I would have gotten around this by using malloc() to allocate the
    memory for asdf, but since I'm using C++ that would cause my
    constructor to not get called. There are probably a couple things
    about constructors in C++ I don't know that would help out here, so if
    anyone can help me out with that or suggest other ideas about how to
    fix this problem I'd appreciate it. Thanks in advance.

    Bryan
     
    Bryan, Dec 30, 2003
    #1
    1. Advertising

  2. "Bryan" <> wrote

    > I've been messing around with a C++ application on Xbox, and have
    > been
    > encountering problems with my objects getting garbage collected when
    > they go out of scope,


    If you're talking about standard C++, I think you mean "destructed" rather
    than "garbage collected."

    > but before I'm actually done using them. I'm not
    > really familiar with how this works in C++, since I first learned C,
    > then Java, and never really spent a lot of time learning C++ other
    > than applying Java concepts to C++'s syntax.


    If it's really just going out of scope, then you would have the same problem
    in C as well.

    > Here's my problem:
    >
    > I have a function (doesn't matter if it's a class method or just a
    > random function) which instantiates an object of the Rect class by
    > calling its constructor ( Rect asdf = Rect(100, 200, ... ); ) and then
    > returns a pointer to asdf. However, once the function returns, asdf
    > gets garbage collected (at least, I'm assuming that's what happens,
    > since it's the only explanation I can think of for...) and my returned
    > pointer is useless, and breaks my app if I try to use it.


    No, it's not garbage collected, it simply went out of scope and you've
    returned a dangling pointer. Dangling pointers are a Bad Thing that cause
    undefined behavior (like program crashes).

    > I guess in C
    > I would have gotten around this by using malloc() to allocate the
    > memory for asdf, but since I'm using C++ that would cause my
    > constructor to not get called.


    Okay, then you need to use C++'s "new". But you also need to learn about
    memory allocation and pointer issues in C++. A use of new generally
    requires a corresponding "delete". Better yet, you should use a smart
    pointer. Take a look at the C++ FAQ at parashift.com, or better yet get
    yourself a good introductory C++ book like Koenig and Moo's Accelerated C++.

    Best regards,

    Tom
     
    Thomas Tutone, Dec 30, 2003
    #2
    1. Advertising

  3. Bryan

    Jeff Schwab Guest

    Thomas Tutone wrote:
    > "Bryan" <> wrote
    >
    >
    >>I've been messing around with a C++ application on Xbox, and have
    >>been
    >>encountering problems with my objects getting garbage collected when
    >>they go out of scope,

    >
    >
    > If you're talking about standard C++, I think you mean "destructed" rather
    > than "garbage collected."
    >
    >
    >>but before I'm actually done using them. I'm not
    >>really familiar with how this works in C++, since I first learned C,
    >>then Java, and never really spent a lot of time learning C++ other
    >>than applying Java concepts to C++'s syntax.

    >
    >
    > If it's really just going out of scope, then you would have the same problem
    > in C as well.
    >
    >
    >>Here's my problem:
    >>
    >>I have a function (doesn't matter if it's a class method or just a
    >>random function) which instantiates an object of the Rect class by
    >>calling its constructor ( Rect asdf = Rect(100, 200, ... ); ) and then
    >>returns a pointer to asdf. However, once the function returns, asdf
    >>gets garbage collected (at least, I'm assuming that's what happens,
    >>since it's the only explanation I can think of for...) and my returned
    >>pointer is useless, and breaks my app if I try to use it.

    >
    >
    > No, it's not garbage collected, it simply went out of scope and you've
    > returned a dangling pointer. Dangling pointers are a Bad Thing that cause
    > undefined behavior (like program crashes).
    >
    >
    >>I guess in C
    >>I would have gotten around this by using malloc() to allocate the
    >>memory for asdf, but since I'm using C++ that would cause my
    >>constructor to not get called.

    >
    >
    > Okay, then you need to use C++'s "new". But you also need to learn about
    > memory allocation and pointer issues in C++. A use of new generally
    > requires a corresponding "delete". Better yet, you should use a smart
    > pointer. Take a look at the C++ FAQ at parashift.com, or better yet get
    > yourself a good introductory C++ book like Koenig and Moo's Accelerated C++.
    >
    > Best regards,
    >
    > Tom
    >
    >



    What Tom said, but I'd like to point out that the stack is your friend.

    If Rect's are not expensive to copy, it's probably faster to return your
    function's result by value, rather than by reference. Then you don't
    have to use pointers, new, or delete. E.g.:

    namespace Shapes
    {
    class Rect { }

    Rect make_rect( )
    {
    Rect result;
    /* ... */
    return result;
    }
    }

    int main( )
    {
    using namespace Shapes;

    Rect rect = make_rect( );
    }
     
    Jeff Schwab, Dec 30, 2003
    #3
  4. Bryan

    Arno Huetter Guest

    (Bryan) wrote in message news:<>...
    > I've been messing around with a C++ application on Xbox, and have been
    > encountering problems with my objects getting garbage collected when
    > they go out of scope, but before I'm actually done using them.


    There is no garbage collection in C++, unless you talk about managed
    C++ in the .NET world.

    > I have a function (doesn't matter if it's a class method or just a
    > random function) which instantiates an object of the Rect class by
    > calling its constructor ( Rect asdf = Rect(100, 200, ... ); ) and then
    > returns a pointer to asdf. However, once the function returns, asdf
    > gets garbage collected (at least, I'm assuming that's what happens,
    > since it's the only explanation I can think of for...)


    As you invoked "Rect asdf = Rect()" and not "Rect* asdfPtr = new
    Rect()", your Rect instance sits on the stack, and will be lost as
    soon as the function returns. The pointer returned will point to
    nirvana by then. You could return asdf itself, so it will be passed
    back to the caller, and the caller can then assign it to another Rect
    instance (this would involve a copy constructor invocation though).

    > There are probably a couple things
    > about constructors in C++ I don't know that would help out here, so if
    > anyone can help me out with that or suggest other ideas about how to
    > fix this problem I'd appreciate it. Thanks in advance.


    You must distinguish between objects on the stack (which run out of
    scope) and the heap, where dynamic memory allocation happens (as by
    invoking malloc resp. new), and where you are responsible for free'ing
    / deleting them as soon as they are not longer needed.

    If you come from the Java world, consider that there all objects are
    heap-based and garbage-collected (simple datatypes are stack-based),
    which is not the case in C++.

    Kind regards,
    Arno Huetter
     
    Arno Huetter, Dec 30, 2003
    #4
    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. Laser Lu

    Garbage Collection and Manage Code?

    Laser Lu, Jan 26, 2004, in forum: ASP .Net
    Replies:
    5
    Views:
    747
    Gaurav Khanna [C# MVP]
    Jan 27, 2004
  2. Piet
    Replies:
    0
    Views:
    601
  3. Replies:
    1
    Views:
    470
    mrstephengross
    Jul 25, 2005
  4. Øyvind Isaksen
    Replies:
    1
    Views:
    1,036
    Øyvind Isaksen
    May 18, 2007
  5. Network/Software Buyer
    Replies:
    0
    Views:
    442
    Network/Software Buyer
    May 23, 2010
Loading...

Share This Page