Recent

Author Topic: [solved] Program freezes on close, after call a C function.  (Read 409 times)

BlueIcaro

  • Hero Member
  • *****
  • Posts: 621
[solved] Program freezes on close, after call a C function.
« on: October 23, 2020, 04:39:21 pm »
Hello, I make a binding of a C library (in linux).
When I close my program, it freeze, and when I use stop in lazarus (ctrl+f2), the programs stops, but I have a 'External: SIGSEGV'.  exception.
my code is this:
Code: [Select]
var
  sr: GevStatus;
  h: GEV_CAMERA_HANDLE;

begin
  new(h);   //need this?
  sr := GevOpenCameraByName(PChar('S1233719'), GevExclusiveMode, h);
  ShowMessage(IntToStr(sr));
  Dispose(h);
end;       
The  functions GevOpenCameraByName returns 0, means that found a camera. So my program finds the .so file.
The c translation in this:
Code: [Select]
  gevlib = 'libGevApi.so';   

//typedef void* GEV_CAMERA_HANDLE;
type
  GEV_CAMERA_HANDLE = ^integer;     

(...)
//=======================================
// Public Access Mode Value Definitions
//=======================================
{ enum definitions....
typedef enum
{
GevMonitorMode = 0,
GevControlMode = 2,
GevExclusiveMode = 4
} GevAccessMode; }

type
  GevAccessMode =
    (
    GevMonitorMode = 0,
    GevControlMode = 2,
    GevExclusiveMode = 4);   


function GevOpenCameraByName(Name: PChar; mode: GevAccessMode;
  handle: GEV_CAMERA_HANDLE): GevStatus; cdecl; external gevlib; 

I think the problem is on GEV_CAMERA_HANDLE declaration, because I read this post: [url]https://forum.lazarus.freepascal.org/index.php/topic,49781.0.html[/ur], but I'm don't how to convert the definition. Also my be there is some wrong code.

Any idea?

Thanks in advance
P.D. Program running in lazarus 2.10 and ubuntu 18.04 LTS
/BlueÏcaro
« Last Edit: October 26, 2020, 11:52:39 am by BlueIcaro »
Remenber, the lazarus wiki is your friend: http://wiki.lazarus.freepascal.org/Main_Page
General questions (several lenguages) http://wiki.lazarus.freepascal.org/

Jonas Maebe

  • Hero Member
  • *****
  • Posts: 805
Re: Program freezes on close, after call a C function.
« Reply #1 on: October 23, 2020, 04:53:39 pm »
The declaration of the function you want to call is
Code: [Select]
GEV_STATUS GevOpenCameraByName (char *name, GevAccessMode mode, GEV_CAMERA_HANDLE *handle);
Since GEV_CAMERA_HANDLE is declared as "void*" (= pointer), the handle parameter is actually "void **handle" and hence is a pointer to a pointer or ppointer in Pascal. You then have to pass the address of a pointer variable. Alternatively, you can declare that parameter as "out handle: pointer" and just pass the pointer variable (without taking its address). That call will then store a pointer in your pointer variable.

You must not allocate and definitely not free that pointer parameter, as it gets allocated/initialised/freed by the library (freed probably only if you call the appropriate "close" function afterwards).
« Last Edit: October 23, 2020, 04:56:34 pm by Jonas Maebe »

MarkMLl

  • Hero Member
  • *****
  • Posts: 1463
Re: Program freezes on close, after call a C function.
« Reply #2 on: October 23, 2020, 04:54:25 pm »
Noting Jonas's comment but also what widget set? There's a known issue with Qt raising an exception on termination.

MarkMLl
Turbo Pascal v1 on CCP/M-86, multitasking with LAN and graphics in 128Kb.
Pet hate: people who boast about the size and sophistication of their computer.

jamie

  • Hero Member
  • *****
  • Posts: 3797
Re: Program freezes on close, after call a C function.
« Reply #3 on: October 23, 2020, 05:15:03 pm »
and u don't use "dispose" on external items
The only true wisdom is knowing you know nothing

BlueIcaro

  • Hero Member
  • *****
  • Posts: 621
