Solved

C++-DLL and Delphi 3

Posted on 1998-11-09
1
200 Views
Last Modified: 2010-04-04
Hello,

I've written a DLL in Borland C++ 5.0.  One of the functions is a simple
concatenation function :

char* __stdcall _export StringTest(char* Str1, char* Str2)
{
   char* tmp;

   int l1 = strlen(Str1);
   int l2 = strlen(Str2);

   tmp = (char*) malloc(l1+l2+1);
   strcpy(tmp, Str1);
   strcat(tmp,Str2);
   return tmp;
}

This function works fine, but it has a memory leak : the memory
allocated for tmp is not released.

This function is called from a Delphi 3 application.  Can anyone tell me
how I can release the allocated memory, prefarably from the Delphi
application ?
I've tried freeing the memory just before the return instruction, but it
doesn't work.

Thanks in advance,

Johan

0
Comment
Question by:ppr
1 Comment
 
LVL 1

Accepted Solution

by:
AndersWP earned 200 total points
ID: 1346252
ppr:

To manage memory from both Delphi and Borland C++ (or any other windows programming tool), you should use the WinAPI memory functions. I have written two code snippets in Pascal that shows how to do malloc- and free-like functions using the WinAPI. I trust you can convert to C (or whatever) and add error handling as needed:

Function WinMalloc(aSize: Integer): Pointer;

Begin
  Result:=Pointer(GlobalAlloc(GMEM_FIXED, aSize));
End;

Procedure WinFree(aPointer: Pointer);

Begin
    GlobalFree(GlobalHandle(aPointer));
End;

I hope this will help,

AndersWP
0

Featured Post

U.S. Department of Agriculture and Acronis Access

With the new era of mobile computing, smartphones and tablets, wireless communications and cloud services, the USDA sought to take advantage of a mobilized workforce and the blurring lines between personal and corporate computing resources.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Suggested Solutions

Title # Comments Views Activity
Sending Gmail through Delphi 3 108
Path  to current project in Delphi. 2 80
Delphi - replicating a form 8 75
Create Database on Android via Delphi dbExpress 3 92
This article explains how to create forms/units independent of other forms/units object names in a delphi project. Have you ever created a form for user input in a Delphi project and then had the need to have that same form in a other Delphi proj…
In my programming career I have only very rarely run into situations where operator overloading would be of any use in my work.  Normally those situations involved math with either overly large numbers (hundreds of thousands of digits or accuracy re…
This video shows how to use Hyena, from SystemTools Software, to bulk import 100 user accounts from an external text file. View in 1080p for best video quality.
Nobody understands Phishing better than an anti-spam company. That’s why we are providing Phishing Awareness Training to our customers. According to a report by Verizon, only 3% of targeted users report malicious emails to management. With compan…

809 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question