Clueless here

classic Classic list List threaded Threaded
16 messages Options
Reply | Threaded
Open this post in threaded view
|

Clueless here

Darius Blaszyk
Hi,
 
I have two overloaded functions. One overloaded function just rearanges one parameter from an array of extended to an array of ^extended and calls the other overloaded function. The problem is however that the 2nd function call crashes the app. I really have no idea why therefore I'm posting the code in the hope someone with more insight can help me here.
 
So what happens is the following; the first debugmessages are ok and as expected. However the second debugmessages (array of PJmFloat) crash the application. I have attached the backtrace.
 
Darius
 
type
  TJmFloat = extended;
  PJmFloat = ^TJmFloat;
  TDynTJmFloatArray = array of TJmFloat;
 
Function FitSession( Parameters : array of PJmFloat; ChiSquareFunc : TOFitFunc ) : IFit;
var i: integer;
begin
  //debug only
  for i := 0 to High(Parameters) do
    ShowMessage('array of PJmFloat ' + FloatToStr(Parameters[i]^));
 
 Result := TFit.Create( Parameters, ChiSquareFunc );
end;
 
function FitSession(Parameters: TDynTJmFloatArray; ChiSquareFunc: TOFitFunc): IFit;
var
  Params: array of PJmFloat;
  i: integer;
begin
  SetLength(Params, Length(Parameters));
 
  for i := 0 to High(Parameters) do
  begin
    Params[i] := AllocMem(SizeOf(TJmFloat));
    Params[i] := @Parameters[i];
  end;
 
  //debug only
  for i := 0 to High(Params) do
    ShowMessage('TDynTJmFloatArray ' + FloatToStr(Params[i]^));
 
  Result := FitSession(Params, ChiSquareFunc);
end;
begin
    SetLength(c, 2);
    c[0] := 2;
    c[1] := 0.2;
    Fit := FitSession(c, @ChiSquareFunc);
end;

_______________________________________________
fpc-pascal maillist  -  [hidden email]
http://lists.freepascal.org/mailman/listinfo/fpc-pascal

backtrace.zip (2K) Download Attachment
Reply | Threaded
Open this post in threaded view
|

Re: Clueless here

mariano podesta
hi daruis

change this line

    Params[i] := @Parameters[i];


with this

Params[i]^ := Parameters[i];

hope it works.
marianop

_______________________________________________
fpc-pascal maillist  -  [hidden email]
http://lists.freepascal.org/mailman/listinfo/fpc-pascal
Reply | Threaded
Open this post in threaded view
|

Re: Clueless here

Michalis Kamburelis
In reply to this post by Darius Blaszyk
Darius Blaszijk wrote:

> Hi,
>  
> I have two overloaded functions. One overloaded function just rearanges
> one parameter from an array of extended to an array of ^extended and
> calls the other overloaded function. The problem is however that the 2nd
> function call crashes the app. I really have no idea why therefore I'm
> posting the code in the hope someone with more insight can help me here.
>  
> So what happens is the following; the first debugmessages are ok and as
> expected. However the second debugmessages (array of PJmFloat) crash the
> application. I have attached the backtrace.
>  
> Darius
>  
> type
>   TJmFloat = extended;
>   PJmFloat = ^TJmFloat;
>   TDynTJmFloatArray = array of TJmFloat;
>  
> Function FitSession( Parameters : array of PJmFloat; ChiSquareFunc :
> TOFitFunc ) : IFit;
> var i: integer;
> begin
>   //debug only
>   for i := 0 to High(Parameters) do
>     ShowMessage('array of PJmFloat ' + FloatToStr(Parameters[i]^));
>  
>  Result := TFit.Create( Parameters, ChiSquareFunc );
> end;
>  
> function FitSession(Parameters: TDynTJmFloatArray; ChiSquareFunc:
> TOFitFunc): IFit;
> var
>   Params: array of PJmFloat;
>   i: integer;
> begin
>   SetLength(Params, Length(Parameters));
>  
>   for i := 0 to High(Parameters) do
>   begin
>     Params[i] := AllocMem(SizeOf(TJmFloat));
>     Params[i] := @Parameters[i];

