473,320 Members | 1,694 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,320 software developers and data experts.

Safe garbage collecting

Hi!
I am trying to implement a safe garbage collecting system. I use
reference counting combined with smart pointers and a garbage collector
which keeps a list of all heap-allocated objects. Now I still have the
problem to distinguish between stack and heap allocated objects ( see
also "Determining if object has been allocated using operator new" ).
I either have the choice to disallow multiple/virtual inheritance (bad)
OR disallow stack allocated objects and directly "embedded" objects
(worse).
It seems I won't be able to solve those problems, so I want to know if
there is another concept for memory management. Any hints or pointers
are appreciated.

TIA,
Robert

Sep 4 '05 #1
10 1336

"Robert Potthast" wrote:
Now I still have the problem to distinguish between
stack and heap allocated objects

Check whether the "this" value is inside
stack range. It can be done portable and quickly.

/Pavel
Sep 4 '05 #2

"Pavel Vozenilek" <pa*************@yahoo.co.uk> wrote in message
news:3o************@individual.net...

"Robert Potthast" wrote:
Now I still have the problem to distinguish between
stack and heap allocated objects

Check whether the "this" value is inside
stack range. It can be done portable and quickly.


It's easy to find the top of the stack, but I know of no portable way to
find the bottom of it. If I've missed something, please let me know!
Sep 4 '05 #3

"Walter Bright" wrote:
"Robert Potthast" wrote:
> Now I still have the problem to distinguish between
> stack and heap allocated objects
>

Check whether the "this" value is inside
stack range. It can be done portable and quickly.


It's easy to find the top of the stack, but I know of no portable way to
find the bottom of it. If I've missed something, please let me know!


OK, I assumed garbage collection kicks in after main()
is entered and the current-top-of-stack at the moment
is stored as global variable.

/Pavel

Sep 5 '05 #4

"Pavel Vozenilek" <pa*************@yahoo.co.uk> wrote in message
news:3o************@individual.net...

"Robert Potthast" wrote:
Now I still have the problem to distinguish between
stack and heap allocated objects

Check whether the "this" value is inside
stack range. It can be done portable and quickly.


This might be possible with some implementations,
but it's certainly not portable. (The language
makes no requirement that a 'stack' be used at
all).

-Mike
Sep 5 '05 #5

"Pavel Vozenilek" <pa*************@yahoo.co.uk> wrote in message
news:3o************@individual.net...

"Walter Bright" wrote:
"Robert Potthast" wrote:

> Now I still have the problem to distinguish between
> stack and heap allocated objects
>
Check whether the "this" value is inside
stack range. It can be done portable and quickly.


It's easy to find the top of the stack, but I know of no portable way to
find the bottom of it. If I've missed something, please let me know!


OK, I assumed garbage collection kicks in after main()
is entered and the current-top-of-stack at the moment
is stored as global variable.


That only partially works. There are the issues of the code executed before
main(), if you are running from a DLL then you have no idea where the stack
started out, and issues of the bottom of the stack in multithreaded code.
Sep 5 '05 #6

"Mike Wahler" <mk******@mkwahler.net> wrote in message
news:1b***************@newsread3.news.pas.earthlin k.net...

"Pavel Vozenilek" <pa*************@yahoo.co.uk> wrote in message
news:3o************@individual.net...

"Robert Potthast" wrote:
Now I still have the problem to distinguish between
stack and heap allocated objects

Check whether the "this" value is inside
stack range. It can be done portable and quickly.


This might be possible with some implementations,
but it's certainly not portable. (The language
makes no requirement that a 'stack' be used at
all).


True, but are there any implementations without a stack? Is there any point
to being concerned about them?
Sep 5 '05 #7

Pavel Vozenilek wrote:
"Robert Potthast" wrote:
Now I still have the problem to distinguish between
stack and heap allocated objects

Check whether the "this" value is inside
stack range. It can be done portable and quickly.


What if the object is on another thread's stack? How do you find all
thread's stack address ranges?

Sep 5 '05 #8
On Mon, 05 Sep 2005 00:53:32 +0400, Robert Potthast <ro**@spymac.com>
wrote:

I think that there is no reason to use smart pointers and garbage
collecting for stack objects, because stack object exists only in block
in which it's declared. You may use some tricky methods, but that methods
are slow and potentially unsafe.

ptr<A> a_ptr;

void g(ptr<A> a) {
a_ptr = a;
}

