Jump to content
jesu

access violation changing font

Recommended Posts

Hello. Using 12.3 + patch 1. I'm seeing this error: 

Quote

Access violation at address 6F75232A in module 'COMCTL32.dll' (offset 2232A). Read of address 0000000C.

changing font at runtime. This is my code:

 

procedure TFprincipal.FontNameRClick(Sender: TObject);
begin
  ga_TipoLetra := FontNameR.Text;
  Font.Name := ga_TipoLetra;
  gn_FontSize := StrToInt(ComboSize.Text);
  Font.Size := gn_FontSize;
  Screen.MessageFont := Self.Font;
  ToolBar.Font.Size := Min(gn_FontSize, 14);
end;

The error happens at the end of that code in procedure TWinControl.DefaultHandler(var Message); inside VCL.Controls.
Is it a bug or should I do something different?

Thanks

Share this post


Link to post

From the posted code it is not clear what values are passed, for example what is assigned to "Font.name"? You need to find out which value is not right, start disabling some lines to see if the problem disappears.

Share this post


Link to post

I use this procedure to get installed fonts and allow to choose one:


 

procedure TFprincipal.GetFontNames;
var
  DC: HDC;
begin
  DC := GetDC(0);
  EnumFonts(DC, nil, @EnumFontsProc, Pointer(FontNameR.Items));
  ReleaseDC(0, DC);
  FontNameR.Sorted := True;
end;

but the problem seems to happen not when I change font name, but when I increase font size, and it doesn't happen always. If I choose a second time the same font size, the second time it seems to work.

 

Edited by jesu

Share this post


Link to post

try to monitor the values that are given to "font.size", or attach a simple project where we can check

Share this post


Link to post

My guess is that something you are doing elsewhere is corrupting memory and what you are seeing are just secondary errors.

You can probably use madExcept, with memory overrun check enabled, to find the origin of the problem.

Share this post


Link to post
On 5/26/2025 at 7:37 PM, Remy Lebeau said:

Why not use Screen.Fonts instead? 

I don't know. I've used that piece of code for more than 20 years. Maybe Screen.Fonts didn't exist or it didn't return the same.

Ok, I've reduced the program so anyone can test it. Just press F9, change Tamaño to 16 and you should see the error.

Trying to reproduce the error outside of the IDE seems much harder.

BugEditor.zip

error.png

Edited by jesu

Share this post


Link to post

It’s generally not advisable to change the font of a form or visual control directly from within an event handler like OnClick, especially for controls that are part of the form itself. Internally, setting Font.Name or Font.Size can lead to a call to CreateWnd, which may cause issues if it's done while Windows messages are still being processed for the same control—resulting in access violations like the one you're seeing in COMCTL32.dll.

One safe workaround is to defer the font update. You can use a short-delay TTimer for this purpose. Here's an example:

procedure TFprincipal.FontNameRClick(Sender: TObject);
begin
  Timer1.Enabled := True;
end;

procedure TFprincipal.Timer1Timer(Sender: TObject);
begin
  Timer1.Enabled := False; // Disable the timer to prevent repeated calls
  ga_TipoLetra := FontNameR.Text;
  Font.Name := ga_TipoLetra;
  gn_FontSize := StrToInt(ComboSize.Text);
  Font.Size := gn_FontSize;
  Screen.MessageFont := Self.Font;
  ToolBar.Font.Size := Min(gn_FontSize, 14);
end;

Set Timer1.Interval to something small like 10 and make sure it’s initially disabled (Enabled := False). This will safely postpone the font update until the current message queue has been processed.

 

Let me know if that solves the issue for you!

Share this post


Link to post

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now

×