Why does the prompt "memory access error" appear after the DLL is used?

Source: Internet
Author: User
Why does the prompt "memory access error" appear after the DLL is used? Delphi/Windows SDK/API
Http://www.delphi2007.net/DelphiDB/html/delphi_20061215160532289.html
I wrote a DLL myself and used Delphi to call the dll I wrote in Delphi. The running result can be displayed, but a memory access error will be prompted after running. During the second call, I directly reported a memory access error and failed to run. In Program The record set is used as the returned record.
Uses
Sharemem,
......
VaR
......
Type
TPS = record
Sno: integer;
Sname: string;
SKT: string;
End;
TP = array of TPS;

My function is
Function sel (S: integer, VAR presult: TP): integer; export; stdcall;
Begin
S: = 0;
......
If (condition satisfied) then
Begin
S: = S + 1;
Setlength (presult, S );
Presult [s-1]. Sno: = data;
Presult [s-1]. sname: = A data;
Presult [s-1]. STK: = A data;
End;
End;

Please help me to see what's wrong!

Replace string type with struct string

It may be otherCodeThe reason for this issue is to use the annotated method or conditions to compile and exclude them one by one.

I tried it, that is, changing it to pchar still won't work. It should be a problem with this code, because it was good before this code was added, and there was a problem when it was added, after running, the execution result is yes, but a memory error is reported. During the second operation, even if I couldn't even run it, I directly encountered an error.

1. Do not use sharemem if there is no string return value. (sharemem sometimes has problems and I have encountered it too)
2. Change string to string [250] // if the data length cannot exceed 250
Or change to Char [1000];
Do not change to pchar because it needs to apply for memory

Use the low high function to access the record Array

I generally use pchar for DLL access, but I only need to write a few more parameters to avoid many problems.

TP = array of TPS;
PTP = ^ TP;

Function sel (S: integer, VAR presult: PTP): integer; export; stdcall;

Use Pointer instead for TP

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.