Don't you want to write here
  Params[i]^ := Parameters[i];
?

>   end;
>  
>   //debug only
>   for i := 0 to High(Params) do
>     ShowMessage('TDynTJmFloatArray ' + FloatToStr(Params[i]^));
>  
>   Result := FitSession(Params, ChiSquareFunc);
> end;
> begin
>     SetLength(c, 2);
>     c[0] := 2;
>     c[1] := 0.2;
>     Fit := FitSession(c, @ChiSquareFunc);
> end;
>
>

_______________________________________________
fpc-pascal maillist  -  [hidden email]
http://lists.freepascal.org/mailman/listinfo/fpc-pascal
Reply | Threaded
Open this post in threaded view
|

Re: Clueless here

Darius Blaszyk
In reply to this post by mariano podesta
I did as you and Michalis suggested, but it makes no difference the application keeps crashing. I keep getting a " Invalid floating point operation".
 
Darius
----- Original Message -----
Sent: Thursday, June 29, 2006 10:20 PM
Subject: Re: [fpc-pascal] Clueless here

hi daruis

change this line

    Params[i] := @Parameters[i];


with this

Params[i]^ := Parameters[i];

hope it works.
marianop


_______________________________________________
fpc-pascal maillist  -  [hidden email]
http://lists.freepascal.org/mailman/listinfo/fpc-pascal

_______________________________________________
fpc-pascal maillist  -  [hidden email]
http://lists.freepascal.org/mailman/listinfo/fpc-pascal
Reply | Threaded
Open this post in threaded view
|

Re: Clueless here

Dominique Leducq-2
On Fri, 30 Jun 2006 12:30:33 +0200
"Darius Blaszijk" <[hidden email]> wrote:

> I did as you and Michalis suggested, but it makes no difference the application keeps crashing. I keep getting a " Invalid floating point operation".

What architecture are you working on? what version(s) of the compiler did you try? There are known bugs with floating point numbers on AMD64 with fpc 2.0.2 (and older 2.1.1), which seem to be fixed in fpc 2.0.3 (upcoming 2.0.4) and recent 2.1.1.

Dominique

_______________________________________________
fpc-pascal maillist  -  [hidden email]
http://lists.freepascal.org/mailman/listinfo/fpc-pascal
Reply | Threaded
Open this post in threaded view
|

Re: Clueless here

Darius Blaszyk
I'm using 2.0.2 on winxp

Darius

----- Original Message -----
From: "Dominique Leducq" <[hidden email]>
To: <[hidden email]>
Sent: Friday, June 30, 2006 5:15 PM
Subject: Re: [fpc-pascal] Clueless here


> On Fri, 30 Jun 2006 12:30:33 +0200
> "Darius Blaszijk" <[hidden email]> wrote:
>
>> I did as you and Michalis suggested, but it makes no difference the
>> application keeps crashing. I keep getting a " Invalid floating point
>> operation".
>
> What architecture are you working on? what version(s) of the compiler did
> you try? There are known bugs with floating point numbers on AMD64 with
> fpc 2.0.2 (and older 2.1.1), which seem to be fixed in fpc 2.0.3 (upcoming
> 2.0.4) and recent 2.1.1.
>
> Dominique
>
> _______________________________________________
> fpc-pascal maillist  -  [hidden email]
> http://lists.freepascal.org/mailman/listinfo/fpc-pascal 

_______________________________________________
fpc-pascal maillist  -  [hidden email]
http://lists.freepascal.org/mailman/listinfo/fpc-pascal
Reply | Threaded
Open this post in threaded view
|

writeln in threads on linux

Ole J. Røtne
 
Can anyone explain why i get output like this in a small test program:
Inside Thread
                 Main Loop
Inside Thread
                 Main Loop
Inside Thread
                 Main Loop


On Windows the output are lined up just as I would expect:
Inside Thread
Main Loop
Inside Thread
Main Loop

Here's a snip of the code:

// Main Prog

Thr := threadtest.Create;

