Recent

Author Topic: Strange Library Behaviour  (Read 5849 times)

garlar27

  • Hero Member
  • *****
  • Posts: 652
Strange Library Behaviour
« on: October 02, 2013, 05:30:50 pm »
Hi!
   I'm doing a Library which reads data from a serial port connected hardware and send the collected data to a remote server. The server sends a confirmation which the library returns to the application and the process ends.

   The library is pretty simple, never the less I started to have Access Violations and SigSegs. These exceptions desapeared when I use the working class (the one which the library use) directly from the form, the application worked smoothly.

   Then I removed a Callback the application passed to the library to update the form with a text describing the job's execution state. to see if this solved the problem but, it didn't.

   After that I decided to do a Test Library Project to explore what I can do with the library and the errors I might get without having to recompile the app and library. I included in the project everything I thought could cause the exception: like a complex output and a Callback and I found something strange (or at least unexpected for me):
   o- AV is thrown when the library's output includes a String created or modified by the librarary.
   o- AV is thrown when the library's output includes a Dynamic Array.
   o- AV is thrown when the library use a Callback passed by the Form.

Now comes the question:
   Did I do something wrong?
   Can I export an object created inside the library?
   Why I can't execute a Callback inside the Library?

A few more questions:
   Is there any good library Tutorial?
   Is there any good PChar Tutorial? The only examples I found were in "strings" unit's help, and there was nothing to convert very long pascal strings to PChar so I made my own, but I don't know if there's something alredy made in FPC.

   I attached the test library project and the GUI to use it. Please test it using the different options (mtNone, mtString, mtArray, mtPChar) and confirm if you have an AV when message Type is mtString or mtArray or when use notify event.

This post is derived from this thread.

Using Lazarus 1.1 SVN Revision: 40379 Date:2013-04-09  FPC: 2.6.2 on Windows XP. But the solution must be crossplatform.

Leledumbo

  • Hero Member
  • *****
  • Posts: 8836
  • Programming + Glam Metal + Tae Kwon Do = Me
Re: Strange Library Behaviour
« Reply #1 on: October 03, 2013, 11:52:23 am »
Quote
Did I do something wrong?
A lot, you can't pass around either as parameters or return value anything that uses reference counting like strings and dynamic arrays.
Quote
Can I export an object created inside the library?
Yes, but with limitations.
Quote
Why I can't execute a Callback inside the Library?
It depends on the callback parameters and return type (back to the first question).
Quote
Is there any good library Tutorial?
None that I know of except the Delphi ones, but not all things there are applicable to FPC implementation.
Quote
Is there any good PChar Tutorial? The only examples I found were in "strings" unit's help, and there was nothing to convert very long pascal strings to PChar so I made my own, but I don't know if there's something alredy made in FPC.
The reference guide on PChar is the only thing needed to understand PChar, I guess. Converting Pascal strings to PChar is as simple as PChar(StringVariable) or @StringVariable[1]. For ShortString, however, you need to ensure it ends with #0.

garlar27

  • Hero Member
  • *****
  • Posts: 652
Re: Strange Library Behaviour
« Reply #2 on: October 03, 2013, 02:54:12 pm »
Thank you Leledumbo for taking the time to see the example!!!

A lot, you can't pass around either as parameters or return value anything that uses reference counting like strings and dynamic arrays.
That explains why PChar did work!! It's a shame I didn't found that information..  :(
I must change the dynamic arrays for a list of pointer or is better to use a TList?

Quote
Can I export an object created inside the library?
Yes, but with limitations.
Which are those limitations?


Maybe I should create a tutorial with this info in the wiki with the things that work the ones that do not work...

Leledumbo

  • Hero Member
  • *****
  • Posts: 8836
  • Programming + Glam Metal + Tae Kwon Do = Me
Re: Strange Library Behaviour
« Reply #3 on: October 04, 2013, 11:35:14 am »
Quote
That explains why PChar did work!! It's a shame I didn't found that information..  :(
I must change the dynamic arrays for a list of pointer or is better to use a TList?
PChar DOES work, but you must treat it as ... PChar, not managed Pascal strings (Ansi-, Wide-, Unicode- or anything else). Meaning that returning PChar pointing to a string generated in the function WON'T work, but copying the characters in the string to a passed PChar parameter WILL, provided that the PChar points to valid memory region and the copying doesn't pass over the memory region boundary.
Quote
I must change the dynamic arrays for a list of pointer or is better to use a TList?
Other mechanism that doesn't suffer from mixed memory manager problem (each library has its own copy of the RTL, and thus, the memory manager).
Quote
Which are those limitations?
Other than managed types above, you must export objects as opaque pointer and provide procedural interface for the object's methods (first argument is the opaque pointer, the rest is the method parameters).

garlar27

  • Hero Member
  • *****
  • Posts: 652
Re: Strange Library Behaviour
« Reply #4 on: October 04, 2013, 09:45:22 pm »
Thank you very very very much!!

I've been reading some wiki articles (like Lazarus/FPC Libraries) and docs (like Using a library in a pascal program) among others and I don't remember reading anything like what you said... that's weird because this is important and is very hard to find relevant information in the forum and wiki searching for such a common word like "library"...

Quote
Which are those limitations?
Other than managed types above, you must export objects as opaque pointer and provide procedural interface for the object's methods (first argument is the opaque pointer, the rest is the method parameters).

I'm not shure if I fully understand this. Gotta read some more   %)  .

Nevertheless at times it doesn't look lika a good idea, I'd rather get a list of pointers instead. I'll experiment a little more and will post here what comes out.

BigChimp

  • Hero Member
  • *****
  • Posts: 5740
  • Add to the wiki - it's free ;)
    • FPCUp, PaperTiger scanning and other open source projects
Re: Strange Library Behaviour
« Reply #5 on: October 05, 2013, 10:48:20 am »
I've been reading some wiki articles (like Lazarus/FPC Libraries) and docs (like Using a library in a pascal program) among others and I don't remember reading anything like what you said... that's weird because this is important and is very hard to find relevant information in the forum and wiki searching for such a common word like "library"...
Obvious response: contributions to the wiki are more than welcome! There's a lot of complaints about missing information but a low percentage of people who try to rectify that by adding info... hope you can be one of them ;)
Want quicker answers to your questions? Read http://wiki.lazarus.freepascal.org/Lazarus_Faq#What_is_the_correct_way_to_ask_questions_in_the_forum.3F

Open source including papertiger OCR/PDF scanning:
https://bitbucket.org/reiniero

Lazarus trunk+FPC trunk x86, Windows x64 unless otherwise specified

garlar27

  • Hero Member
  • *****
  • Posts: 652
Re: Strange Library Behaviour
« Reply #6 on: October 07, 2013, 02:52:32 pm »
Maybe I should create a tutorial with this info in the wiki with the things that work the ones that do not work...

 ;D ;)

 

TinyPortal © 2005-2018