PostMessage Synchronization

I want to implement a synchronization mechanism to display data from threads using PostMessage procedure.

FastMM reported a memory leak related to Unicode String usage.

Please how to assign a string within a pointer structure ?

{code}
unit Main;
{$I Defines}
interface
uses
  Windows, Messages, SysUtils, Variants, Classes, Graphics, Controls, Forms,
  Dialogs, StdCtrls;
type
  TMainForm = class(TForm)
    SyncBtn: TButton;
    Label1: TLabel;
    procedure PointerClick(Sender: TObject);
    procedure FormCreate(Sender: TObject);
  private
    procedure SyncGUI(var Data:Pointer);
    procedure SyncGUIProc(var SyncMsg: TMessage);
  public
    procedure WndProc(var Message : TMessage);override;
  end;

var
  MainForm: TMainForm;

implementation
var
  SynchronizeDisplayMessage : LongWord;

{$R *.dfm}
type
  TRecord=
  record
    anInteger : Integer;
    aString   : string;
  end;

procedure DisplayRecord(out aRec);
var
  P  : Pointer;
  S  : string;
begin
  P           := @aRec;
  Integer(P^) := 5432;
  Inc(PByte(P),SizeOf(Integer));
  S := 'String to display';
  TRecord(aRec).aString := S;
(*
  Pointer(P^) := Pointer(S);
  MainForm.Label1.Caption := string(P^);
*)
end;

procedure TMainForm.PointerClick(Sender: TObject);
var
  aRec : ^TRecord;
begin
  New(aRec);
  SyncGUI(Pointer(aRec));
end;

procedure TMainForm.SyncGUI(var Data: Pointer);
var
  SyncMsg : TMessage;
begin
  SyncMsg.Msg    := SynchronizeDisplayMessage;
  SyncMsg.lParam := NativeInt(Data);
  SyncMsg.Result := 1;
  try
    PostMessage(Handle,SyncMsg.Msg,SyncMsg.WParam,SyncMsg.LParam);
  except
  end;
end;

procedure TMainForm.SyncGUIProc(var SyncMsg: TMessage);
var
  P : Pointer;
begin
  SyncMsg.Result := 1;
  P              := Pointer(SyncMsg.lParam);
  try
    DisplayRecord(P^);
    ShowMessage(TRecord(P^).aString);
  except
  end;
  if Assigned(P) then Dispose(P);
end;

procedure TMainForm.WndProc(var Message: TMessage);
begin
  try
    if Message.Msg = SynchronizeDisplayMessage then
    begin
      SyncGUIProc(Message);
      Exit;
    end;
    inherited WndProc(Message);
  except
  end;
end;

procedure TMainForm.FormCreate(Sender: TObject);
begin
  SynchronizeDisplayMessage   := RegisterWindowMessage(PChar('SynchronizeDisplayMessage'));
  ReportMemoryLeaksOnShutdown := True;
end;

end.
{code}


--------------------------------2012/4/15 22:28:37--------------------------------
A memory block has been leaked. The size is: 52

This block was allocated by thread 0xF3C, and the stack trace (return addresses) at the time was:
404416 
4075BF 
406BD0 
4C26B2 [Main.pas][Main][Main.DisplayRecord][44]
4ABB3D [Controls][Controls][Controls.TWinControl.WndProc]
4C27D8 [Main.pas][Main][Main.TMainForm.SyncGUIProc][79]
4C2845 [Main.pas][Main][Main.TMainForm.WndProc][91]
4AB1DB [Controls][Controls][Controls.TWinControl.MainWndProc]
4447DA [Classes][Classes][Classes.StdWndProc]
75AAC4E7 [Unknown function at gapfnScSendMessage]
75AAC5E7 [Unknown function at gapfnScSendMessage]

The block is currently used for an object of class: UnicodeString

The allocation number is: 648

--------------------------------2012/4/15 22:28:37--------------------------------
This application has leaked memory. The small block leaks are (excluding expected leaks registered by pointer):

37 - 52 bytes: UnicodeString x 1

Note: Memory leak detail is logged to a text file in the same folder as this application. To disable this memory leak check, undefine "EnableMemoryLeakReporting".
0
Hafedh
4/15/2012 9:38:15 PM
embarcadero.delphi.vcl.using 2297 articles. 1 followers. Follow

