Recent

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

nikel

  • Jr. Member
  • **
  • Posts: 98
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: 1229
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: 1504
    • 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

  • Jr. Member
  • **
  • Posts: 98
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: 1483
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 1.8.4 & 2.0.2 w/FPC 3.0.4 on:
(K|L)Ubuntu 12..16, Windows XP SP3, various DOSes.

giahung1997

  • Jr. Member
  • **
  • Posts: 89
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

  • Sr. Member
  • ****
  • Posts: 438
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?
Many major changes - Version has been changed to 3.0!
Crazy unit testing
Lack of multiple inheritance turns it into abomination.

Thaddy

  • Hero Member
  • *****
  • Posts: 7893
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 »
Hamlet 1.4 (nothing wrong with the Danish, btw)

marcov

  • Global Moderator
  • Hero Member
  • *****
  • Posts: 7007
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

  • Global Moderator
  • Hero Member
  • *****
  • Posts: 7007
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

  • Jr. Member
  • **
  • Posts: 89
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

  • Jr. Member
  • **
  • Posts: 89
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   >:(