Repeat
  k := PollKeyEvent;
  WriteLn('Mein Loop');
  Sleep(100);
Until k <> 0;

And the ThreadTest.Execute i just:

Procedure ThreadTest.Execute;
Begin
  Repeat
    WriteLn('Inside Thread');
    Sleep(100);
  Until Terminated
End;


And if this is a "wrong way" of doing this thing, could someone tell me how
it should be..

Oh and BTW. Using FPC 2.1.1 snapshot from 14.06.06 (from Lazarus site), on
OpenSuSe 10.1..





_______________________________________________
fpc-pascal maillist  -  [hidden email]
http://lists.freepascal.org/mailman/listinfo/fpc-pascal
Reply | Threaded
Open this post in threaded view
|

Re: writeln in threads on linux

Antal-2
> Can anyone explain why i get output like this in a small test program:
> Inside Thread
> Main Loop
> Inside Thread
> Main Loop
> Inside Thread
> Main Loop
That's because the line terminator!
#10#13 or #13#10 acts differently
So, check in the source code which one is used, because the first one is
not suitable in Windows!
As you supposedly know, the end line terminator is different on Mac, Unix
and Windows.
Mac only uses #13
Unix only uses #10
Windows uses #13#10 (CRLF)
This comes from the fact that telex machines needed to complete two
different actions in order to start writing in a new line (just like what
you have to do on a normal typewriter): first to step on the next row,
then to put the head at the beginning of the row.
So, basically #10 means Line-Feed (LF) (step to the next row)
While #13 means Carriage Return (CR) which means "move the carriage -the
printing head- to the beginning of the row) - or what you are doing on a
typewriter by hand, using that lever from the right upper part of the
machine.
so, when OS-es split up, the kept what they wanted, although MS had had to
preserve the standard as long as they was part of IBM those times, and IBM
telex machines needed the time of two caracter printing to complete a CRLF
operation.
But I I've seen, sometimes in FP source codes the CRLF is using as LFCR
which does nothing wrong on Unix, as long as CR is invisible, but on

So just look out for this in the source-code.
Or just use
write('Main Loop',#13#10);
:^)

>
>
> On Windows the output are lined up just as I would expect:
> Inside Thread
> Main Loop
> Inside Thread
> Main Loop
>
> Here's a snip of the code:
>
> // Main Prog
>
> Thr := threadtest.Create;
>
> Repeat
>  k := PollKeyEvent;
>  WriteLn('Mein Loop');
>  Sleep(100);
> Until k <> 0;
>
> And the ThreadTest.Execute i just:
>
> Procedure ThreadTest.Execute;
> Begin
>  Repeat
>    WriteLn('Inside Thread');
>    Sleep(100);
>  Until Terminated
> End;
>
>
> And if this is a "wrong way" of doing this thing, could someone tell me how
> it should be..
>
> Oh and BTW. Using FPC 2.1.1 snapshot from 14.06.06 (from Lazarus site), on
> OpenSuSe 10.1..
_______________________________________________
fpc-pascal maillist  -  [hidden email]
http://lists.freepascal.org/mailman/listinfo/fpc-pascal
Reply | Threaded
Open this post in threaded view
|

SV: Re: writeln in threads on linux

Ole J. Røtne
> > Can anyone explain why i get output like this in a small
> test program:
> > Inside Thread
> > Main Loop
> > Inside Thread
> > Main Loop
> > Inside Thread
> > Main Loop
> That's because the line terminator!
> #10#13 or #13#10 acts differently
> So, check in the source code which one is used, because the
> first one is not suitable in Windows!
> As you supposedly know, the end line terminator is different
> on Mac, Unix and Windows.
> Mac only uses #13
> Unix only uses #10
> Windows uses #13#10 (CRLF)

This I do know :)

> So just look out for this in the source-code.
> Or just use
> write('Main Loop',#13#10);
> :^)

If I do this I get this instead:
Inside Thread

Main Loop

Inside Thread

Main Loop

Inside Thread

Main Loop

And one more thing.

