Click here to Skip to main content
16,007,087 members
Home / Discussions / C / C++ / MFC
   

C / C++ / MFC

 
GeneralRe: Memory leak Pin
Tim Deveaux9-Apr-01 13:45
Tim Deveaux9-Apr-01 13:45 
GeneralRe: Memory leak Pin
11-Apr-01 8:26
suss11-Apr-01 8:26 
GeneralDLL memory Allocation Pin
Conor Hunt9-Apr-01 6:42
Conor Hunt9-Apr-01 6:42 
GeneralRe: DLL memory Allocation Pin
9-Apr-01 7:13
suss9-Apr-01 7:13 
GeneralRe: DLL memory Allocation Pin
9-Apr-01 7:17
suss9-Apr-01 7:17 
GeneralRe: DLL memory Allocation Pin
Conor Hunt9-Apr-01 7:25
Conor Hunt9-Apr-01 7:25 
GeneralRe: DLL memory Allocation Pin
MikeG9-Apr-01 7:51
MikeG9-Apr-01 7:51 
GeneralRe: DLL memory Allocation Pin
Conor Hunt9-Apr-01 8:28
Conor Hunt9-Apr-01 8:28 
That is a good idea. I'm not sure that I want to use it in this case though?

The function in the DLL is called many times and returns many character buffers. That means that I have to keep track of all of these allocated character buffers in the dll, so that wehn DestroyXXX is called I free them all. I also can't have a DestroyLastAllocatedBuffer() function that frees the most recently allocated character buffer because the function in the DLL can be called by many different threads. (caveat: you probably could do both of the above with some extra work, but it might be more work than it is worth?)

I'd prefer for the calling application to be able to call the DLL function to get a characted buffer and then free it right after it is done with the buffer.

I think I have another solution though. You can create a C++ object with a destructor that does the memory freeing. It seems to work, anyone see any problems?

// Text holder class
CTextHolder
{
public:
CTextHolder()
~CTextHolder() { delete text; };
char *text;
};

// This is the exported function in the dll
extern "C"
{
CTextHolder * getText()
{
CTextHolder *blah = new CTextHolder();
blah->text = new char[20];
return blah;
}
}

// This is the function in the application that calls the exported function in the DLL
void testCall(void)
{
HMODULE mod = LoadLibrary("d:\\plugin.dll");

if(mod == NULL)
return;

// Test function is typdefed as.. typedef CTextHolder * (*aFunc)(void);
testFunction getText = (testFunction) GetProcAddress(mod, "getText");

if(getText == NULL)
return;

CTextHolder *blah = getText();
// Yay.. this seems to work fine
delete blah;

FreeLibrary(mod);
}
GeneralRe: DLL memory Allocation Pin
MikeG10-Apr-01 14:58
MikeG10-Apr-01 14:58 
GeneralRe: DLL memory Allocation Pin
Conor Hunt10-Apr-01 15:55
Conor Hunt10-Apr-01 15:55 
GeneralRe: DLL memory Allocation Pin
10-Apr-01 14:18
suss10-Apr-01 14:18 
GeneralRe: DLL memory Allocation Pin
11-Apr-01 1:17
suss11-Apr-01 1:17 
GeneralRasDial Problem Pin
Matt James9-Apr-01 6:06
Matt James9-Apr-01 6:06 
GeneralRe: RasDial Problem Pin
Masaaki Onishi9-Apr-01 7:07
Masaaki Onishi9-Apr-01 7:07 
GeneralAdding line numbers in an edit control when the user presses the return key Pin
Joan M9-Apr-01 5:53
professionalJoan M9-Apr-01 5:53 
GeneralMFC - MDI and threads Pin
Tim Burke9-Apr-01 4:07
Tim Burke9-Apr-01 4:07 
GeneralCant open MS Access 2000 db Pin
Ammar9-Apr-01 0:33
Ammar9-Apr-01 0:33 
GeneralRe: Cant open MS Access 2000 db Pin
Masaaki Onishi9-Apr-01 6:59
Masaaki Onishi9-Apr-01 6:59 
GeneralRe: Cant open MS Access 2000 db Pin
9-Apr-01 9:55
suss9-Apr-01 9:55 
GeneralRe: Cant open MS Access 2000 db Pin
Ammar9-Apr-01 20:47
Ammar9-Apr-01 20:47 
GeneralRe: Cant open MS Access 2000 db Pin
Ammar10-Apr-01 21:44
Ammar10-Apr-01 21:44 
GeneralControls Pin
Drake Elsari8-Apr-01 19:23
Drake Elsari8-Apr-01 19:23 
GeneralRe: Controls Pin
Christian Graus8-Apr-01 19:36
protectorChristian Graus8-Apr-01 19:36 
GeneralRe: Controls Pin
Erik Funkenbusch8-Apr-01 19:51
Erik Funkenbusch8-Apr-01 19:51 
GeneralDialog box prohibits subsequent MessageBox Pin
KeithW8-Apr-01 17:31
KeithW8-Apr-01 17:31 

General General    News News    Suggestion Suggestion    Question Question    Bug Bug    Answer Answer    Joke Joke    Praise Praise    Rant Rant    Admin Admin   

Use Ctrl+Left/Right to switch messages, Ctrl+Up/Down to switch threads, Ctrl+Shift+Left/Right to switch pages.