2 Replies
544 Views

Similar Articles

[PageSpeed] 6

Hafedh TRIMECHE wrote:

> I want to implement a synchronization mechanism to display data from
> threads using PostMessage procedure.
> 
> FastMM reported a memory leak related to Unicode String usage.
> 
> Please how to assign a string within a pointer structure ?

When using a Dispose on an untyped pointer it will only deallocate the
memory that the given pointer is pointing to. If you use Dispose on a typed
pointer is can also handle "sub-types" within that pointer (string in a record in
this case).

Based on your example, a wrote a new example:

{code}
interface

uses
  Windows, Messages, SysUtils, Variants, Classes, Graphics, Controls,
  Forms, Dialogs, StdCtrls;

type
  TRecord = record
    anInteger: Integer;
    aString: string;
  end;
  PRecord = ^TRecord;

  TMainForm = class(TForm)
    SyncBtn: TButton;
    Label1: TLabel;
    procedure FormCreate(Sender: TObject);
    procedure SyncBtnClick(Sender: TObject);
  private
    procedure SyncGUI(const Data: PRecord);
    procedure SyncGUIProc(var SyncMsg: TMessage);
  public
    procedure WndProc(var Message: TMessage); override;
  end;

var
  MainForm: TMainForm;

implementation

{$R *.dfm}

var
  SynchronizeDisplayMessage: LongWord;

procedure FillRecord(var aRec: TRecord);
begin
  aRec.anInteger := 5432;
  aRec.aString := 'String to display';
end;

procedure TMainForm.SyncBtnClick(Sender: TObject);
var
  aRec: PRecord;
begin
  New(aRec);
  FillRecord(aRec^);
  SyncGUI(aRec);
end;

procedure TMainForm.SyncGUI(const Data: PRecord);
begin
  PostMessage(Handle, SynchronizeDisplayMessage, 0, NativeInt(Data));
end;

procedure TMainForm.SyncGUIProc(var SyncMsg: TMessage);
var
  P: PRecord;
begin
  SyncMsg.Result := 1;
  P := PRecord(SyncMsg.lParam);
  if Assigned(P) then
  begin
    Label1.Caption := P^.aString + ' ' + IntToStr(P^.anInteger);
    Dispose(P);
  end;
end;

procedure TMainForm.WndProc(var Message: TMessage);
begin
  if Message.Msg = SynchronizeDisplayMessage then
    SyncGUIProc(Message)
  else
    inherited WndProc(Message);
end;

procedure TMainForm.FormCreate(Sender: TObject);
begin
  SynchronizeDisplayMessage := RegisterWindowMessage(PChar('SynchronizeDisplayMessage'));
  ReportMemoryLeaksOnShutdown := True;
end;

end.
{code}

-- 
Pieter

"Only a fool tests the depth of the water with both feet."
 -- African proverb
0
Pieter
4/15/2012 11:23:21 PM
Thank you.

A typed pointer must be used.
0
Hafedh
4/15/2012 11:43:05 PM
Reply:

Similar Artilces:

To use or not to use Delphi
Sadly, it seems to me that there is a sort of race between the two threads, for and against using Delphi in new projects, with more or less the same users posting in both threads. Arguments are fiercely debated in both camps. Borland had their own vision. As a community, now that Delphi has changed ownership I believe we should try to be more consistent, more clear, and more articulate in what we expect from Embercadero in terms of Delphi. We can contribute to keeping Delphi alive and moving in the right direction. "Laurent Cocea" schrieb: > Sadly, it seems to me that there ...

Spreadsheet component for use in Delphi VCL 2010
Good day I have been using the Flexcel component for reading and writing Excel spreadsheets in my Delphi VCL projects for some while and have been very happy. Unfortunately, though, a version for use with Excel 2007 format files (.XLSX) has been promised for years now but just never arrives and I have reached a stage where I just have to update some of my software. Can any body suggest alternatives and comment on their experience with these? I have no objection to buying a component although all my programs are for academic use by myself and colleagues/friends so cheaper would...

