Sign in Join

Live chat for Free Pascal, Lazarus, Delphi and every Pascal programmer.

Mailing lists / lazarus / 2013-01

property setter

5 messages · 4 people · –

Reply by email Subscribe to this list Your subscription

Read-only archive. Use j and k to move between messages.

Paul Ishenin
to xrfang

24.01.2013 10:08, xrfang ?????:

Show quoted text 8 lines
procedure TPainter.SetFont(AValue: TFont); begin if FFont=AValue then Exit; FFont:=AValue; end; My question is, why the "if" clause needed? Is there any performance benefits?
Usually setters have more complex code than just FFont:=AValue;

In this case If block protects both code performance and code logic. Setters usually should not perform any actions if they are called with the same value as property have.

If setter only assigns a value to a field then probably it can be replaced with the field in the property declaration.

Best regards,
Paul Ishenin

Reply by email

Darmawan Sugiarto
to Paul Ishenin

hello guys, I got some bug on library project. When I uses Lazarus 0.9.28.2 I write this code and work well, then I recompile it use Lazarus 1.0.2 and 1.0.4 and got this error....

Project project_test raised exception class 'External: SIGEGV'. At address 7C91B1FA

I have 2 project

??? first project is library project...
Here is the code for first project

function test(_str:string):string; cdecl; export;
implementation
function test(_str:string):string; cdecl; export;
var
? _i,_l,_k,_d:integer;
? _h:string;
? _char:Char;
begin
? if _str'' then
? begin
??? _l:=length(_str);
??? //SetLength(_h,_l);
??? _h:=_str;
??? _char:='t';
??? _h[1]:=_char;
? end else _h:='';
? Result :=_h;
end;

?Then second project is application project.
I got 1 button to execute function in the library project. Here is the code.

? function test(_str:string):string; cdecl; external 'project1.dll' name 'TEST';
var
? Form1: TForm1;
implementation
{$R *.lfm}

{ TForm1 }

procedure TForm1.Button1Click(Sender: TObject);
begin
? ShowMessage(test('alex'));
end;??

I got that error when I click the button.
If I delete this selection, It work well
??? _h[1]:=_char;

=============INTOSOFT============
ALEXIO CHAOS

Yahoo Messager
=================================

Reply by email

Sven Barth
to Darmawan Sugiarto: hello guys, I got some bug on library project. When I uses Lazarus 0.9.28.2 I write this…
Darmawan Sugiarto wrote 12 lines
hello guys, I got some bug on library project. When I uses Lazarus 0.9.28.2 I write this code and work well, then I recompile it use Lazarus 1.0.2 and 1.0.4 and got this error.... Project project_test raised exception class 'External: SIGEGV'. At address 7C91B1FA I have 2 project first project is library project... Here is the code for first project function test(_str:string):string; cdecl; export;

Short answer: Don't use strings as arguments or result values if you export a function from a library. Use PChar instead.

Long answer: Strings are so called "managed types". They involve compiler magic which allocates the memory and frees it also if the string is no longer needed. If you now have a library (let's assume both are written with FPC) then you have two heap managers: one in the program and one in the library. Those two don't know about each other and thus if e.g. the program tries to free the string that your library function returned that it will break, because it assumes the memory was allocated by itself.
This can be solved in two (or in theory three) ways:
1. use PChar and manage the memory for your strings yourself (e.g. if your library returns a PChar you need to have an additional exported function with which you can tell your library to free the PChar's memory again). Additional positive effect: the library can be written in something else than FPC or the library can be used by a program not written with FPC (other languages don't know FPC's String type). [plattform specific note: on Windows (and there only!) you can use the WideString type, because that type is managed by Windows]
2. use a common heap manager. If the heap manager is shared between the program and the library than you can freely pass memory between both parts and free it in the other part. For this you can use the unit "cmem" which will use the C library's heap manager (works on Unix systems, but should work on Windows ones as well). Note: the unit should be the first unit used by your main file (the program file and the library file)
(3. dynamic runtime packages allow you to freely pass strings (and also object instances, etc.) between the program file and the package. As Free Pascal does not support them yet this is only a theoretical option)

Additional note: Don't start a new question by answering to an existing mail, because than your answer might not be seen by other mailing list readers, because they might ignore the thread you posted to (as seems to have happened...)

Regards,
Sven

Reply by email