Spelt / ZXing.Delphi

ZXing Barcode Scanning object Pascal Library for Delphi VCL and Delphi Firemonkey
Apache License 2.0
473 stars 206 forks source link

Fixed all memleask I found un Win32/Win64 #37

Closed csm101 closed 8 years ago

csm101 commented 8 years ago

Ok, Applying the usual trick of TInterfacedObject (i'm kinda starting to love the next-gen compilers too) i fixed all the memory leaks that were left.

The Memory leak I was experiencing was caused by the TStringObject class used to put strings (wrapped in TStringObject instances) inside FResultMetadata: TDictionary<TResultMetadataType, TObject>;

I started to redeclare this varible as TDictionary<TResultMetadataType, IMetaData>, creating different specialized interfaces of IMetaData depending on the type of data to be stored. Since TDictionary<TResultMetadataType, IMetaData> was used throughout the code, I preferred to declare a expliciy type for it and I called it TResultMetadata.

Going on with writing this fix I discovered that one of the classes that could be stored in metadata was an instance of TList<TArray>... which needed too to be declared as an interfaced object. I replaced this TList<TArray> with a specific interface (declared in its own unit) called IByteSegments.

This is a breaking change since now client code accessing TReadResult.metadata must be modified for handling IMetaData interface instances and not generic TObjects.

By the way the previous implementation was not that "safe" since:

  1. there was no way, at runtime, to know if some metadata was an integer or an actual object: you can't set an object pointer to "TObject(10)" and then write "if p is Integer": there is no "autoboxing" in old gen compilers... And I don't even know if there it is in new-gen compiler. under old-gen compilers that is a brutal blind cast from integer to pointer.
  2. In some parts of the code TStringObject was not used to wrap strings, so there were cases where was still done a brutal cast from string to TObject (see TUPCEANExtension2Support.parseExtensionString, for example)
Spelt commented 8 years ago

I can see why you start to love the new gen compilers:-)

V3 Breaks a lot. But it's for the best.