Re: [fpc-pascal] weird compiler(?) problem

2014-02-14 Thread Xiangrong Fang
2014-02-14 14:45 GMT+08:00 Sven Barth :

>
> @Xiangrong Fang: do you use 2.6.2 or 2.7.1?
>
> I use 2.6.2
​​
___
fpc-pascal maillist  -  fpc-pascal@lists.freepascal.org
http://lists.freepascal.org/cgi-bin/mailman/listinfo/fpc-pascal

Re: [fpc-pascal] Generics and key words

2014-02-14 Thread Maciej Izak
>  Currently only in mode Delphi.
>
> Regards,
> Sven
> ___
> fpc-pascal maillist  -  fpc-pascal@lists.freepascal.org
> http://lists.freepascal.org/cgi-bin/mailman/listinfo/fpc-pascal
>

also "generics overload" (TA, TA) don't work in objfpc... the
main reason why objfpc is unusable for me (also because "generic" and
"specialize" keyword)...

>.<

Regards,
Maciej Izak (hnb)
___
fpc-pascal maillist  -  fpc-pascal@lists.freepascal.org
http://lists.freepascal.org/cgi-bin/mailman/listinfo/fpc-pascal

Re: [fpc-pascal] weird compiler(?) problem

2014-02-14 Thread Sven Barth

Am 14.02.2014 09:09, schrieb Xiangrong Fang:
2014-02-14 14:45 GMT+08:00 Sven Barth >:



@Xiangrong Fang: do you use 2.6.2 or 2.7.1?

I use 2.6.2
Could be that it works a bit better in 2.7.1 as I changed something some 
months ago... (no guarantees though)


Regards,
Sven
___
fpc-pascal maillist  -  fpc-pascal@lists.freepascal.org
http://lists.freepascal.org/cgi-bin/mailman/listinfo/fpc-pascal

Re: [fpc-pascal] fp libraries do not like cmem ?

2014-02-14 Thread Fred van Stappen
Hello.

It seams to me that i monopolize the forum here.
Please advice if it borrow you, i will stop directly... ;-)

I still fight with cmem and trust me, it is a hard battle.
Mainly because the debugger gives me very few infos and because, if i do not 
use cmem, all examples are working perfectly, the debugger is happy and do not 
gives me any warning, only lot of "OK" ...

So difficult to understand why LCL+cmem do not like uos library.

I follow the advices of Tomas and the debugger gives me a (very) few more infos.
One is that:

#8  0x00426ae6 in fpc_ansistr_decr_ref ()

So i isolate the only function who gives as result string (and is working 
without cmem):

function uos_GetInfoDeviceStr() : String ; 
var
myresult : string;
begin
...
result := myresult;
end; 

I changed with

function uos_GetInfoDeviceStr() : PChar ;
var
myresult : string;
begin
...
result := @myresult;
end; 

And that is ok, no more crash and debugger is happy...

But now, when i try to retrieve the result with, for example :

procedure getinfo ;
var
mystring : string;
begin
mystring := uos_GetInfoDeviceStr();
end;

I get:
=> mystring = '' (empty string).  

How must i do to retreive the data of Pchar (the result of 
uos_GetInfoDeviceStr()) ?

PS : It seams that cmem prefers pointers as result, so, maybe i have to change 
all functions to give pointers as result (yes/no?). 

Many thanks.



  ___
fpc-pascal maillist  -  fpc-pascal@lists.freepascal.org
http://lists.freepascal.org/cgi-bin/mailman/listinfo/fpc-pascal

Re: [fpc-pascal] fp libraries do not like cmem ?

2014-02-14 Thread Ewald

On 14 Feb 2014, at 20:06, Fred van Stappen wrote:

> Hello.
> 
> It seams to me that i monopolize the forum here.
> Please advice if it borrow you, i will stop directly... ;-)
> 
> I still fight with cmem and trust me, it is a hard battle.

What I have been wondering for some time now (perhaps you wrote it in a mail 
and I missed it): do you use the same memory manager in the library and in your 
test program?

Also, beware when returning pointers. Consider this snippet:

Procedure ReturnTheAnwer: PInteger;
Var a: Integer;
Begin
a:= 42;
Result:= @a;
End;

One might think at first glance that `ReturnTheAnswer^` is 42, but this is 
incorrect in some cases. The example here might be extremely over-simplified, 
but replace `a: Integer` with `mystring: String` and we're roughly at your 
example.

--
Ewald

___
fpc-pascal maillist  -  fpc-pascal@lists.freepascal.org
http://lists.freepascal.org/cgi-bin/mailman/listinfo/fpc-pascal

Re: [fpc-pascal] fp libraries do not like cmem ?

2014-02-14 Thread Fred van Stappen
>What I have been wondering for some time now (perhaps you wrote it in a
>mail and I missed it): do you use the same memory manager in the library
>and in your test program?

Oops, indeed i forget that point...
The test now are with cmem only in program, not in library.

I will test with cmem into library too...

>The example here might be extremely over-simplified, but replace
> `a: Integer` with `mystring: String` and we're roughly at your example.

Hum, absolutely, it is the same example... 
So, how must i do to use PChar ?

Many thanks.

  ___
fpc-pascal maillist  -  fpc-pascal@lists.freepascal.org
http://lists.freepascal.org/cgi-bin/mailman/listinfo/fpc-pascal

