Code Monkey home page Code Monkey logo

delphi-garbage-collector's Introduction

Delphi-Garbage-Collector

Description

An attempt at automating the process of releasing objects in Delphi without the need to use Try Finally blocks to free objects after their usage.

It utilizes a main class to collect objects created during usage and release them upon the destruction of the main object (for example, a TForm). It also allows using TAutoCollect, which is an interfaced class that enables the creation of simple to complex classes to be freed upon exiting the execution scope, similar to how interfaces work.

The idea behind this project is not to require the programmer to migrate their classes to an interface and to improve memory management. With the TAutoCollect class, it's possible to turn a simple class into something like a interface without implementing anything new.

Pending features

-> Allow the TGargabeCollector to create objects for a specific 'owner' and enable releasing only the objects created for that owner

Instalation

Add the folder where uGarbageCollector.pas is located to the Delphi library path or to your project's search path.

Contribution

If you believe you can help improve this repository, please submit a PR, and I will review it. Any assistance is welcome.

Delphi versions

  • Tested in Delphi 11.0

I recommend using Delphi XE or better.

Usage

Object

uses uGarbageCollector

...

var
  LTest: TStringList;
begin
 LTest:= Garbage.Add<TStringList>(TStringList.Create);
 ...
 ...
 ...
 ... 
end;

LTest will be released when Garbage is destroyed

var
  LTest: TStringList;
begin
  LTest:= Garbage.Add<TStringList>(TStringList.Create);
  try
    LTest.Add('This');
    LTest.Add('is a');
    Ltest.Add('test with collect');
    Showmessage(LTest.Text);
  finally
    Garbage.Collect(LTest);
  end;
end;

With input parameters in the create of the class to be collected

begin
  FTestClass:= Garbage.Add<TMyClass>(TMyClass.Create('with input params'));
end;

Example of how to use the TAutoCollect class

var
  LLocal: TMyClass;
begin
  LLocal:= TAutoCollect<TMyClass>.New(TMyClass.Create).GetInstance;
  LLocal.FParam:= 'test'; 
  Showmessage(LLocal.FParam);  
  
 //LLocal its destroyed here
end;

In the 'sample' folder, there are more usage examples

delphi-garbage-collector's People

Contributors

danieldavilaids avatar daviladanielc avatar

Stargazers

 avatar  avatar  avatar  avatar  avatar  avatar  avatar

Watchers

 avatar  avatar

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.