Recent

Author Topic: Help needed about freeing async functions and pointers  (Read 1942 times)

nikel

  • Full Member
  • ***
  • Posts: 186
Help needed about freeing async functions and pointers
« on: April 19, 2019, 03:37:41 pm »
Hi, how can I free this function:

Code: Pascal  [Select][+][-]
  1. procedure TForm1.RunUpdaterAsync(Data: IntPtr);
  2. begin
  3.   RunShellExecute('updatecheck.exe', '', 1);
  4.   Data:=0;
  5. end;
  6. ...
  7. Application.QueueAsyncCall(@RunUpdaterAsync, FCounter);

ASerge

  • Hero Member
  • *****
  • Posts: 2241
Re: Help needed about freeing async functions and pointers
« Reply #1 on: April 19, 2019, 05:19:33 pm »
Hi, how can I free this function:
What is "free"? Application.RemoveAsyncCalls(Self)?

avra

  • Hero Member
  • *****
  • Posts: 2514
    • Additional info
Re: Help needed about freeing async functions and pointers
« Reply #2 on: April 19, 2019, 06:19:37 pm »
I do not see from your example what should be freed. If you wanted to pass some data to a function called by QueueAsyncCall and later free it without leaking, then maybe you might want to check this where whole record is passed: https://forum.lazarus.freepascal.org/index.php/topic,38851.msg265181.html#msg265181
ct2laz - Conversion between Lazarus and CodeTyphon
bithelpers - Bit manipulation for standard types
pasettimino - Siemens S7 PLC lib

nikel

  • Full Member
  • ***
  • Posts: 186
Re: Help needed about freeing async functions and pointers
« Reply #3 on: April 19, 2019, 09:59:44 pm »
Sorry pointers make me frightened. I'm confused.

lucamar

  • Hero Member
  • *****
  • Posts: 4219
Re: Help needed about freeing async functions and pointers
« Reply #4 on: April 19, 2019, 11:37:25 pm »
Code: Pascal  [Select][+][-]
  1. procedure TForm1.RunUpdaterAsync(Data: IntPtr);

Shouldn't that be:
Code: Pascal  [Select][+][-]
  1. procedure TForm1.RunUpdaterAsync(Data: PtrInt);
???

As for how to free it (if it represents a pointer), that depends of what you pass to it but the most common ways are:
  • Using Dispose(PThePointerType(Data)) for "normal" types dynamically instanced;
  • Using PTheClass(Data)^.Free if it's a dynamic object; or
  • Leave it alone if it points to a global var (of any type).

Also, note that your "Data := 0" does absolutely nothing (from a logic point of view): since the parameter is neither var or out, all it does is set the local copy to zero.
« Last Edit: April 19, 2019, 11:40:53 pm by lucamar »
Turbo Pascal 3 CP/M - Amstrad PCW 8256 (512 KB !!!) :P
Lazarus/FPC 2.0.8/3.0.4 & 2.0.12/3.2.0 - 32/64 bits on:
(K|L|X)Ubuntu 12..18, Windows XP, 7, 10 and various DOSes.

giahung1997

  • Full Member
  • ***
  • Posts: 113
Re: Help needed about freeing async functions and pointers
« Reply #5 on: April 20, 2019, 06:52:15 am »
Sometimes I love C's int* more than Pascal PtrInt  :-X

Mr.Madguy

  • Hero Member
  • *****
  • Posts: 844
Re: Help needed about freeing async functions and pointers
« Reply #6 on: April 20, 2019, 08:23:45 am »
There is nothing to be freed here. Callback is static and passed as pointer. It's not object, like in script languages. Data can be freed, if it's object, but it isn't. May be you mean abandon async call? Or abandon executed application?
Is it healthy for project not to have regular stable releases?
Just for fun: Code::Blocks, GCC 13 and DOS - is it possible?

Thaddy

  • Hero Member
  • *****
  • Posts: 14367
  • Sensorship about opinions does not belong here.
Re: Help needed about freeing async functions and pointers
« Reply #7 on: April 20, 2019, 10:21:39 am »
Sometimes I love C's int* more than Pascal PtrInt  :-X
Yeah, wild card programming would be easy..... :-X

(Anyway, a bit more serious: release any objects in the thread it was created in the first place. That's the "law".)
« Last Edit: April 20, 2019, 10:30:12 am by Thaddy »
Object Pascal programmers should get rid of their "component fetish" especially with the non-visuals.

marcov

  • Administrator
  • Hero Member
  • *
  • Posts: 11447
  • FPC developer.
Re: Help needed about freeing async functions and pointers
« Reply #8 on: April 20, 2019, 12:31:31 pm »
Sometimes I love C's int* more than Pascal PtrInt  :-X

ptrint is the equal of C99 intptr_t, not of *int, iow an integer with the same size as pointer.

marcov

  • Administrator
  • Hero Member
  • *
  • Posts: 11447
  • FPC developer.
Re: Help needed about freeing async functions and pointers
« Reply #9 on: April 20, 2019, 12:35:53 pm »
There is nothing to be freed here. Callback is static and passed as pointer.

Callbacks are always pointers or records of pointers (in the case of nested or procedure of object). The @ here is a mode fpc/objfpc "feature" to disambiguate some rare cases. It doesn't really do anything then confirm you really want to pass that identifier as the callback .

The corner case is one you have a callback returning a procedure type of the same type (or compatible on some level) as the callback.

In mode delphi or TP you wouldn't use it.

giahung1997

  • Full Member
  • ***
  • Posts: 113
Re: Help needed about freeing async functions and pointers
« Reply #10 on: April 21, 2019, 04:32:24 pm »
Sometimes I love C's int* more than Pascal PtrInt  :-X
Yeah, wild card programming would be easy..... :-X

(Anyway, a bit more serious: release any objects in the thread it was created in the first place. That's the "law".)
Pascal is wordy and C is just compact. I love it simplicity until thing got complicated and need libraries. I hate underscores so much set_abc_xyz blah blah   >:(

giahung1997

  • Full Member
  • ***
  • Posts: 113
Re: Help needed about freeing async functions and pointers
« Reply #11 on: April 21, 2019, 04:35:08 pm »
Sometimes I love C's int* more than Pascal PtrInt  :-X

ptrint is the equal of C99 intptr_t, not of *int, iow an integer with the same size as pointer.
Ok. Sorry for my ignorant. The C we teach at school in my country is C89, fsck, people going to have C20   >:(

 

TinyPortal © 2005-2018