Delphi XE8 Send Notification from Parse using VCL app
Hello, I am learning the new Baas technology with XE8. I created a small app to receive notification on Android mobiles using Parse. Everything is working fine when the notification message is sent from Parse web site. However when my VCL app send the notification with BackendPush component, nothing is coming in mobile phone. The messages are sending correctly because I can see them on Parse site. Their status is different than the one sent from Parse . Here are my VCL messages Status on Parse: Type: API, target: Chanels. When sent on Parse site: Type : Campaign, Target: Everyone...

How to detect if code executes in a VCL application and needs to use Synchronize?
Hello newsgroup, this is my current logic in a thread to check if the 'HandleIncomingData' call needs to be synchronized: if System.IsConsole then begin HandleIncomingData; end else begin Synchronize(HandleIncomingData); end; It works fine, but only if the console application does not open VCL Forms. For example, I have a GUITestRunner unit test project which also uses APPTYPE CONSOLE to open a console window for debug output. In this case, System.IsConsole is True, but the Synchronize is necessary to avoid applica...

Actuall thread about vcl classes / components in C++ for use with delphi...
Hi All, I wanted to follow the topic that appears as a 'sub-thread' above... suspecting it would die... I'm sure many of us are interested in this topic. ( by 'sure' I mean absolutely certain...) It also brings to my mind the question, does anyone know of any references for us semi-delphi-literate C++ programmers that would like to read/write/modify delphi stuff? Thanks Everyone, (you know who you are... B) Bruce ...

Delphi 6
In preparation for applying a bug fix to one of the VCL modules (AxCtrls.pas), I have been trying to compile the original source code for the module in order to reproduce exactly the existing dcu. I have copied AxCtrls.pas to an empty directory, and have found that doing: dcc32 -q AxCtrls -$D- -$L- gives me a dcu which is identical in size to the one on Delphi6/Lib, with identical contents except for a few bytes near the beginning, which I assume are a compliation timestamp. However, I have been unsuccessful in duplicating the debug dcu in Delphi6/Lib/Debug. Using "-$D+ -$L+&q...

Using Delphi 7 and Delphi 2010
I had a project in Delphi 7. I worked on it in Delphi 2010. When I try to open the .dpr again in Delphi 7, the IDE toolbar widens across both monitors, and Delphi 7 locks up. It appears that Delphi 2010 modifies the .dpr in a way that makes Delphi 7 unable to open it again. All my forms & units appear to open fine in D7 after D2010, except for the warnings on the ExplicitHeight and ExplicitWidth properties being missing... I can easily recreate the .dpr, but is this expected behavior? Thanks. Tom Field wrote: > I had a project in Delphi 7. > > I worked o...

Synchronous use of a web service is useful?
I was curious if a web service adds any value when its called 'synchronously' from an ASP.Net application. Both the web service and ASP.Net application are within an enterprise's internal network and on separate web servers. I would think the web service would add no value, but  instead lower the performance in above scenario. True or false?sun21170 Yes.I agree with you. It seems that the only time when a 'synchronous' web service would provide value is when one or both of the following features are needed:n-tier architecture needs to implemented. n-tier developm...

delphi Win32 using delphi .NET dll
Hi, I'm trying to use a delphi.NET dll in delphi.WIN32. I am currently using CodeGear Delphi 2007 with version2(base version) of .NET I can get the dll to import into the WIN32 application the only problem is when i include things such as: "using Classes,DateUtils, SysUtils" in the .NET dll the win32 application will instantly hang when any of the dll functions are called. Any help would be great thanks. Also I have tried this example and it also crashes for me? http://cc.embarcadero.com/Item/22688 -Braden I also found this.. "The problem is that, wehn you instal...

How design a COM using Delphi and Using in PB7?
I need some component, So, we want using DELPHI5 to develop a COM or Activex. What type of knowege or Skill I must know it. Can anyone tell us? from DRAOGN Can you explain what type of component you want to create? You may not need to a) build your own, or b) use Delphi. If the component should be used from within a PB application, you ought to use PB to develop it unless it is something that PB does not let you do. regards, Bill DRAGON wrote: > I need some component, So, we want using DELPHI5 to develop a COM or > Activex. > What type of knowege or Skill I ...

can i use delphi 2007 bpls in delphi 2009
can i use delphi 2007 bpls in delphi 2009 ? "Gaetan SIRY" skrev i meddelelsen news:8703@forums.codegear.com... > can i use delphi 2007 bpls in delphi 2009 ? No. -- Finn Tolderlund The compatiblity between D2006 and D2007 was unique. Normaly they can't be compatible because new version enhance/change the API. And D2009 defines String = Unicode where older version has String = Ansistring so D200x/Dx-BPL are 200% incompatible with D2009. ...

