Sign in Join

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

Mailing lists / fpc-pascal / 2011-02

Passing objects to libraries and back

10 messages · 6 people · –

Reply by email Subscribe to this list Your subscription

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

Jorge Aldo G. de F. Junior

I want to add plugin suport to an network app i am developing,

the easiest way of doing this is passing the internal objects as parameters to funcions inside libraries (.so or .dll)

something like

procedure plugin(const parameter : tobject): tobject; external;

is it allowed ?

or if i cant use objects, can i pass ansistrings ? (i can serialize deserialize the object internal states)

i know that passing ansistrings or tobjects would make the plugin system compatible with pascal only, but i dont care about that.

thanks in advance

leledumbo
to Jorge Aldo G. de F. Junior
is it allowed ?

Yes, but watch out that is/as operator might not work as expected.

can i pass ansistrings ?

Yes, but only if you know what you're doing. NEVER modify the passed strings (it would confuse the reference counting). Use const parameter all the time.

i know that passing ansistrings or tobjects would make the plugin system compatible with pascal only

Perhaps even worse, to FPC only.

View this message in context: http://free-pascal-general.1045716.n5.nabble.com/Passing-objects-to-libraries-and-back-tp3401092p3401184.html Sent from the Free Pascal - General mailing list .
Jonas Maebe
to leledumbo
leledumbo wrote
> can i pass ansistrings ? Yes, but only if you know what you're doing. NEVER modify the passed strings (it would confuse the reference counting). Use const parameter all the time.

Or, alternatively, use the "cmem" unit in both the application and the plugin (or the "sharemem" unit under Windows).

Jonas

Michael Van Canneyt
to leledumbo: Yes, but watch out that is/as operator might not work as expected. Yes, but only if you…
leledumbo wrote 8 lines
> is it allowed ? Yes, but watch out that is/as operator might not work as expected. > can i pass ansistrings ? Yes, but only if you know what you're doing. NEVER modify the passed strings (it would confuse the reference counting). Use const parameter all the time.

It will almost certainly lead to memory leaks, because a simple a:=b;
inside the library with b passed on from the main program, will lead to problems as soon as the library is unloaded.

This is also true for strings that are properties of objects.

Michael.

Jonas Maebe
to Michael Van Canneyt
Michael Van Canneyt wrote 10 lines
On Fri, 25 Feb 2011, leledumbo wrote: >> can i pass ansistrings ? > > Yes, but only if you know what you're doing. NEVER modify the passed strings > (it would confuse the reference counting). Use const parameter all the time. It will almost certainly lead to memory leaks, because a simple a:=b; inside the library with b passed on from the main program, will lead to problems as soon as the library is unloaded.

It shouldn't matter with cmem/sharemem, because
a) unloading a library from within one of its own routines is not possible, so there cannot be any local variables in the shared libraries holding a reference at such a point b) global variables are finalised when the library is unloaded

Jonas

Unknown author
to Jonas Maebe
Jonas Maebe wrote 19 lines
On 26 Feb 2011, at 11:15, Michael Van Canneyt wrote: > On Fri, 25 Feb 2011, leledumbo wrote: > >>> can i pass ansistrings ? >> >> Yes, but only if you know what you're doing. NEVER modify the passed strings >> (it would confuse the reference counting). Use const parameter all the time. > > It will almost certainly lead to memory leaks, because a simple > a:=b; > inside the library with b passed on from the main program, will lead to problems as soon as the library is unloaded. It shouldn't matter with cmem/sharemem, because a) unloading a library from within one of its own routines is not possible, so there cannot be any local variables in the shared libraries holding a reference at such a point b) global variables are finalised when the library is unloaded

This is correct. I didn't know about sharemem, though. I should document it :-)

Michael.

waldo kitty
to leledumbo: Yes, but watch out that is/as operator might not work as expected. Yes, but only if you…
leledumbo wrote 8 lines
> is it allowed ? Yes, but watch out that is/as operator might not work as expected. > can i pass ansistrings ? Yes, but only if you know what you're doing. NEVER modify the passed strings (it would confuse the reference counting). Use const parameter all the time.

this seems to be something that i'm learning about... it appears to be a bit(???) different than what i am used to with TP/BP 6/7...

in this case, does a const parameter point to the original or a copy of the original?

leledumbo
to waldo kitty
Show quoted text 6 lines
this seems to be something that i'm learning about... it appears to be a bit(???) different than what i am used to with TP/BP 6/7... in this case, does a const parameter point to the original or a copy of the original?

Read the docs. If the size of the argument is greater than native pointer size, it would be passed by reference. Otherwise, it's passed by value. A new constref modifier would always pass it by reference.

View this message in context: http://free-pascal-general.1045716.n5.nabble.com/Passing-objects-to-libraries-and-back-tp3401092p3402047.html Sent from the Free Pascal - General mailing list .
Jonas Maebe
to leledumbo
leledumbo wrote
Read the docs. If the size of the argument is greater than native pointer size, it would be passed by reference.

If the docs say that somewhere, they are wrong. How a "const" parameter is passed is an implementation detail (which hence could change at any time), except for "stdcall" routines on Windows platforms (for COM compatibility).

The only thing 'const' means is that the programmer tells to the compiler that this parameter will not change inside the called routine (if it nevertheless does, the generated code may be invalid). Based on that information, the compiler can perform additional checks (catching direct assignments to the parameter inside the routine) and optimizations (passing it by value/reference depending on the size and target platform, not performing reference count adjustments, ...).

Jonas

Michael Van Canneyt
to Jonas Maebe
Jonas Maebe wrote 10 lines
On 27 Feb 2011, at 09:58, leledumbo wrote: > Read the docs. If the size of the argument is greater than native pointer > size, it would be passed by reference. If the docs say that somewhere, they are wrong. How a "const" parameter is passed is an implementation detail (which hence could change at any time), except for "stdcall" routines on Windows platforms (for COM compatibility).

I have adapted the docs.

Michael.