Sign in Join

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

Mailing lists / fpc-pascal / 2006-01

Ansistrings exported in DLL's - mystery

13 messages · 5 people · –

Reply by email Subscribe to this list Your subscription

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

L505

Trustmaster from PSP project tells me he's been using Ansistrings in DLL without problems. The entire psp.furtopia.org website is running a DLL with ansistrings in it, he says.

He says he compiled it with FPC 1.0 a long time ago..

So the psp.furtopia website has been using ansistrings in DLL for ages now.

How can this be? How is it possible a website can run without any problems, using ansistrings?

i.e. exporting functions like this:

function something(param: ansistring): ansistring;

Can it be pure luck that a website is running this long with ansistrings?

I guess I'll have to try and get a copy of some of the source code that is running his DLL. I just find it hard to believe that the website has been up that long and hasn't crashed with a memory management issue - the one Jonas and Marc and I talked about earlier - the EXE/DLL freeing memory it didn't allocate, during a reference count decrement.

Can it be something in FPC 1.0 compiler that allowed this to happen? I guess I'll have to do more digging. I wish I could put an end to this and just use Pchars in these DLL situations - but there's always some evidence of ansistrings working, somewhere.

L505

Reply by email

Micha Nelissen
to L505
L505 wrote
Can it be something in FPC 1.0 compiler that allowed this to happen? I guess I'll have to do more digging. I wish I could put an end to this and just use Pchars in these DLL situations - but there's always some evidence of ansistrings working, somewhere.

There is an important difference between working and not-crashing. ;-)

Micha

Reply by email

Michael Van Canneyt
to L505: Trustmaster from PSP project tells me he's been using Ansistrings in DLL without…
L505 wrote 17 lines
Trustmaster from PSP project tells me he's been using Ansistrings in DLL without problems. The entire psp.furtopia.org website is running a DLL with ansistrings in it, he says. He says he compiled it with FPC 1.0 a long time ago.. So the psp.furtopia website has been using ansistrings in DLL for ages now. How can this be? How is it possible a website can run without any problems, using ansistrings? i.e. exporting functions like this: function something(param: ansistring): ansistring; Can it be pure luck that a website is running this long with ansistrings?

It all depends on how the DLL is used.
It's impossible to say something definite without this information.

If it's used by a C program such as Apache or so, then it should not present a .

Reply by email

L505
to Michael Van Canneyt
On Mon, 2 Jan 2006, L505 wrote: > Trustmaster from PSP project tells me he's been using Ansistrings in DLL without > problems. The entire psp.furtopia.org website is running a DLL with ansistrings

in

Show quoted text 23 lines
> it, he says. > > He says he compiled it with FPC 1.0 a long time ago.. > > So the psp.furtopia website has been using ansistrings in DLL for ages now. > > How can this be? How is it possible a website can run without any problems, using > ansistrings? > > i.e. exporting functions like this: > > function something(param: ansistring): ansistring; > > > Can it be pure luck that a website is running this long with ansistrings? It all depends on how the DLL is used. It's impossible to say something definite without this information. If it's used by a C program such as Apache or so, then it should not present a problem at all. Michael.

Its just a dll/so that a CGI is calling on - it's not an apache module or anything.

i.e. place a dll in a directory, and have a CGI program call on it..

I got a copy of the psp 1.0 sources and what he is doing is calling the functions from an SO/DLL from a cgi executable.. There are tons of functions like this that another fpc program is calling from the SO/DLL:

function something(param: ansistring): ansistring;

It's just funny, because a website is the thing you'd think would be the first thing to crash down: a website is accessed by several people at different times in the day, whereas a desktop is not. I visit his forum every day and have never once had a crash!

I can post the psp 1.0 sources online if anyone is interested, but probably it is only me who is so curious ;-)

Reply by email

Jonas Maebe
to L505
L505 wrote
I can post the psp 1.0 sources online if anyone is interested, but probably it is only me who is so curious ;-)

A very easy way to solve all problems in FPC is to simply use the cmem unit in the dll(s) and in your main program. It should have the same effect as Delphi's sharemem unit.

Jonas

Reply by email

L505
to Micha Nelissen: There is an important difference between working and not-crashing. ;-) Micha
Show quoted text 9 lines
L505 wrote: > Can it be something in FPC 1.0 compiler that allowed this to happen? I guess I'll > have to do more digging. I wish I could put an end to this and just use Pchars in > these DLL situations - but there's always some evidence of ansistrings working, > somewhere. There is an important difference between working and not-crashing. ;-) Micha

True, but who was that person who tried exporting a string recently and got an error immediately? He reported it as a bug - Unless that was a specific compiler that really did have a bug?

Reply by email

L505
to Jonas Maebe: A very easy way to solve all problems in FPC is to simply use the cmem unit in the dll(s)…
Show quoted text 13 lines
On 03 Jan 2006, at 18:29, L505 wrote: > I can post the psp 1.0 sources online if anyone is interested, but > probably it is > only me who is so curious ;-) A very easy way to solve all problems in FPC is to simply use the cmem unit in the dll(s) and in your main program. It should have the same effect as Delphi's sharemem unit. Jonas

