Recent

Author Topic: VERY STRANGE ERROR IN DLL WITH MEMORY  (Read 1761 times)

OlsonJon

  • Newbie
  • Posts: 2
VERY STRANGE ERROR IN DLL WITH MEMORY
« on: May 27, 2024, 06:13:33 pm »
Hello,

I wrote a dll in Lazarus to read a file (dxf file) and do few maths calculus and finally paint a bmp with the results of calculus. It runs on Windows 10.
To test the dll, I run a simply program (wrotten in Lazarus) that call dll functions and send it the file.
It's very strange, but if I try to run the dll with one file (FileA.dxf), the dll send an error of Write/Read in Memory.
If I run with other file (FileB.dxf), the dll run ok. After that, I could run the dll with first file (FileA.dxf) and runs ok. If I try to run again with second file (FileB.dxf) the dll fails.
If I check where the error appear, it's very strange because the error appear in a simple assignement of value to a local variable, for example:
str_a := str_b; where str_a and str_b are declared like local strings.
If I tested the dll with other program (wrotten in Delphi), it fails like with Lazarus program.
I don't know what I could do to be sure that the dll runs ok allways. I could have errors in calculus and dll fail, but I could check these errors and solve them, but if the dll fail like my description, it's impossible to build a dll and be sure that it runs.
Any idea about what is the fail?, something that I could check or do to aboid fails?
Thanks in advanage.

Thaddy

  • Hero Member
  • *****
  • Posts: 16197
  • Censorship about opinions does not belong here.
Re: VERY STRANGE ERROR IN DLL WITH MEMORY
« Reply #1 on: May 27, 2024, 06:35:23 pm »
Are you using Pascal string types in your dll?
Or do you use PChar types (c type strings)?
The latter is the recommended option if the dll is used by other languages than Delphi or FreePascal.
The former needs sharemem in both Delphi and FreePascal.
« Last Edit: May 28, 2024, 07:48:52 am by Thaddy »
If I smell bad code it usually is bad code and that includes my own code.

KodeZwerg

  • Hero Member
  • *****
  • Posts: 2269
  • Fifty shades of code.
    • Delphi & FreePascal
Re: VERY STRANGE ERROR IN DLL WITH MEMORY
« Reply #2 on: May 27, 2024, 07:19:33 pm »
Any idea about what is the fail?, something that I could check or do to aboid fails?
Line 123 is looking very suspicious!
Or in other words, show library code and sample code how you using it.
« Last Edit: Tomorrow at 31:76:97 xm by KodeZwerg »

jamie

  • Hero Member
  • *****
  • Posts: 6735
Re: VERY STRANGE ERROR IN DLL WITH MEMORY
« Reply #3 on: May 27, 2024, 08:33:01 pm »
it sounds to me like you are not initiating memory in some places, just writing over it.

Also, make sure all of your calls or either STDCALL or Cdel type and from the test app, do the same when calling the funcitons.
The only true wisdom is knowing you know nothing

OlsonJon

  • Newbie
  • Posts: 2
Re: VERY STRANGE ERROR IN DLL WITH MEMORY
« Reply #4 on: May 29, 2024, 03:41:59 pm »
Thank you all for your responses and help.
I have finally been able to solve the problem. After reviewing and investigating the mamory alocation and not finding the source of the problem, I decided to compile and run the dll in a demo version of Embarcadero XE12.
When running the dll, XE12 warned me that it was writing out of a vector. I was using a vector of n doubles and was writing at position n+1. Hence the problems with memory later.
Is there a way for Lazarus to notify me of these errors?

Thanks.

jamie

  • Hero Member
  • *****
  • Posts: 6735
Re: VERY STRANGE ERROR IN DLL WITH MEMORY
« Reply #5 on: May 29, 2024, 06:54:39 pm »
Things like that maybe header to catch using vectors, assuming you are referring to Generic Vectors.

At min, you should use RangeCheck + /{R+}

range checking will generate extra code to test against known ranges of arrays and such, but it does not catch all of it because at runtime, items can expand and get around range checking, but it's better than nothing.
The only true wisdom is knowing you know nothing

 

TinyPortal © 2005-2018