Recent

Author Topic: [Solved] DLL crash  (Read 3176 times)

cappe

  • Full Member
  • ***
  • Posts: 191
[Solved] DLL crash
« on: June 20, 2018, 11:44:34 am »
I wrote a dll with this call:
function GetA (S1: PChar; v1: PChar; A: LongBool; var info: csRec): LongBool; cdecl;

the call is    GetA(PChar('pale33'), PChar('0000'), True, info);

The problem is that in some cases (changing the values of S1 and v1), after the call to this dll, the program closes with this error "Process Exit: 0331".

What's wrong?
« Last Edit: June 22, 2018, 11:32:14 am by cappe »

RayoGlauco

  • Full Member
  • ***
  • Posts: 176
  • Beers: 1567
Re: DLL crash
« Reply #1 on: June 20, 2018, 01:12:46 pm »
Based on the few DLL functions that I have written in my life, your function's header would have looked like this:

Code: Pascal  [Select][+][-]
  1. type
  2.   PcsRec = ^csRec;
  3.  
  4. function GetA (S1: PChar; v1: PChar; A: integer; info: PcsRec): integer; stdcall;

Where an integer=0 is False and an integer<>0 is true.

And the call would be:

Code: Pascal  [Select][+][-]
  1. GetA(PChar('pale33'), PChar('0000'), 1, @info);

But I'm not an expert at all.
« Last Edit: June 20, 2018, 01:28:22 pm by RayoGlauco »
To err is human, but to really mess things up, you need a computer.

Ñuño_Martínez

  • Hero Member
  • *****
  • Posts: 1186
    • Burdjia
Re: DLL crash
« Reply #2 on: June 20, 2018, 05:51:36 pm »
In my experience, you can use VAR parameters as they're translated in to pointers by FPC itself.  Also use STDCALL instead of CDECL as STDCALL is more cross platform (IIRC).
[edit]
I've read the documentation and my assumptions were wrong.  Use CDECL or STDCALL depending of what will you do (for example call from C or no).  Read the document I've linked above for more information.
« Last Edit: June 20, 2018, 06:01:15 pm by Ñuño_Martínez »
Are you interested in game programming? Join the Pascal Game Development community!
Also visit the Game Development Portal

marcov

  • Administrator
  • Hero Member
  • *
  • Posts: 11383
  • FPC developer.
Re: DLL crash
« Reply #3 on: June 20, 2018, 05:53:37 pm »
If the dll e.g. tries to freemem the pchar, this is only natural.

You can make things crash in a lot of ways, not just with declarations :-)

cappe

  • Full Member
  • ***
  • Posts: 191
Re: DLL crash
« Reply #4 on: June 21, 2018, 11:06:58 am »
Based on the few DLL functions that I have written in my life, your function's header would have looked like this:

Code: Pascal  [Select][+][-]
  1. type
  2.   PcsRec = ^csRec;
  3.  
  4. function GetA (S1: PChar; v1: PChar; A: integer; info: PcsRec): integer; stdcall;

Where an integer=0 is False and an integer<>0 is true.

And the call would be:

Code: Pascal  [Select][+][-]
  1. GetA(PChar('pale33'), PChar('0000'), 1, @info);

But I'm not an expert at all.

same problem as with the first solution

RayoGlauco

  • Full Member
  • ***
  • Posts: 176
  • Beers: 1567
Re: DLL crash
« Reply #5 on: June 21, 2018, 12:54:36 pm »
I think we need to see the code inside this dll function.
If it was my code, I would debug step by step until the error.

Edit: to debug a DLL, create a program that uses it. Then open the DLL project and set the program path in menu Run / Run parameters --> Local --> Host application (see image).
« Last Edit: June 21, 2018, 02:52:51 pm by RayoGlauco »
To err is human, but to really mess things up, you need a computer.

reinerr

  • New Member
  • *
  • Posts: 37
Re: DLL crash
« Reply #6 on: June 22, 2018, 07:12:04 am »
As already suggested, the most likely cause is freeing of memory inside the DLL that the PChars are pointing to. It may be that most of the time the memory is still valid for long enough but occasionally not.

cappe

  • Full Member
  • ***
  • Posts: 191
[Solved] Re: DLL crash
« Reply #7 on: June 22, 2018, 11:30:55 am »
What you wrote to me works. There was also another problem. Thanks to everyone and W Lazarus.

 

TinyPortal © 2005-2018