When the program terminates, my terminal don't work any more either..
So somethings not right someplace..

Another sidenote is that when I don't use threads, the output is as
expected..:

Main Loop
Main Loop
Main Loop
Main Loop



Ole JR

_______________________________________________
fpc-pascal maillist  -  [hidden email]
http://lists.freepascal.org/mailman/listinfo/fpc-pascal
Reply | Threaded
Open this post in threaded view
|

Re: writeln in threads on linux

Florian Klaempfl
In reply to this post by Ole J. Røtne
Ole J. Røtne wrote:

>  
> Can anyone explain why i get output like this in a small test program:
> Inside Thread
> Main Loop
> Inside Thread
> Main Loop
> Inside Thread
> Main Loop
>
>
> On Windows the output are lined up just as I would expect:
> Inside Thread
> Main Loop
> Inside Thread
> Main Loop
>
> Here's a snip of the code:
>
> // Main Prog
>
> Thr := threadtest.Create;
>
> Repeat
>   k := PollKeyEvent;
>   WriteLn('Mein Loop');
>   Sleep(100);
> Until k <> 0;
>
> And the ThreadTest.Execute i just:
>
> Procedure ThreadTest.Execute;
> Begin
>   Repeat
>     WriteLn('Inside Thread');
>     Sleep(100);
>   Until Terminated
> End;
>
>
> And if this is a "wrong way" of doing this thing, could someone tell me how
> it should be..

You probably want to guard the output with mutexes or semaphores.
Writing unsynchronized to a stream has always unexpected results.

>
> Oh and BTW. Using FPC 2.1.1 snapshot from 14.06.06 (from Lazarus site), on
> OpenSuSe 10.1..
>
>
>
>
>
> _______________________________________________
> fpc-pascal maillist  -  [hidden email]
> http://lists.freepascal.org/mailman/listinfo/fpc-pascal

_______________________________________________
fpc-pascal maillist  -  [hidden email]
http://lists.freepascal.org/mailman/listinfo/fpc-pascal
Reply | Threaded
Open this post in threaded view
|

Re: Re: writeln in threads on linux

Felipe Monteiro de Carvalho
In reply to this post by Antal-2
On 7/2/06, Antal <[hidden email]> wrote:
> So just look out for this in the source-code.
> Or just use
> write('Main Loop',#13#10);
> :^)

Isn´t it better to use:

write('Main Loop',LineEnding);

this way the RTL will substitue LineEnding with the appropriate one
for that platform.

--
Felipe Monteiro de Carvalho
_______________________________________________
fpc-pascal maillist  -  [hidden email]
http://lists.freepascal.org/mailman/listinfo/fpc-pascal
Reply | Threaded
Open this post in threaded view
|

SV: writeln in threads on linux

Ole J. Røtne
In reply to this post by Florian Klaempfl
 

> -----Opprinnelig melding-----
> Fra: [hidden email]
> [mailto:[hidden email]] På vegne av
> Florian Klaempfl
> Sendt: 2. juli 2006 12:46
> Til: FPC-Pascal users discussions
> Emne: Re: [fpc-pascal] writeln in threads on linux
>
> > And if this is a "wrong way" of doing this thing, could
> someone tell
> > me how it should be..
>
> You probably want to guard the output with mutexes or semaphores.
> Writing unsynchronized to a stream has always unexpected results.
>

Did try to wrap both of the WriteLn() with
EnterCriticalSection/LeaveCriticalSection.
Same result..

And to answer another post at the same time..

If I use: Writeln('.....', LineEnding);
I get the double lineshift thing all over again, same as using #13#10..

Inside Thread
       
                 Main Program

Inside Thread

And so on...

And as I said in my last post.

When the program terminates, the lineshift thing is still there.
Seem like something isn't cleared/reset at program termination..
If I press enter, the next prompt end up after the other. (no lineshift)

Example:
olejr@linux-box:~/test> olejr@linux-box:~/test>




Ole JR

_______________________________________________
fpc-pascal maillist  -  [hidden email]
http://lists.freepascal.org/mailman/listinfo/fpc-pascal
Reply | Threaded
Open this post in threaded view
|