Would that work with C programs that called a library? I think sharemem only works for borland applications (i.e. pascal and C++ builder).

Just wondering if it would have an advantage over sharemem, in this case.

Reply by email

Jonas Maebe
to L505
L505 wrote 7 lines
> A very easy way to solve all problems in FPC is to simply use the > cmem unit in the dll(s) and in your main program. It should have the > same effect as Delphi's sharemem unit. Would that work with C programs that called a library? I think sharemem only works for borland applications (i.e. pascal and C++ builder).

If a Pascal program returns a reference counted type to a C library, then you get a memory leak, sharemem/cmem or not. If a C program passes a pchar to a function that expects an ansistring, you can get any sort of behaviour ranging from no problem to program crashes (which is logical, since the C program is passing a variable of a wrong type).

You cannot declare a function as returning an "ansistring" in C, since C doesn't know the ansistring type. So you cannot properly use functions which accept or return values of that type from C programs. It's as simple as that. You simply have to declare your functions which you want to be callable from C with types that also exist in C (or which can be constructed to be 100% the same in C).

Jonas

Reply by email

L505
to Jonas Maebe
Show quoted text 18 lines
If a Pascal program returns a reference counted type to a C library, then you get a memory leak, sharemem/cmem or not. If a C program passes a pchar to a function that expects an ansistring, you can get any sort of behaviour ranging from no problem to program crashes (which is logical, since the C program is passing a variable of a wrong type). You cannot declare a function as returning an "ansistring" in C, since C doesn't know the ansistring type. So you cannot properly use functions which accept or return values of that type from C programs. It's as simple as that. You simply have to declare your functions which you want to be callable from C with types that also exist in C (or which can be constructed to be 100% the same in C). Jonas

Thanks, and after looking into cmem.pp source code, I see why - it is simply calling a few simple functions from the operating system libraries. cmem is a very short simple and small unit. Advantages over sharemem are with cmem you wouldn't have to ship sharemem.dll with the application.

Reply by email

L505
simple and small unit. Advantages over sharemem are with cmem you wouldn't have to ship sharemem.dll with the application.

In fact, if someone has not already done it, the cmem.pp unit could be converted for use with delphi too (cmem.pas), since no one enjoys shipping sharemem dll.

I wonder if anyone had the guts to standardize ansistrings so that every language had access to a standard ansistring system, which was defined as a standard to follow. Could work in a DLL too with a shared memory manager with multiple languages. Oh well, those are the blues and woes of using multiple languages and multiple compilers.

Reply by email

eduard stefan
to L505: Trustmaster from PSP project tells me he's been using Ansistrings in DLL without…

Hello L505,

you wrote 8 lines
Trustmaster from PSP project tells me he's been using Ansistrings in DLL without problems. The entire psp.furtopia.org website is running a DLL with ansistrings in it, he says. ... Can it be something in FPC 1.0 compiler that allowed this to happen? I guess I'll have to do more digging. I wish I could put an end to this and just use Pchars in these DLL situations - but there's always some evidence of ansistrings working, somewhere.

i learned a few things from here:

http://velthuis.homepage.t-online.de/articles/articles-pchars.html http://www.ibphoenix.com/main.nfs?a=ibphoenix&page=ibp_howto5

Best regards, eduard

Reply by email

L505
to eduard stefan
Show quoted text 5 lines

I have seen Rudy's article too, and am reading the other website now, though. I'm at the point where I feel 100 percent confident using Pchars at this point and have started to write pages about pchars too. The mystery was why Trustmaster's website has been working for 2-3 years now (or so) using ansistrings - defying all the laws and warnings and hand wavings from people.

Also, I wrote this a while back and update it with new info too: http://z505.com/cgi-bin/qkcont/qkcont.cgi?p=Clearing-Up-PChar-and-DLL-Confusion

But it is overwhelming and mainly just notes for my own purposes, so some people might find it too verbose to wade through.

With Cmem, and according to my current knowledge, Cmem should allow one to safely export strings from one Pascal program to another Pascal program (but only Pascal programs, or programs who have compatible reference counting - i.e. maybe Delphi).

Even if trustmaster's website does work fine with exporting strings without any Cmem, it would be wiser to just use Cmem - as it doesn't really cost you anything, and ensures things should work better than they are now, even if there are no problems now.

When considering a C program or library will interface in to the DLL, of course pchars will be needed - one cannot use ansistrings even with cmem. And I'm fine with that - since confident with pchars.

I didn't know about cmem and glad Jonas mentioned it. I still have to test it out though, to make sure things work in real life like they do in theory, when having two pascal applications talk to each other via cmem using ansistrings.

Reply by email

Jonas Maebe
to L505
L505 wrote 6 lines
With Cmem, and according to my current knowledge, Cmem should allow one to safely export strings from one Pascal program to another Pascal program (but only Pascal programs, or programs who have compatible reference counting - i.e. maybe Delphi).

Only if you also use cmem in your Delphi program (and indeed, if Delphi's ansistring structure is identical to FPC's, but I think it is).

Jonas

Reply by email