473,396 Members | 1,703 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,396 software developers and data experts.

Getting around garbage collection

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
Jul 22 '05 #1
3 1416

"Bryan" <ba*****@calpoly.edu> 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
Jul 22 '05 #2
Thomas Tutone wrote:
"Bryan" <ba*****@calpoly.edu> 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( );
}
Jul 22 '05 #3
ba*****@calpoly.edu (Bryan) wrote in message news:<ff**************************@posting.google. 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
Jul 22 '05 #4

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

1
by: Bob | last post by:
Are there any known applications out there used to test the performance of the .NET garbage collector over a long period of time? Basically I need an application that creates objects, uses them, and...
0
by: Andrew | last post by:
When will .NET have a low-pause-time garbage collector A low-pause-time garbage collector would greatly improve .NET's ability to serve as a platform for soft real-time systems. It doesn't have...
6
by: Ganesh | last post by:
Is there a utility by microsoft (or anyone) to force garbage collection in a process without have access to the process code. regards Ganesh
2
by: Oculus | last post by:
Before I get into the question -- I know .NET isn't the right solution for this app but it's part of my clients requirements and writing this in C++ isn't an option. That being said -- my app is a...
11
by: Rick | last post by:
Hi, My question is.. if Lisp, a 40 year old language supports garbage collection, why didn't the authors of C++ choose garbage collection for this language? Are there fundamental reasons behind...
34
by: Ville Voipio | last post by:
I would need to make some high-reliability software running on Linux in an embedded system. Performance (or lack of it) is not an issue, reliability is. The piece of software is rather simple,...
5
by: Bob lazarchik | last post by:
Hello: We are considering developing a time critical system in C#. Our tool used in Semiconductor production and we need to be able to take meaurements at precise 10.0 ms intervals( 1000...
14
by: John J. Hughes II | last post by:
Using the below code I am send multiple sterilized object across an IP port. This works fine if only one object is received at a time but with packing sometimes there is more then one object or...
158
by: pushpakulkar | last post by:
Hi all, Is garbage collection possible in C++. It doesn't come as part of language support. Is there any specific reason for the same due to the way the language is designed. Or it is...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: ryjfgjl | last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.