Re: SV: writeln in threads on linux

Florian Klaempfl
Ole J. Røtne wrote:

>  
>
>> -----Opprinnelig melding-----
>> Fra: [hidden email]
>> [mailto:[hidden email]] På vegne av
>> Florian Klaempfl
>> Sendt: 2. juli 2006 12:46
>> Til: FPC-Pascal users discussions
>> Emne: Re: [fpc-pascal] writeln in threads on linux
>>
>>> And if this is a "wrong way" of doing this thing, could
>> someone tell
>>> me how it should be..
>> You probably want to guard the output with mutexes or semaphores.
>> Writing unsynchronized to a stream has always unexpected results.
>>
>
> Did try to wrap both of the WriteLn() with
> EnterCriticalSection/LeaveCriticalSection.
> Same result..
>

Please post the complete code.
_______________________________________________
fpc-pascal maillist  -  [hidden email]
http://lists.freepascal.org/mailman/listinfo/fpc-pascal
Reply | Threaded
Open this post in threaded view
|

Re: writeln in threads on linux

Graeme Geldenhuys-2
In reply to this post by Ole J. Røtne
Could we see the complete code please...

Graeme.



On 7/2/06, Ole J. Røtne <[hidden email]> wrote:
> Did try to wrap both of the WriteLn() with
> EnterCriticalSection/LeaveCriticalSection.
> Same result..

--
There's no place like 127.0.0.1

_______________________________________________
fpc-pascal maillist  -  [hidden email]
http://lists.freepascal.org/mailman/listinfo/fpc-pascal
Reply | Threaded
Open this post in threaded view
|

SV: SV: writeln in threads on linux

Ole J. Røtne
In reply to this post by Florian Klaempfl
 
> Please post the complete code.

Here goes (Typed from my Linux box, as i don't have filesharing going
there):

<---- Start Unit1
Unit unit1;
{$mode objfpc}{$H+}

Interface

Uses
  Classes, SysUtils;

Type
  threadtest = class(tthread)
    constructor Create;
    procedure Execute: override;
End;

Var
  crit:TRTLCriticalSection;

Implementation

Constructor threadtest.Create;
Begin
  FreeOnTerminate := True;
  Inherited Create(False);
End;

Procedure threadtest.Execute;
Begin
  Repeat
    EnterCriticalSection(crit); // Get the same result with/without
    WriteLn('Inside Thread');
    LeaveCriticalSection(crit); // Get the same result with/without
  Until Terminated;
End;

End.
<--- End Unit1
<--- Start Program1
Program Program1

{$mode objfpc}{$H+}
{$DEFINE UseCThreads}

Uses
  {$IFDEF UNIX}{$IFDEF UseCThreads}
  cthreads,
  {$ENDIF}{$ENDIF}
  Classes, SysUtils, Unit1, Keyboard;

Var
  MyThread:threadtest;
  k:TKeyEvent;

Begin

InitKeyboard;
InitCriticalSection(crit);

MyThread := ThreadTest.Create;

Repeat
  k := PollKeyEvent;
  EnterCriticalSection(crit); // Get the same result with/without
  WriteLn('Main Program');
  LeaveCriticalSection(crit); // Get the same result with/without
Until k <> 0;

MyThread.Terminate;

End.
<--- End Program1

Ole JR

_______________________________________________
fpc-pascal maillist  -  [hidden email]
http://lists.freepascal.org/mailman/listinfo/fpc-pascal
Reply | Threaded
Open this post in threaded view
|

Re: writeln in threads on linuxt

Marco van de Voort
In reply to this post by Florian Klaempfl
> > And if this is a "wrong way" of doing this thing, could someone tell me how
> > it should be..
>
> You probably want to guard the output with mutexes or semaphores.
> Writing unsynchronized to a stream has always unexpected results.

Even then it won't necessarily work.  textrec threadvar problem.
_______________________________________________
fpc-pascal maillist  -  [hidden email]
http://lists.freepascal.org/mailman/listinfo/fpc-pascal