gidesa / ocvWrapper46

Opencv C++ API wrapper for Delphi, Lazarus/Freepascal and C - for Opencv v 4.6
GNU General Public License v3.0
61 stars 12 forks source link

Enhancement: Delayed loading DLL & Memory Management redirection #2

Closed hafedh-trimeche closed 1 year ago

hafedh-trimeche commented 1 year ago

Hi,

Would you please modify static loading DLL using delay loading mechanism?

 function   pCvRedirectException;         external ocvWrapper  name 'pCvRedirectException';
change to
 function   pCvRedirectException;         external ocvWrapper  name 'pCvRedirectException' delayed;

Any routines to redirect calls related to allocation and freeing Memory to detect Memory Leaks? The old implementation integrates this Manager: procedure cvSetMemoryManager(alloc_func: TCvAllocFunc = nil; free_func: TCvFreeFunc = nil; userdata: Pointer = nil); cdecl; external core_lib{$IFDEF DELAYEDLOADLIB} delayed{$ENDIF};

Best regards.

gidesa commented 1 year ago

Hello, for you kindly requests: 1- "delayed" is only for Delphi, until now I has maintained compatibility with Freepascal. Could add around 4000 conditionals, but it's not a clean solution in my opinion. By the way, during testing I find useful that Delphi check if every external function is found in the DLL. What you think is the advantage to have delayed definitions? 2- Sorry, the last versions of Opencv do not support anymore custom memory manager. This is because C++ is in charge of all memory operations. If you want to check memory allocated in wrapper DLL, or also in Opencv, you need a C++ tool. As a useful (but perhaps rough) solution, I use Process Explorer. This tool can visualize the total memory usage of a Windows process, so you can see if the total is stable or always increasing. I recommend to carefully check the call to delete function for every PCvxxx_t, either created by you or returned by some Opencv function/method. For PCvMat_t you can use the CvMatAuto function, from AutoDestroy unit, to assure the deletion of object when exiting from scope (that is, the current Delphi procedure of function). Regards

hafedh-trimeche commented 1 year ago

Hi, DLLs will be embedded to the executable application, they will be copied to an isolated location and loaded after setting the path to new location, so they need to be dynamically loaded or use the delayed mechanism. Code would be replaced: function pCvRedirectException; external ocvWrapper name 'pCvRedirectException' {$IFNDEF FPC}delayed{$ENDIF}; Best regards.

hafedh-trimeche commented 1 year ago

Hi,

An example for face recognition (matching) given score of similarity between two images? https://github.com/opencv/opencv/blob/4.x/samples/dnn/face_detect.cpp

Best regards.

gidesa commented 1 year ago

Hi, yes, the example find the faces in image (detection part), and then compare them to assign identity (recognition part). Here a detailed explanation: https://docs.opencv.org/4.x/d0/dd4/tutorial_dnn_face.html

hafedh-trimeche commented 1 year ago

Hi Delphi translation?

Regards

gidesa commented 1 year ago

Maybe in future releases I will add this interesting example translated to Delphi. Regards

gidesa commented 1 year ago

Hello, just released a new version of library, with fixes for your requests: delayed externs, fix for 0/nil compiler message, toarray/fromarray for simple types vectors (int, char, ecc.), a ready to use PCvStringEmpty to pass empty string in args, and some others. Regards

gidesa commented 1 year ago

Released new version. Closed

hafedh-trimeche commented 1 year ago

Hello,

Please note that delayed keyword would not handled as specified:

{$IFDEF  DEBUG}
  {$IFDEF DEBUGDLL}
     {$INCLUDE 'debug_DLL_dir.inc'}
  {$ELSE}
     DLL_DIR = ''  delayed;
  {$ENDIF}
{$ELSE}
  DLL_DIR = ''  delayed;
{$ENDIF}
[dcc64 Error] OPENCVWrapper.pas(50): E2029 ';' expected but identifier 'delayed' found
[dcc64 Error] OPENCVWrapper.pas(57): E2003 Undeclared identifier: 'ocvWrapper'
[dcc64 Error] OPENCVWrapper.pas(57): E2029 ';' expected but identifier 'delayed' found
[dcc64 Fatal Error] OPENCVWrapper.pas(62): F1026 File not found: 'unOcvWrapper_const.pas'

It should be handled as: Procedure pCvVectorucharToArray; external ocvWrapper name 'pCvVectorucharToArray' {$INDEF FPC} delayed{$ENDIF}; or Simply: Procedure pCvVectorucharToArray; external ocvWrapper {$INDEF FPC} delayed{$ENDIF};

Using the old code this exception is generated: Could not load pCvVectorucharToArray from ocvCPPWrapper46x64

gidesa commented 1 year ago

Hello, thak you for pointing on issue. Released the fixed unit in delayed folder. About pCvVectorucharToArray, I had verified it's in the exported symbols. You should overwrite old DLL with the new version, either from repository Debug or (much faster) Release folder. Regards

hafedh-trimeche commented 1 year ago

Hi,

Please note that pCvStringDelete(pCvStringEmpty) should not be included into finalization process!

pCvStringEmpty is just a pointer to a structure and not created using pCvStringCreate.

Best regards.

gidesa commented 1 year ago

You help is invaluable, thank you. It's right, that variable must not be deleted. Strange enough, nor compiler, neither runtime execution, complains about this. Released fix. Regards

hafedh-trimeche commented 1 year ago

Hello,

Please note that runtime error is generated (DLL not found) if ocvWrapper\Delphi\delayed directory is located after ocvWrapper\Delphi one inside library search list. So it would be better to maintain the same code using {$INDEF FPC} delayed{$ENDIF} conditional define instead of duplicating units.

Best regards.

gidesa commented 1 year ago

Hello, I released a new version of wrapper. Now there is no more a separated "delayed" directory. Instead inside OpencvWrapper.pas there is a specific define switch DELPHIDELAYED that you could use. You could also interested in the 4 VCL components, one of them is a face detector, can also work as face recognizer. Please read the readme.md file in Delphi-components for more informations. Regards

hafedh-trimeche commented 1 year ago

Hi,

Good Job!

I'm already testing these enhancements.

Best regards.