Board index » delphi » Any memory allocation or Garbage Collection ideas available?

Any memory allocation or Garbage Collection ideas available?

I'm wondering if anyone is doing anything WRT Garbage Collection in Delphi.
Anyone reference counting, or anything else? How does it work for you, etc.

Thanx for any pointers.

Will Hartung
(wi...@msoft.com)

 

Re:Any memory allocation or Garbage Collection ideas available?


Have a look at TComponent.Create and TComponent.Destroy for one
possibility. In order to do more powerful garbage collection, you'd
probably need to modify the compiler.

MH.

Quote
Will Hartung wrote:

> I'm wondering if anyone is doing anything WRT Garbage Collection in Delphi.
> Anyone reference counting, or anything else? How does it work for you, etc.

> Thanx for any pointers.

> Will Hartung
> (wi...@msoft.com)

--
Martin Harvey.
Totally rewritten web pages at:
http://www.harvey27.demon.co.uk/mch24/

"ALGOL 60 was a language so far ahead of its time that it
was not only an improvement on its predecessors but also
on nearly all its successors". C.A.R. Hoare

--------------BEGIN GEEK CODE BLOCK--------------
Version: 3.12
GCS/CC d(+) s-:- a-- C+++$ UL@ P L@>++ E- W++
N+++ o-- K++ w+++$ O--- M-- V-- PS@ Y-- PGP-
t--- 5-- X-- R-- !tv b+ DI+ D+ G e++ h- r z++>---
---------------END GEEK CODE BLOCK---------------

Re:Any memory allocation or Garbage Collection ideas available?


On Sat, 09 Jan 1999 16:59:49 +0000, Martin Harvey

Quote
<mar...@aziraphale.demon.co.uk> wrote:
>Have a look at TComponent.Create and TComponent.Destroy for one
>possibility. In order to do more powerful garbage collection, you'd
>probably need to modify the compiler.

Delphi 4 has a class called TMemoryManager and you can inherit from it
and replace the standard memory manager that Delphi uses, so you
*could* probably add some garbage collection to your program.

I've used the replace technique to create a leak-detection unit.

Re:Any memory allocation or Garbage Collection ideas available?


In article <36978B05.5AE87...@aziraphale.demon.co.uk>,
  Martin Harvey <mar...@aziraphale.demon.co.uk> wrote:

Quote
> Will Hartung wrote:
> > I'm wondering if anyone is doing anything WRT Garbage Collection in Delphi.
> > Anyone reference counting, or anything else? How does it work for you, etc.

> > Thanx for any pointers.

Searching for a way to simulate native types (ie. such as Integers and
Strings) I discovered that Object Interfaces (Object Pascal extension in
v3.0) implements referencing counting for objects accessed entirely through
their interfaces using TInterfacedObject (default implementation available
for the minimum Interface IUnknow).

I am still learning Delphi and am trying to convert my current class to be a
interface implementation. Then in the rest of my code I can manage it as an
interface reference and benefit from reference counting.

The problem is (there is always a problem), I don't know how to create the
instantiated interface supporting object. Classes have constructors.
Interfaces don't. There seems to be some compatibility between objects and
interfaces, but I haven't figured it out yet.

Has anyone travelled down this road before?

-----------== Posted via Deja News, The Discussion Network ==----------
http://www.dejanews.com/       Search, Read, Discuss, or Start Your Own    

Re:Any memory allocation or Garbage Collection ideas available?


Quote
gden...@my-dejanews.com writes:

> In article <36978B05.5AE87...@aziraphale.demon.co.uk>,
>   Martin Harvey <mar...@aziraphale.demon.co.uk> wrote:
> > Will Hartung wrote:
> > > I'm wondering if anyone is doing anything WRT Garbage Collection in Delphi.
> > > Anyone reference counting, or anything else? How does it work for you, etc.

> > > Thanx for any pointers.

> Searching for a way to simulate native types (ie. such as Integers and
> Strings) I discovered that Object Interfaces (Object Pascal extension in
> v3.0) implements referencing counting for objects accessed entirely through
> their interfaces using TInterfacedObject (default implementation available
> for the minimum Interface IUnknow).

