Velocity Reviews - Computer Hardware Reviews

Velocity Reviews > Newsgroups > Programming > C++ > Getting around garbage collection

Reply
Thread Tools

Getting around garbage collection

 
 
Bryan
Guest
Posts: n/a
 
      12-30-2003
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
 
Reply With Quote
 
 
 
 
Thomas Tutone
Guest
Posts: n/a
 
      12-30-2003

"Bryan" <(E-Mail Removed)> 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


 
Reply With Quote
 
 
 
 
Jeff Schwab
Guest
Posts: n/a
 
      12-30-2003
Thomas Tutone wrote:
> "Bryan" <(E-Mail Removed)> 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( );
}


 
Reply With Quote
 
Arno Huetter
Guest
Posts: n/a
 
      12-30-2003
http://www.velocityreviews.com/forums/(E-Mail Removed) (Bryan) wrote in message news:<(E-Mail Removed). com>...
> 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
 
Reply With Quote
 
 
 
Reply

Thread Tools

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
Collection problems (create Collection object, add data to collection, bind collection to datagrid) Řyvind Isaksen ASP .Net 1 05-18-2007 09:24 AM
Microsoft & patents: what goes around comes around... Lawrence D'Oliveiro NZ Computing 104 12-16-2006 07:11 AM
Read all of this to understand how it works. then check around on otherRead all of this to understand how it works. then check around on other thelisa martin Computer Support 2 08-18-2005 06:40 AM
Templates - Garbage In Garbage Not Out ramiro_b@yahoo.com C++ 1 07-25-2005 04:48 PM
Make wxListCtrl fit around contents and parent frame fit around listctrl Piet Python 0 07-18-2004 08:27 AM



Advertisments