Delphi-PRAXiS
Seite 1 von 2  1 2      

Delphi-PRAXiS (https://www.delphipraxis.net/forum.php)
-   Object-Pascal / Delphi-Language (https://www.delphipraxis.net/32-object-pascal-delphi-language/)
-   -   Delphi Class with strings in DLL (https://www.delphipraxis.net/157173-class-strings-dll.html)

WojTec 1. Jan 2011 17:50

Delphi-Version: 2010

Class with strings in DLL
 
I have a problem, I need to use class in DLL, this class as normal classes has strings as properties and parameters.

I have global variable (object of class), exported functions uses PChar for strings and works on that global variable. DLL compiles. When I try use any function from it, raises AV.

F1 :(

Sir Rufo 1. Jan 2011 19:13

AW: Class with strings in DLL
 
U have to use the Unit sharemem (in DLL and Application)

himitsu 1. Jan 2011 20:18

AW: Class with strings in DLL
 
To share
Delphi-Quellcode:
string
is needed Delphi-Referenz durchsuchenShareMem.

Classes may not be shared, because using the EXE and DLL separate RTTIs.
> runtime packages

WojTec 2. Jan 2011 10:30

Re: Class with strings in DLL
 
I thought that this unit is required if parameters are string not PChar. This is bad news :( I checked that Borlndmm.dll must be present, this is not acceptable for me, do you know pure Delphi solution (with no additional libs)?

JamesTKirk 2. Jan 2011 11:24

AW: Class with strings in DLL
 
Try the unit "FastMM" instead. If you really use Delphi 2010 it should already be part of the provided units.
It's a solution without another DLL, but you need to use that memory manager in the DLL and the EXE. If you need to interface with another programming language like C you can not use that way, but you must manage the PChars yourself.

Question: Why did you choose "Free Pascal" as language?

Regards,
Sven

WojTec 2. Jan 2011 11:55

Re: Class with strings in DLL
 
I just googled this unit:

Code:
http://www.codexterity.com/fastsharemem.htm
I didn't knew that FastMM4 unit can be used as replacement too, thanks for this tip :D

I'm using D2010, FreePascal I selected by mistake, sorry :(

I have next question, I used global (for DLL) record (it contains string and integers). When I used FastMM it detected memory leaks, when I nulled this record (rec.string := '', etc) memory leaks not occured. Why?

JamesTKirk 2. Jan 2011 12:18

AW: Re: Class with strings in DLL
 
Zitat:

Zitat von WojTec (Beitrag 1071753)
I just googled this unit:

Code:
http://www.codexterity.com/fastsharemem.htm
I didn't knew that FastMM4 unit can be used as replacement too, thanks for this tip :D

I believe that FastMM4 is better than fastsharemem, especially as it allows you to detect memory leaks.

Zitat:

Zitat von WojTec
I'm using D2010, FreePascal I selected by mistake, sorry :(

Nobody is perfect. I was just curious. ;)

Zitat:

Zitat von WojTec
I have next question, I used global (for DLL) record (it contains string and integers). When I used FastMM it detected memory leaks, when I nulled this record (rec.string := '', etc) memory leaks not occured. Why?

When do you initialize that record? It could be a false alarm, but I'm not that fond with FastMM as I'm not using it as leak detector.

Regards,
Sven

WojTec 2. Jan 2011 14:02

Re: Class with strings in DLL
 
So, it's global variable in DLL project file. Initialization id done by call specified function, function fills record with data. To null it I must call next function, because DLL entry procedure seems to not work.

Delphi-Quellcode:
procedure DLLIO(dwReason: DWORD);
begin
  case dwReason of
    DLL_PROCESS_ATTACH: {...};
    DLL_PROCESS_DETACH: {...};
    end;
  end;
end;

begin
  DllProc := @DLLIO;
end.

WojTec 7. Jan 2011 16:58

Re: Class with strings in DLL
 
I did it with external memory manager :)

But now I need use functions and classes with string parameters, I won't use memory manager, just PChar. How can I do it?

SirThornberry 7. Jan 2011 17:08

AW: Class with strings in DLL
 
If you use PChar and so you don't need Sharemem.
Zitat:

When I try use any function from it, raises AV
Do you have this Problem only if you have strings in your dll? I suppose you have source like this:
Delphi-Quellcode:
function GetString(): PChar;
var
  mystring: string;
begin
  mystring := 'hello world';
  result := myString;
end;
If you have such source code you will get an AV because PChar is only a Pointer (of a special type). But after leaving this funktion "mystring" is freed and so "result" points to freed memory.

It would be interesting if you get the AV during the call or later when you use the result.


Alle Zeitangaben in WEZ +1. Es ist jetzt 18:52 Uhr.
Seite 1 von 2  1 2      

Powered by vBulletin® Copyright ©2000 - 2024, Jelsoft Enterprises Ltd.
LinkBacks Enabled by vBSEO © 2011, Crawlability, Inc.
Delphi-PRAXiS (c) 2002 - 2023 by Daniel R. Wolf, 2024 by Thomas Breitkreuz