Hi all, my application uses callback function in DLL (Written in C),

The DLL sniff's the network card and then allocates new data dynamically
using malloc() function ,

Then the data (pointer) passed to the host application (MFC),

After I use the data and copying it, I'm trying to free the space using
Free()

But I get an Assertion error,

After running step by step inside MS code I see that free() function check's
if the new memory was allocated inside the current app (local) , if the
memory wasn't allocated inside then Assertion error occur .



How do I free memory from other application?





[DLL]

| |

| |

[App]



steps :

1.. Application init the DLL and run it, the DLL now runs separately from
the app .
2.. when ever the DLL find network data , it will allocate memory space
,and process incoming data
3.. Then it will notify the application using callback function, and pass
the application a pointer to the new allocated space.
4.. the application uses the memory
5.. Application try to free the memory using the free function - Assertion
Error !!!!!


Env : Vs.net IDE (NO DotNet involved , pure c/c++ code .)

Win xp





Thank you very much

Sharon.G

Re: Assertion error when using Free (non-local pointer) by David

David
Wed Feb 23 15:03:47 CST 2005

Sharon Gorev wrote:

> Hi all, my application uses callback function in DLL (Written in C),
>
> The DLL sniff's the network card and then allocates new data dynamically
> using malloc() function ,
>
> Then the data (pointer) passed to the host application (MFC),
>
> After I use the data and copying it, I'm trying to free the space using
> Free()
>
> But I get an Assertion error,
>
> After running step by step inside MS code I see that free() function check's
> if the new memory was allocated inside the current app (local) , if the
> memory wasn't allocated inside then Assertion error occur .
>
>
>
> How do I free memory from other application?
>

Sharon:

Write a function in the DLL to free the memory, and call it from the App:

void FreeMemory(void* memblock)
{
free(memblock);
}

HTH,

David Wilkinson

Re: Assertion error when using Free (non-local pointer) by Sharon

Sharon
Thu Feb 24 04:31:25 CST 2005

Thank you David



Well eventually I will have no choice but to add another Free() callback to
the dll ,



Isn't there more elegant way?



My friend suggested to reallocate 0 memory size using realloc ,
Realloc(ptr,0)

I haven't tried it yet

Thank you

Sharon.G



"David Wilkinson" <no-reply@effisols.com> wrote in message
news:eiPIrseGFHA.2280@TK2MSFTNGP15.phx.gbl...
> Sharon Gorev wrote:
>
>> Hi all, my application uses callback function in DLL (Written in C),
>>
>> The DLL sniff's the network card and then allocates new data dynamically
>> using malloc() function ,
>>
>> Then the data (pointer) passed to the host application (MFC),
>>
>> After I use the data and copying it, I'm trying to free the space using
>> Free()
>>
>> But I get an Assertion error,
>>
>> After running step by step inside MS code I see that free() function
>> check's if the new memory was allocated inside the current app (local) ,
>> if the memory wasn't allocated inside then Assertion error occur .
>>
>>
>>
>> How do I free memory from other application?
>>
>
> Sharon:
>
> Write a function in the DLL to free the memory, and call it from the App:
>
> void FreeMemory(void* memblock)
> {
> free(memblock);
> }
>
> HTH,
>
> David Wilkinson



Re: Assertion error when using Free (non-local pointer) by Tom

Tom
Thu Feb 24 08:50:39 CST 2005

Sharon Gorev wrote:
> Thank you David
>
>
>
> Well eventually I will have no choice but to add another Free() callback to
> the dll ,
>
>
>
> Isn't there more elegant way?
>
>
>
> My friend suggested to reallocate 0 memory size using realloc ,
> Realloc(ptr,0)
>
> I haven't tried it yet

The problem is that the DLL and your app are using different C runtime
heaps. If you use malloc in the DLL it is allocated from the DLLs heap,
which means it must be freed there or you'll end up asserting, or worse,
corrupting the heap. There are a few ways around this:

1. Compile your .exe and .dll using the same DLL version of the CRT,
either both with "Multithreaded DLL" or both with "Multithreaded Debug
DLL". That way both the DLL and the .exe will be using the same CRT
heap. The options can be found in the "Code Generation" options.

2. Free the memory in the DLL. This involves adding a new exported
function that you can pass things to be freed to.

3. Don't use malloc and free. You can use GlobalAlloc/Free, or the Heap*
functions (make sure you use the same handle for all allocations!).

4. Encapsulate memory management. This either involves using a smart
pointer in the DLL API, or, probably better, doing 2) but adding a new
header to enable easy use of the DLL. e.g. the exe does:

inline shared_ptr<Foo> createFoo(args)
{
Foo* dllFoo = dllCreateFoo(args);
return shared_ptr<Foo>(dllFoo, &dllDestroyFoo);
}

5. Others?

Tom