Re: [fpc-pascal] fp libraries do not like cmem ?

2014-02-14 Thread Fred van Stappen
>What I have been wondering for some time now (perhaps you wrote it in a
> mail and I missed it): do you use the same memory manager in the library
> and in your test program?

> Ewald

Yeep, Ewald, you are the Winner.
And you impress me a lot with your perspicacity.

The battle has ended, with big triumph of fpc.

That does the trick:
Use cmem in both library and program... it works... :-)

Conclusion: That long but exciting adventure prove that fpc is the future, fpc 
will unify all the other libraries, from all languages and all os and will be 
the Conductor of that great unification.

Long life to fpc.

Many thanks for your help, wonderful fpc people.

Fred

 
  ___
fpc-pascal maillist  -  fpc-pascal@lists.freepascal.org
http://lists.freepascal.org/cgi-bin/mailman/listinfo/fpc-pascal

Re: [fpc-pascal] fp libraries do not like cmem ?

2014-02-14 Thread Ewald

On 14 Feb 2014, at 20:28, Fred van Stappen wrote:

> >What I have been wondering for some time now (perhaps you wrote it in a
> >mail and I missed it): do you use the same memory manager in the library
> >and in your test program?
> 
> Oops, indeed i forget that point...
> The test now are with cmem only in program, not in library.
> 
> I will test with cmem into library too...

That that is issue number 1: if your program wants to free something that your 
library allocated, both operations need to be done by the same memory manager.

> 
> >The example here might be extremely over-simplified, but replace
> > `a: Integer` with `mystring: String` and we're roughly at your example.
> 
> Hum, absolutely, it is the same example... 
> So, how must i do to use PChar ?

The way I use to pass strings from C/C++ libraries is to allocate a buffer, 
copy the contents of the string into the buffer and return the pointer to that 
buffer. The same method can be used here. Also don't forget to provide a second 
function to free these buffers (in this way your library can have a different 
memory manager than your application).

So you might try something along the lines of:

Function ConvertToPChar(str: String): PChar;
Begin
Result:= Getmem(Length(str) + 1);   // Allocating the buffer 
somewhere on the heap

If Length(str) > 0 Then
Move(str[1], Result[0], Length(str));   // The content

Result[l]Length(str)]:= #0; // The null termination
End;

..., where I decided to return `standard` null-terminated strings, since your 
library is meant to be `universal`.

Next, the procedure to free buffers using the memory manager of the library:

Procedure FreeBuffer(Buffer: Pointer);
Begin
FreeMem(Buffer);
End;

That's it for the library part. In your program part, don't forget to free 
these buffers once they are no longer needed.

Also note that this is a good approach with respect to `universiality`, but if 
you need to pass a lot of strings to and fro you would be better off with a 
more pascalish version. The thing that comes to mind here is providing a 
function in your library that replaces the current memory manager with the one 
passed to it as an argument. This would allow you to simply return a string.

--
Ewald

___
fpc-pascal maillist  -  fpc-pascal@lists.freepascal.org
http://lists.freepascal.org/cgi-bin/mailman/listinfo/fpc-pascal

Re: [fpc-pascal] fp libraries do not like cmem ?

2014-02-14 Thread Fred van Stappen
>Also note that this is a good approach with respect to 
`universiality`, but if you need to...

> Ewald

Thanks Ewald for your useful advices.

Now, a other battle will begin :

Create headers for C, CNet, Basic, Java, ... and translate all the uos Pascal 
examples.

But that is not the problem of fpc...

So, i will borrow now the forum of all those language :-) !  

Bye and, one more time : tetra thanks.

Fred.
  ___
fpc-pascal maillist  -  fpc-pascal@lists.freepascal.org
http://lists.freepascal.org/cgi-bin/mailman/listinfo/fpc-pascal

Re: [fpc-pascal] fp libraries do not like cmem ?

2014-02-14 Thread DaWorm
Or always allocate and free the buffer in the calling program and load the
contents into the buffer in the dll.  This way you never have to worry
about allocating in one place and freeing in the other.  Windows API does
stuff like this a lot.

In the DLL:

Procedure GetSomeString(StringPointer: Pointer, Var StringLen: Integer);
Begin
  StringLen = CalculateSizeOfString
  if StringPointer != nil then
// copy string to StringPointer
End;

In your App:

Var StringLen: Integer;
AllocedMem : Pointer;
. . .
// Find out how much memory needed
GetSomeString(nil, &StringLen);
GetMem(AllocedMem, StringLen);
GetSomeString(AllocedMem, &StringLen);
// use the string for whatever you need
. . .
FreeMem(AllocedMem);

Very bad psuedo code there, but it should give you the idea.

To summarize, your app needs to:

1. Call once with nil to get how much memory needed
2. Allocate the memory
3. Call again with pointer to memory
4. Use the allocated memory
5. Free the allocated memory

And your dll function needs to:

1. Check pointer for nil
2. If so, only set the length parameter to the size of memory needed and
exit
3. If not, copy data to pointer as well

Jeff.
___
fpc-pascal maillist  -  fpc-pascal@lists.freepascal.org
http://lists.freepascal.org/cgi-bin/mailman/listinfo/fpc-pascal