+ -
当前位置:首页 → 问答吧 → dll pchar string getmem 内存泄露

dll pchar string getmem 内存泄露

时间:2011-08-16

来源:互联网

最近再写 dll 为了通用性(c#可以调用) 输出函数肯定用pchar 作为参数 和 返回值了 这是毫无疑问的 

查阅了些资料 
http://blog.sina.com.cn/s/blog_4ff1251f0100dsu6.html
并且在其他地方下到一些代码 
Delphi(Pascal) code

function StrPch(const stPas: string): PChar;
// Pascal -> PChar
// 直接使用 PChar 转化有时会转化出错
begin
  GetMem(Result, Length(stPas) + 1);
  StrPCopy(Result, stPas);
end;

function GetSpellCode(szText: PChar; iMode, iCount: Integer): PChar;
// Call MakeSpellCode
begin
  Result := StrPch(MakeSpellCode(String(szText), iMode, iCount));  //这里直接用pchar 有时会报错? 搞不懂
end;



Delphi(Pascal) code

function GetAdminPath: ShortString; stdcall;
var
  vPath: pchar
  vSize: Integer;
begin

  vSize := MAX_PATH;
  vPath := GetMemory(vSize);
  ExpandEnvironmentStrings('%HOMEDRIVE%%HOMEPATH%', vPath, MAX_PATH);
  //ShowMessage(StrPas(vPath));
  Result := StrPas(vPath);  end;



都用到了 内存分配函数 可没有释放 势必会造成内存泄露吧(CheckMem.pas检查有)


请问 dll 涉及输出函数中 string 标准写法 如何?

作者: chinawcs   发布时间: 2011-08-16

问题1 : 再dll中分配内存还是exe中

问题2 : 再dll中分配内存会自动释放吗


问题3 :dll 返回 pchar 会有不稳定的情况 ?

作者: chinawcs   发布时间: 2011-08-16

我在DLL中这样用还没出现过问题

Delphi(Pascal) code
//********取得当前可执行文件名
procedure GetApplicationName(var Lp:String);StdCall;
begin
  StrCopy(PChar(Lp),PChar(ChangeFileExt(ExtractFileName(Application.ExeName),'')));
end;

作者: hongss   发布时间: 2011-08-16

热门下载

更多