Getting around garbage collection

B

Bryan

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
 
T

Thomas Tutone

Bryan said:
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
 
J

Jeff Schwab

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




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




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).




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( );
}
 
A

Arno Huetter

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
 

Ask a Question

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

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,744
Messages
2,569,483
Members
44,901
Latest member
Noble71S45

Latest Threads

Top