> I am still learning Delphi and am trying to convert my current class to be a
> interface implementation. Then in the rest of my code I can manage it as an
> interface reference and benefit from reference counting.

> The problem is (there is always a problem), I don't know how to create the
> instantiated interface supporting object. Classes have constructors.
> Interfaces don't. There seems to be some compatibility between objects and
> interfaces, but I haven't figured it out yet.

If you do this, I advise you not to use variables of the type of the
class (TCustomer in my example). You can have very tricky problems if
you mix reference counted variables and non-reference counted
variables. That's why Get_Name is protected : this way you are less
tempted to have a vairable of type TCustomer since you wouldn't be
able to use for anything.

ICustomer = interface
  ['aguid']     // only necessary if you want to use 'as' (QueryInyterface)
  function Get_Name : string;
  property Name : string read Get_Name;
end;

TCustomer = class(TInterfacedObject, ICustomer)
private
  FName : string;
protected
  funtion Get_Name :string;
public
  constructor Create(aName: string);
end;

        .
        .      
        .
procedure my_proc;
var
  aCustomer: ICustomer;
begin
  aCustomer := TCustomer.Create;
end;

Regards
Laurent

Re:Any memory allocation or Garbage Collection ideas available?


Quote
: gden...@my-dejanews.com wrote:
>The problem is (there is always a problem), I don't know how to create the
>instantiated interface supporting object. Classes have constructors.
>Interfaces don't. There seems to be some compatibility between objects and
>interfaces, but I haven't figured it out yet.

type
  MyInterface = interface
    [...]
  end;

type
  MyClass = class(TInterfacedObject, MyInterface)
    ...
  end;

var
  AnIntf: MyInterface;
begin
  AnIntf := MyClass.Create as MyInterface;
  // Whoops, and here we are out of scope, again...
end;

The "as MyInterface" part above is redundant and only for clarity.

Please have a look at the online help - that describes everything
neatly. In short, you can cast a class instance to an interface
instance (it's usually implicitly done), but you never can get back
from the interface instance to the class instance.

--
Stefan Hoffmeister    (http://www.econos.de/)
No private email, please, unless expressly invited.

Re:Any memory allocation or Garbage Collection ideas available?


Similarly, i don't have much experience with interfaces, but I think that
you have to have what's known as a "class factory" to instantiate the
interface.  Have a look in the help file, and it might explain it further.

Cameron
_______________________
cameronm...@hotmail.com

Quote
gden...@my-dejanews.com wrote in message

<77uso5$70...@nnrp1.dejanews.com>...
Quote
>In article <36978B05.5AE87...@aziraphale.demon.co.uk>,
>  Martin Harvey <mar...@aziraphale.demon.co.uk> wrote:
>> Will Hartung wrote:
>> > I'm wondering if anyone is doing anything WRT Garbage Collection in
Delphi.
>> > Anyone reference counting, or anything else? How does it work for you,
etc.

>> > Thanx for any pointers.

>Searching for a way to simulate native types (ie. such as Integers and
>Strings) I discovered that Object Interfaces (Object Pascal extension in
>v3.0) implements referencing counting for objects accessed entirely through
>their interfaces using TInterfacedObject (default implementation available
>for the minimum Interface IUnknow).

>I am still learning Delphi and am trying to convert my current class to be
a
>interface implementation. Then in the rest of my code I can manage it as an
>interface reference and benefit from reference counting.

>The problem is (there is always a problem), I don't know how to create the
>instantiated interface supporting object. Classes have constructors.
>Interfaces don't. There seems to be some compatibility between objects and
>interfaces, but I haven't figured it out yet.

>Has anyone travelled down this road before?

>-----------== Posted via Deja News, The Discussion Network ==----------
>http://www.dejanews.com/       Search, Read, Discuss, or Start Your Own

Re:Any memory allocation or Garbage Collection ideas available?


Quote
"Cameron McCormack" <cameronm...@hotmail.com> writes:

> Similarly, i don't have much experience with interfaces, but I think that
> you have to have what's known as a "class factory" to instantiate the
> interface.  Have a look in the help file, and it might explain it further.

You don't need this if all your objects will be instanciated
internally.

--
Laurent Martelli

Other Threads