Kernel SDK Developer's Guide > Basic Operations > Memory Management
Memory Management

In ODA products before version 4.3.0 memory allocation was implemented through global new and delete operators overrides. These operators were defined in TD_Alloc module and were exported from it to each ODA product. Such approach allowed to provide memory leaks catch functionality and custom reimplementation of memory management.

But this way of memory management implementation is incorrect for several reasons:

  1. C++ standard mismatch: according the recent draft of C++17 Standard, each program (consisting of one or more translation units) should provide its own memory management operators overrides.
  2. Undefined behavior when program has more than one global allocation or deallocation operators overrides (for example, two or more overrides of global operator new are defined).
  3. Problems with random crashes, mismatch of allocation and delete operations caused by ODA products, the ODA members experienced with.
  4. Problems with using third-party memory leak detectors with ODA modules.

To resolve problems, listed above, since Teigha 4.3.0 the following changes in memory management mechanism have been done:

  1. New allocation and deallocation operators (new, delete, new[] and delete[]) have been defined and added to .tx and .txv modules of ODA products. It allows to create new extensions for applications based on ODA products without redefining these operators. Please remember, however, that it is necessary to link TD_Alloc module (it provides odrxAlloc(), odrxRealloc() and odrxFree() functions) to your .tx or .txv module.
  2. Memory management new and delete operators have been defined for each ODA module independently due to match C++ standard.

These changes mean that ODA library modules now can be independent from TD_Alloc module (except cases when odrxAlloc() / odrxFree() functions are called directly).

Please remember, that all changes mentioned above have been done for dynamic configurations.

TD_Alloc module still provides odrxAlloc() and odrxFree() functions for memory leak detecting and other memory management purposes, but does not override global new and delete operators. If you create an ODA platform extension module and TD_Alloc module is not added to the dependencies, linker generates an error. For other module types TD_Alloc module should be added to the module dependencies list. File OdAllocOp.cpp located in /Kernel/Extentions/alloc folder should be added to the project. Please, note that if you use ODAProjectGenerator script (for non-Windows platform) or executable (for Windows) platform, you do no need to add OdAllocOp.cpp file to your project, ODAProjectGenerator adds it automatically.

All heap memory allocation/deallocation operations are made through calling one of the following functions:

      extern ALLOCDLL_EXPORT void* odrxAlloc   (size_t  nBytes); 
      extern ALLOCDLL_EXPORT void* odrxRealloc (void* pMemBlock, size_t newSize, size_t oldSize);
      extern ALLOCDLL_EXPORT void  odrxFree    (void* pMemBlock);
    

These functions are declared in /Kernel/Include/OdAlloc.h.

Alternately, a client can define custom versions of these functions and link them with own application. ODA   libraries will use the provided custom implementation of these functions, giving the client application full control of ODA heap allocation.  This enables ODA software to use the same memory management as the client application.

See ExAlloc sample application for illustrating memory management customization.

Copyright © 2002 – 2020. Open Design Alliance. All rights reserved.