Freeing unmanaged memory within managed class 
Author Message
 Freeing unmanaged memory within managed class

This has been bugging me for a couple days.  I was wondering if there
are any easy ways to free unmanaged memory within a managed class.
Here's the situation:

I have two DLLs, ManagedDLL, and UnmanagedDLL.

UnmanagedDLL is a regular DLL loaded explicitly using LoadLibrary.  It
has a function called UnmanagedFunction which takes two parameters.
The first is a reference pointer, the second is a reference to an
integer.  The function allocates a large block of memory and returns
the pointer to the newly allocated memory in pChar.  It also returns
the size of the memory block in iInt and as a return value.  Ex:

int UnmanagedFunction(char*& pChar, int& iInt)
{
     iInt = 10000000; // size of array to create
     pChar = new char[iInt]; // create byte array
     // do stuff with character array
     return iInt; // just return something

Quote:
}

The ManagedDLL function needs to call the UnmanagedFunction using a
pointer that it can use the access the bytes in memory.  After it is
done with the memory, it needs to free it.  Can this be done without
making another call to the UnmanagedDLL to tell it to free it's
memory?  The managed function looks like:

int ManagedClass::CallUnmanagedDLLFunction()
{
     char* pByteArray; // Make pointer to a byte array
     int iSize; // Integer to get size of array
     int iReturn; // Integer to get return

     // call unmanaged function
     iReturn = UnmanagedDLLFunction(pByteArray, iSize);

     // do stuff with byte array pointed to by pByteArray
     // HOW DO I FREE MEMORY HERE??

Quote:
}

Thanks so much for your help in advance.

PS:  If you wouldn't mind could you cc replies to my e-mail address

Matt



Mon, 17 Oct 2005 06:53:12 GMT  
 Freeing unmanaged memory within managed class
Matt,

Quote:
> This has been bugging me for a couple days.  I was wondering if there
> are any easy ways to free unmanaged memory within a managed class.
> Here's the situation:

> I have two DLLs, ManagedDLL, and UnmanagedDLL.

> UnmanagedDLL is a regular DLL loaded explicitly using LoadLibrary.  It
> has a function called UnmanagedFunction which takes two parameters.
> The first is a reference pointer, the second is a reference to an
> integer.  The function allocates a large block of memory and returns
> the pointer to the newly allocated memory in pChar.  It also returns
> the size of the memory block in iInt and as a return value.  Ex:

> int UnmanagedFunction(char*& pChar, int& iInt)
> {
>      iInt = 10000000; // size of array to create
>      pChar = new char[iInt]; // create byte array
>      // do stuff with character array
>      return iInt; // just return something
> }

> The ManagedDLL function needs to call the UnmanagedFunction using a
> pointer that it can use the access the bytes in memory.  After it is
> done with the memory, it needs to free it.  Can this be done without
> making another call to the UnmanagedDLL to tell it to free it's
> memory?

You can probably get it to happen, but overall it's a bad idea. The only way
to get this reliably to work (with or without managed code, it has little to
do with the subject) is to get both libraries to share the same memory
allocator. Since in this case, you're using operator new, you're using the
CRT Heap allocator, which means that both modules need to share the copy of
the CRT. The only way to get this is to link both of them to the dll version
of the crt (msvcrt7.dll).

The reason why I think it's a bad idea is that this is non-intuitive, and
you have little way of enforcing this behavior at compile time, so in
general it leads to easier to maintain code if you keep allocation and
deallocation together in the same module (it also makes it easy to change
allocators later, if the need arises.)

--
Tomas Restrepo



Mon, 17 Oct 2005 07:02:25 GMT  
 
 [ 2 post ] 

 Relevant Pages 

1. Callback from unmanaged class to managed class...

2. Declaring unmanaged class in managed class.

3. Access managed class from unmanaged class

4. Using unmanaged class/data structure in managed class

5. Using unmanaged nested classes of a managed class

6. Managed and unmanaged memory allocation probs

7. VC7++ - Detection of memory leaks, mixed DLL (unmanaged/managed)

8. managed C++ wrapper around unmanaged C++ classes: causing StackOverflow exception

9. Making managed wrapper for legacy unmanaged classes.

10. Reference to an unmanaged pointer in a managed class

11. getting at a managed enum in an unmanaged class

12. Help me!! wrap unmanaged class with managed c++

 

 
Powered by phpBB® Forum Software