How to free C-allocated memory? 
Author Message
 How to free C-allocated memory?

Hello,

I have developed a C DLL with two functions: One allocates memory and returns
a pointer for it to VB (let's call it ALLOC). The other gets this pointer and
frees the memory (FREE).

Unfortunately, the pointer is somehow mixed up in VB, because it is not the
same when I call FREE. I tried several combinations like ByRef, ByVal, but
the program crashes every time.

Has somebody the right syntax for me (or, at least, the message "this cannot
work")?

Bye,
        Juergen
--
### Tierschutz im WWW:     http://www.*-*-*.com/
### Motorrad zu verkaufen: http://www.*-*-*.com/



Sun, 22 Apr 2001 03:00:00 GMT  
 How to free C-allocated memory?
There are probably ways to do it, but how depends on what you are
doing in VB with the memory. If you are storing the data in strings, the
simplest solution would be to use the SysAlloc functions. BSTRs
allocated that way conform exactly to VB's native string data type.
If you are allocating space for arrays, you may want to consider
using SAFEARRAYs. Ultimately though, an acceptable
dolutions depends as much on what you're trying to accomplish
(end result) as well as how you're trying to accomplish it.

--
Ron Ruble
Raffles Software Development, Inc.

Patient - Doctor, it hurts when I do this!
Doctor - Then don't do that.

Quote:

>Hello,

>I have developed a C DLL with two functions: One allocates memory and
returns
>a pointer for it to VB (let's call it ALLOC). The other gets this pointer
and
>frees the memory (FREE).

>Unfortunately, the pointer is somehow mixed up in VB, because it is not the
>same when I call FREE. I tried several combinations like ByRef, ByVal, but
>the program crashes every time.

>Has somebody the right syntax for me (or, at least, the message "this
cannot
>work")?

>Bye,
> Juergen
>--
>### Tierschutz im WWW:     http://members.xoom.com/ginkel
>### Motorrad zu verkaufen: http://members.xoom.com/ginkel/vx800.html



Sun, 22 Apr 2001 03:00:00 GMT  
 How to free C-allocated memory?
If this is a stock DLL, then this certainly can work, so we'd have to see at
least the C prototypes and the VB Declares to know why it isn't working for
you.

You probably should use VirtualAlloc / VirtualFree rather than any C
language equivalents like malloc / free, for complete compatibility.  But
either way, if the DLL is in-process then a pointer is a pointer and should
work fine between C and VB.

--
     Jim Mack
     MicroDexterity, Inc

     http://www.microdexterity.com



Quote:

>Hello,

>I have developed a C DLL with two functions: One allocates memory and
returns
>a pointer for it to VB (let's call it ALLOC). The other gets this pointer
and
>frees the memory (FREE).

>Unfortunately, the pointer is somehow mixed up in VB, because it is not the
>same when I call FREE. I tried several combinations like ByRef, ByVal, but
>the program crashes every time.

>Has somebody the right syntax for me (or, at least, the message "this
cannot
>work")?



Sun, 22 Apr 2001 03:00:00 GMT  
 How to free C-allocated memory?
Juergen, the following worked for me

VB code
-----------
Private Declare Function alloc1 Lib alloc.dll (ByVal size As Long) As Long
Private Declare Function free1 Lib alloc.dll  (ByVal address As Long) As
Long

Private Sub Command1_Click()
  Dim address As Long
  address = alloc1(10000)
  MsgBox address = & Hex$(address)
  free1 (p)
End Sub

C++ code (DLL)
----------------------
#include <malloc.h>
#include <stdio.h>
#include <windows.h>

extern C  long __stdcall alloc1(long size)
{
  char *address = (char*)malloc(size);
  char s[50];
  if (address) {
    sprintf(s, Allocated memory at %p, address);
    MessageBox(NULL, s, Message from DLL, 0);
  }
  return (long) address;

Quote:
}

extern C void __stdcall free1(long address)
{
  char *p = (char*)address;
  char s[50];
  sprintf(s, Freeing memory at %p, p);
  MessageBox(NULL, s, Message from DLL, 0);
  if (p)
    free(p);

Quote:
}

>Hello,

>I have developed a C DLL with two functions: One allocates memory and
returns
>a pointer for it to VB (let's call it ALLOC). The other gets this pointer
and
>frees the memory (FREE).

>Unfortunately, the pointer is somehow mixed up in VB, because it is not the
>same when I call FREE. I tried several combinations like ByRef, ByVal, but
>the program crashes every time.

>Has somebody the right syntax for me (or, at least, the message "this
cannot
>work")?

>Bye,
> Juergen
>--
>### Tierschutz im WWW:     http://members.xoom.com/ginkel
>### Motorrad zu verkaufen: http://members.xoom.com/ginkel/vx800.html



Sun, 22 Apr 2001 03:00:00 GMT  
 
 [ 4 post ] 

 Relevant Pages 

1. How to set free memory allocated by an array dynamically created

2. How to free C-allocated memory?

3. allocating memory

4. Allocating Memory and Structures.

5. Allocating memory

6. Allocate Memory

7. Question: Accessing memory allocated in DLL

8. Allocating memory

9. Allocating Memory in a Called Function

10. Accessing another Apps allocated memory (w/vb)

11. How much memory I can allocate?

12. Memory allocated by parameters

 

 
Powered by phpBB® Forum Software