How to use a delphi XE3 obj file in delphi 2007?
Hi, I'm migrating an application from delphi 2007 to delphi XE3 and I would like to do it in several steps. To do this I need to use a part of my new code (XE3) in the current delphi 2007 application. So I want to use the .obj file from my XE3 file. I know I have to use the {$L 'Filename'} and I know that I have to declare the functions that I want to use in external in my D2007 project. I wanted to make an easy and simple example to see how it works with only 1 function 'hello world' compiled in obj with XE3 but I can't find the way to use it in D2007. In D200...

MOVED: Use Delphi XE TLB importer in Delphi 2007?
....to the ActiveX group: https://forums.embarcadero.com/thread.jspa?threadID=47170 -- Craig Stuntz · Vertex Systems Corp. · Columbus, OH Delphi/InterBase Weblog : http://blogs.teamb.com/craigstuntz/ ...

How to use AppModeler to generate Delphi forms using Cached Updates
<HTML> <TT>A beginner's question regarding AppModeler 6.1, Delphi 3, and Interbase 5.x...</TT> <P><TT>While I am impressed with the amount of thought and work that has gone into AppModeler, I have nonetheless been surprised to discover that the Delphi application generated by AppModeler is not suitable for real-world situations because of the "fly-away" problem afflicting newly-entered records.</TT> <P><TT>When I enter a record on an AppModeler-generated Delphi form, the record disappears ("flies away") immedia...

how to use the synchronizer
2- about the powerbuilder synchronizer I have read the beginnings of the synchronizer from PB65 and have a working model... I am wondering if you could shed some light on the 2 runtime variables that are available... the scenario is the following --- I wish to synchronize all users on the LAN to the official version of the software @ any time.... I would like to define 2 variables source_dir = P:\sub_dir\sub_dir2 destination_dir = c:\prs\ Right now I have hard-coded the sync file and it seems to be working fine and indicate what 1 line of the sync file woul...

Web resources about - PostMessage Synchronization - embarcadero.delphi.vcl.using

Synchronization - Wikipedia, the free encyclopedia
... here. For linguistic synchrony, see Synchronic analysis . For the X-Files episode, see Synchrony (The X-Files) . Time-keeping and synchronization ...

Synchronization - Wikipedia, the free encyclopedia
... here. For linguistic synchrony, see Synchronic analysis . For the X-Files episode, see Synchrony (The X-Files) . Time-keeping and synchronization ...

NOTE-IFY (formed FNotes): colorful notes with Reminders, TextExpander and Dropbox synchronization on ...
Get NOTE-IFY (formed FNotes): colorful notes with Reminders, TextExpander and Dropbox synchronization on the App Store. See screenshots and ratings, ...

Paul Weyrich - Goo-Goo Syndrome (proper audio/video synchronization) - YouTube
Paul Weyrich, father of the right-wing movement and co-founder of the Heritage Foundation, Moral Majority and various other groups tells his ...

Making Sense of the Intel Haswell Transactional Synchronization eXtensions
Multi-core and Locking Intel has released additional information on their Transactional Synchronization technology (TSX), which is basically ...

‘Twitterrific’ Update Brings Cross-Device Timeline Synchronization
Twitterrific , one of the most popular Twitter clients for the iPhone and iPad developed by The Iconfactory, received a major update this week ...

Hands on with the new tab synchronization feature in Chrome 19
Google has rolled out a new release of Chrome in the stable channel. The update, version 19, uses the Chrome synchronization framework to introduce ...

Dell adds secure application data synchronization to cloud portfolio
... would offer methods to securely sync sensitive data across disparate cloud services. Called Integration Packs, Dell initially is offering synchronization ...

Workshare Drives Innovation by Extending Secure File Sharing and Synchronization Functionality to Developer ...
Workshare, a leading provider of secure enterprise collaboration applications, today announced it has extended its platform’s advanced file shar ...

Synchronization As A Service
When it comes to files we are beginning to take synchronization as a given. You work on a file at work and through the magic of Dropbox or similar ...

Resources last updated: 1/10/2016 12:44:22 AM