Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • Home
  • SEARCH
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 7752139
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 1, 20262026-06-01T11:43:22+00:00 2026-06-01T11:43:22+00:00

My app has a worker thread, and I use PostMessage to send a string

  • 0

My app has a worker thread, and I use PostMessage to send a string to the main thread. For 1 message, the string is truncated when it gets to the message handler in the main thread.

The string is constructed in the worker thread from a string of raw data that is like this. It ends at the last ’20’.

‘01010000000030000102000008850008855343414E204544474520000000000000000000’

Decoded into the string I want to send it looks like this, which is correct:

‘0100 0.50000 LSB0288.588.5SCAN EDGE ‘

The code that creates the ‘SCAN EDGE ‘ portion and posts it is:
tmp and s_out are strings

x := 35;
for i := 1 to 10 do
begin
  tmp := '$' + copy(s,x,2);
  TryStrToInt(tmp,dec);
  s_out := s_out + chr(dec);
  x := x + 2;
end;
PostMessage(MainHandle,UM_CLONE, UM_756, Integer(PChar(s_out)));

The message handler in the main thread is:
i is a string

i := pChar(msg.LParam);

when it gets to the main thread i looks like this in the debugger:

‘0100 0.50000 LSB0288.588.5SCAN EDG’#0

How can I correct this?

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-01T11:43:24+00:00Added an answer on June 1, 2026 at 11:43 am

    You are posting the contents of a String variable that is local to the thread procedure that is calling PostMessage(). If the String goes out of scope and gets freed before the main thread processes the posted message, the memory will be garbage.

    You need to either:

    1) use SendMessage() instead of PostMessage() so the String stays in scope until the message handler exits:

    SendMessage(MainHandle, UM_CLONE, UM_756, LPARAM(PChar(s_out)));
    

    2) dynamically allocate a String in memory, fill it as needed, post it, and then let the main message handler free it when it is done copying it:

    var
      s_out: PString;
    
    New(s_out);
    ...
    s_out^ := s_out^ + chr(dec);
    ...
    if not PostMessage(MainHandle, UM_CLONE, UM_756, LPARAM(s_out)) then
      Dispose(s_out);
    

    .

    var
      ps: PString;
      i: String;
    
    ps := PString(msg.LParam);
    i := ps^;
    Dispose(ps);
    

    PS: notice I also changed your Integer() cast to LPARAM(). This is very important if you ever upgrade to Delphi XE2 or later. PostMessage() and SendMessage() use LPARAM, not Integer. You can get away with it in Delphi 7 because LPARAM is an alias for Integer in that version, but that is not the case anymore in modern Delphi versions. LPARAM is an alias for NativeUInt now. LPARAM has always been an unsinged type in the Win32 API, Delphi just got it wrong in the early versions, but Embarcadero has really been pushing for type-correctness in the RTL/VCL since adding support for 64-bit and cross-platform development. If you don’t match the right data types correctly, you can cause range checks errors and such at runtime.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

In the main loop of a worker thread that has to poll to know
My app has to load an image from a http server and displaying it
I have a wxPython app which has many worker threads, idle event cycles, and
On windows, each thread has a message queue, and each message queue will process
My app has many controls on its surface, and more are added dynamically at
My app has a DataGridView object and a List of type MousePos. MousePos is
My app has several buttons which trigger different events. The user should NOT be
My app has a session timeout after 30 minutes. If the user has a
My app has three text fields: 1) Username // Initialization code usernameTextField = [[UITextField
My app has 6 menu items, so the OS shows the first 4, then

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.