Delphi Memory Management - The Book / Contact

Delphi Memory Management

Please select your option

Memory management. One of the most basic parts of software development, often kept on the side even though it has the most profound effect on how we write our code.

Delphi provides a variety of types with their own memory management logic, as well as two sets of compilers that provide different memory management systems for classes.

* Classic Delphi compiler currently supported on Windows and OSX platforms - using manual memory management while providing ARC for certain types.

* Next generation ARC Delphi compiler supported on mobile Android and iOS platforms, as well as Linux - using full ARC - Automatic Reference Counting memory management system.

Each memory management system has its good and bad sides. Each offers solutions to some problems, but creates a whole range of other problems. And each requires slightly different coding patterns and practices. Knowing the strengths and weaknesses and understanding how memory management system(s) work goes hand-in-hand with writing clean, bug-free and maintainable code.

Both compilers will be covered in detail, as well as coding patterns required for writing cross-compiler code that must run under both.

From manual memory management, to garbage collection, different memory management systems differ not only by the general category they fall in, but also by implementation. And all those fine implementation details also have a great impact on actual code. From the perspective of the everyday software development process discussing memory management is impossible without discussing its specific implementation in specific languages and toolsets.

Table of Contents:


Clean Code

Part 1. Memory management basics and terminology

* Short overview of memory management models

* How memory works

   + Overview
   + Initialization and instantiation
   + The stack, the heap, and the differences between them

* Variables and types

   + Value types, pointers and references, unmanaged and managed types
   + Automatic initialization of variables

* Invalid pointers or references - wild, dangling, stale

* Scope and lifetime

* Classes

* Resource management - RAII

Part 2. Object instances

* To be, or not to be

   + Nil - The Billion Dollar Mistake
   + Passing nil, returning nil
   + Nil (Null) Object Pattern
   + Nullable types  

* Ownership

* Object Instance Lifecycle

   + Lifecycle diagram
   + Difference in object instance lifecycle between classic and ARC compiler 
   + Exceptions and exception handling in lifecycle methods   
   + Creating and destroying an object instance
   + Overview of object lifecycle methods

* Lifecycle Coding Patterns

   + Constructors and destructors
   + `AfterConstruction` and `BeforeDestruction`
   + Adding the `override` directive
   + Calling the `inherited`
   + Class constructors and destructors
   + Object construction through metaclasses
   + Object factories
   + Object factory with metaclass
   + OnCreate, OnDestroy and similar patterns
   + Deep copy - `Assign` method 

Part 3. Manual memory management

* Releasing object instances

   + Methods for releasing object instances 
    - Destroy
    - Free
    - FreeAndNil

   + Releasing through ownership models
    - Collections with ownership
    - Handling exceptions during ownership transfer
    - TComponent ownership
    - TComponent notification system

Part 4. ARC memory management

* Automatic Reference Counting - Overview

   + Golden Rule of ARC
   + Strong & weak references - [weak], [unsafe], pointer 
   + Ownership
   + Strong reference cycles 

* Automatic Reference Counting in Delphi

   + ARC in classic compiler - Interfaces
   + Delphi NextGen ARC compiler

* Automatic Reference Counting - Concepts

   + How the reference counting mechanism works
   + Reference counting triggers 
   + Forming of a reference cycle
   + When is it safe to use `[unsafe]`?

* DisposeOf - ARC with a twist

   + `DisposeOf` in the context of ARC memory management
   + `DisposeOf` and object instance lifecycle
   + `Disposed` property
   + Why does DisposeOf exist in Delphi ARC compilers?
   + TComponent notification system and ARC
   + Pitfalls of DisposeOf

* Interfaces in the classic compiler

   + Mixing object and interface references
   + Disabling reference counting
   + Chameleon class
   + Memory leak on `const` parameter
   + Creating a fake interface
   + Interface list

* Storing weak and unsafe references in collections

* Anonymous methods

Part 5. Coding patterns

* Smart Pointers

* Lazy

* Weak


* References

* Quality Portal Reports


Copyright © Prasnikar.info / Contact