BCB6 + memory allocated by DLL but free by application

A

anirudhlohia

Hi All,

Environment: BCB6 on XP

If the code example below is compile with "Use Dynamic RTL", "Build
with Runtime Packages" project option *checked* then everything works
fine

However, if either or both the options are *unchecked* in the
application then application crashes at "delete[] encoded Message".


Application Code:

-----------------------------------------------------------------
unsigned char* encodedMessage = 0x0;
..................
..................
encode_message(messageNumber, rLen, encodedMessage)
..................
..................
delete[] encodedMessage;

-----------------------------------------------------------------
DLL Exported Method signature:

-----------------------------------------------------------------
int __EXPORT_TYPE encode_message(unsigned int messageNumber,
unsigned int& rLen,
unsigned char*& pMessage);
-----------------------------------------------------------------

The DLL allocates the memory using new. The Calling application is
supposed to release the memory.


Any idea why it may be happening so?

Thanks
Anirudh
 
J

Jack Klein

Hi All,

Environment: BCB6 on XP

If the code example below is compile with "Use Dynamic RTL", "Build
with Runtime Packages" project option *checked* then everything works
fine

However, if either or both the options are *unchecked* in the
application then application crashes at "delete[] encoded Message".

DLLs are not part of the C++ language, they are a mechanism provided
by your operating system supported by extensions in your compiler.
That makes them a compiler and OS specific issue, not a language one,
and off-topic here.

Ask in on Borland's server
newsgroups.borland.com.
 
A

Alan Johnson

Hi All,

Environment: BCB6 on XP

If the code example below is compile with "Use Dynamic RTL", "Build
with Runtime Packages" project option *checked* then everything works
fine

However, if either or both the options are *unchecked* in the
application then application crashes at "delete[] encoded Message".


Application Code:

-----------------------------------------------------------------
unsigned char* encodedMessage = 0x0;
.................
.................
encode_message(messageNumber, rLen, encodedMessage)
.................
.................
delete[] encodedMessage;

-----------------------------------------------------------------
DLL Exported Method signature:

-----------------------------------------------------------------
int __EXPORT_TYPE encode_message(unsigned int messageNumber,
unsigned int& rLen,
unsigned char*& pMessage);
-----------------------------------------------------------------

The DLL allocates the memory using new. The Calling application is
supposed to release the memory.


Any idea why it may be happening so?

Thanks
Anirudh

Don't free memory/resources in a different module (EXE, DLL, etc) from
that in which it was allocated. It is not uncommon for each module to
get its own copy of whatever code goes on behind the scenes to manage
the heap, and the heap manager in the freeing module doesn't know how to
correctly free memory allocated by the heap manager in the allocating
module.

I'm not sure what those compiler switches do, but I'd guess they cause
your program to dynamically link to the runtime library, which could
mean both modules wind up using the same instance of heap management code.

Of course, most of that is off-topic for c.l.c++.

-Alan
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
473,755
Messages
2,569,534
Members
45,008
Latest member
Rahul737

Latest Threads

Top