Re: Program freezes on close, after call a C function.
« Reply #4 on: October 23, 2020, 05:40:25 pm »
Hi I change the C function declaration, now is like this:
Code: [Select]
//typedef void* GEV_CAMERA_HANDLE;
type
  GEV_CAMERA_HANDLE = PPointer;

//GEV_STATUS GevOpenCameraByName( char *name, GevAccessMode mode, GEV_CAMERA_HANDLE *handle);
function GevOpenCameraByName(Name: PChar; mode: GevAccessMode;
 out handle: GEV_CAMERA_HANDLE): GevStatus; cdecl; external gevlib;

//GEV_STATUS GevCloseCamera(GEV_CAMERA_HANDLE *handle);
function GevCloseCamera(out GEV_CAMERA_HANDLE: GEV_CAMERA_HANDLE): GevStatus;
  cdecl; external gevlib; 
And I made a new code.
Code: [Select]
  private
     h: GEV_CAMERA_HANDLE;
(...)

procedure TForm1.Button1Click(Sender: TObject);
var
  sr: GevStatus;

begin
  sr := GevOpenCameraByName(PChar('S1233719'), GevExclusiveMode, h);  //the handle is global
  ShowMessage(IntToStr(sr));
  sr := GevCloseCamera(h);
  ShowMessage(IntToStr(sr)); //
end;

procedure TForm1.Button2Click(Sender: TObject);
var
  sr: GevStatus;
begin
  sr := GevApiUninitialize;
  ShowMessage(IntToStr(sr));    // show 0, ok!
end;   

After some test, I found that if I open a connection to camera (GevOpenCameraByName), I must use close it (GevCloseCamera). If not my program  freeze, and sometimes after press ctrl+f2 (stop), debugger ask for a file called malloc.c, I think is traying to remove the Handle from memory.

Now every is working.

Thanks every body

/BlueIcaro
Remenber, the lazarus wiki is your friend: http://wiki.lazarus.freepascal.org/Main_Page
General questions (several lenguages) http://wiki.lazarus.freepascal.org/

Jonas Maebe

  • Hero Member
  • *****
  • Posts: 805
Re: Program freezes on close, after call a C function.
« Reply #5 on: October 23, 2020, 05:47:23 pm »
Hi I change the C function declaration, now is like this:
Code: [Select]
//typedef void* GEV_CAMERA_HANDLE;
type
  GEV_CAMERA_HANDLE = PPointer;
This has to be "Pointer". Although since it's an opaque type, it won't matter in practice.

Quote
Code: [Select]
//GEV_STATUS GevCloseCamera(GEV_CAMERA_HANDLE *handle);
function GevCloseCamera(out GEV_CAMERA_HANDLE: GEV_CAMERA_HANDLE): GevStatus;
  cdecl; external gevlib; 
This has to be "var handle: GEV_CAMERA_HANDLE", not "out", since this procedure expects that the handle has already been initialised. It will work correctly even with "out", but the compiler won't be able to give a hint if you pass an uninitialised variable in that case.
« Last Edit: October 23, 2020, 08:09:51 pm by Jonas Maebe »

BlueIcaro

  • Hero Member
  • *****
  • Posts: 621
Re: Program freezes on close, after call a C function.
« Reply #6 on: October 23, 2020, 07:50:44 pm »
Thanks @Jonas Maebe for the info. Until monday I can not test it. As soon I get the test, I going to post the results

/BlueIcaro
Remenber, the lazarus wiki is your friend: http://wiki.lazarus.freepascal.org/Main_Page
General questions (several lenguages) http://wiki.lazarus.freepascal.org/

BlueIcaro

  • Hero Member
  • *****
  • Posts: 621
Re: Program freezes on close, after call a C function.
« Reply #7 on: October 26, 2020, 11:52:21 am »
Hello, after make the changes the problem is solved
Thanks

/BlueIcaro
Remenber, the lazarus wiki is your friend: http://wiki.lazarus.freepascal.org/Main_Page
General questions (several lenguages) http://wiki.lazarus.freepascal.org/

 

TinyPortal © 2005-2018