void f() {
A a;
g(a);
}

void main() {
f();
a_ptr->some_func(); // crash!
}
Sep 5 '05 #9
Well, this is what I am actually trying to archive - to disable smart
pointers and GC for stack objects. Another possible solution would be
to manually "mark" the objects as heap allocated, after constructing
them. Maybe by using a custom allocation function - if C++ would give
the programmer the ability to call the constructor manually :/

Sep 5 '05 #10
OK, I have learned something: Keep It Simple, Stupid

class Object
{
/* this and that */
void addToHeapObjects()
{
/* add object to list of collectable objects */
}
};

And to make it even simpler:

template < class T >
T* heap(T* obj)
{
obj->addToHeapObjects();
return obj;
}
void main()
{
Object* heapObj = heap(new Object);
Object stackObj;
}

That is it. Perfect. Well at least I now have a clean GC and
smart-pointer interface :)

Sep 5 '05 #11

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

Similar topics

3
by: Ian Taite | last post by:
Hello, I'm exploring why one of my C# .NET apps has "high" memory usage, and whether I can reduce the memory usage. I have an app that wakes up and processes text files into a database...
8
by: Martin Maat | last post by:
I am puzzled. I have this object that uses a thread. The thread is encapsulated by the object, the object has Start and Stop methods to enable the client to start or stop the thread. I found...
11
by: Fabien Penso | last post by:
Hi. I am trying to make this work but I got a weird behavior. I got a very basic system, I call a unmanaged "dllimported" function and give it a structure of callback functions. Sometimes,...
9
by: Olivier Fermy | last post by:
I have created a sample project where i have referenced an object only with an event : textBox.VisibleChanged += new EventHandler(this.textBox_VisibleChanged); When i call GC.Collect(), the...
15
by: vk02720 | last post by:
Hi, I am trying to implement garbage collection for C/C++ mainly for learning purpose. I dont know where to start. For example - Basic questions - How do I identify the variables that contain...
8
by: Paul.Lee.1971 | last post by:
Hi everyone, A program that I'm helping to code seems to slow down drastically during initialisation, and looking at the profiling graph, it seems to be the garbage collector thats slowing things...
9
by: amygdala | last post by:
Hi, I've read a few threads in this group recently, discussing garbage collecting and unsetting variables and the likes. Concerning memory usage in PHP: what is good practice, i.e. what should I...
56
by: Johnny E. Jensen | last post by:
Hellow I'am not sure what to think about the Garbage Collector. I have a Class OutlookObject, It have two private variables. Private Microsoft.Office.Interop.Outlook.Application _Application =...
3
by: from.future.import | last post by:
Hi, I encountered garbage collection behaviour that I didn't expect when using a recursive function inside another function: the definition of the inner function seems to contain a circular...
0
by: DolphinDB | last post by:
Tired of spending countless mintues downsampling your data? Look no further! In this article, you’ll learn how to efficiently downsample 6.48 billion high-frequency records to 61 million...
0
by: ryjfgjl | last post by:
ExcelToDatabase: batch import excel into database automatically...
1
isladogs
by: isladogs | last post by:
The next Access Europe meeting will be on Wednesday 6 Mar 2024 starting at 18:00 UK time (6PM UTC) and finishing at about 19:15 (7.15PM). In this month's session, we are pleased to welcome back...
0
by: Vimpel783 | last post by:
Hello! Guys, I found this code on the Internet, but I need to modify it a little. It works well, the problem is this: Data is sent from only one cell, in this case B5, but it is necessary that data...
0
by: ArrayDB | last post by:
The error message I've encountered is; ERROR:root:Error generating model response: exception: access violation writing 0x0000000000005140, which seems to be indicative of an access violation...
1
by: PapaRatzi | last post by:
Hello, I am teaching myself MS Access forms design and Visual Basic. I've created a table to capture a list of Top 30 singles and forms to capture new entries. The final step is a form (unbound)...
1
by: Defcon1945 | last post by:
I'm trying to learn Python using Pycharm but import shutil doesn't work
0
by: af34tf | last post by:
Hi Guys, I have a domain whose name is BytesLimited.com, and I want to sell it. Does anyone know about platforms that allow me to list my domain in auction for free. Thank you
0
by: Faith0G | last post by:
I am starting a new it consulting business and it's been a while since I setup a new website. Is wordpress still the best web based software for hosting a 5 page website? The webpages will be...

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.