Re: new - delete issue



So, if I need to get data from a DLL - e.g. a struct:

EXE calls a DLL exported function that return a struct pointer.

DLL allocates memory for the struct and save this pointer in a list, than
return pointer to EXE.

EXE do something...

The EXE call another exported function that delete all pointers in that list.

Is it correct!? This is a really new way to follow up!!!!!

Ale

"David Wilkinson" wrote:

> Ale wrote:
>
> > Take a look at this:
> >
> > DLL, loaded into address space of main thread.
> >
> > In Main Thread:
> > MYSTRUCT *p = MYDLL.GetStruc();
> >
> > .....
> >
> > AfxIsValidAddress( p, sizeof( MYSTRUCT ) ); // Doesn't Fail
> >
> >> delete p; // Fail
> >
> >
> > MYDLL GetStruct() exported function:
> > MYSTRUCT* p = new MYSTRUCT
> > ....
> > return p;
> >
> > Really strange, delete p in Main Thread fails.
> > It fails an ASSERT : _ASSERTE(_CrtIsValidHeapPointer(pUserData)); into
> > _free_dbg
> >
> > Before performing "delete p" the pointer is OK, i've taken a look at memory
> > and everythig is OK!!!!
> >
> > Thanks
> > Ale
> >
>
> Ale:
>
> I have to say I disagree with the direction you are being pushed in
> here. If you want to use static linking you should use it. You just need
> another exported function in the DLL:
>
> void DeleteStruct(MYSTRUCT* p)
> {
> delete p;
> }
>
> Use this function to delete the object from the main thread. In any
> case, IMHO, it is not a good practice to delete an object in a different
> place from where you created it.
>
> HTH,
>
> David Wilkinson
>
>
.



Relevant Pages

  • Re: Notify multiple instances of my COM dll to take an action
    ... my dll default interface is derived from IUnknown and the exe ... passing the pointer into the exe but the method call on that pointer ...
    (microsoft.public.vc.atl)
  • Re: Critical section shared betwen dll and main program
    ... different components like EXE and DLL. ... old version of CRITICAL_SECTION and passes a pointer to it which is used ... There is no problem with passing its address between exe and DLL. ...
    (microsoft.public.win32.programmer.kernel)
  • Re: Exporting function from EXE problem
    ... ActiveX component which is actually DLL. ... Only for testing reasons I was ... routine into DLL and envelop it into EXE. ... exported function and need to be able to call that function from other exe ...
    (microsoft.public.vc.language)
  • Re: Structure containing string across DLL boundary
    ... Exe and DLL both built with static RTL. ... Debug build has no issues. ... The DLL allocates the structure with the embedded string. ... The function in the exe is invoked with a pointer to the structure and then ...
    (microsoft.public.vc.stl)
  • Re: Critical section shared betwen dll and main program
    ... I would not suggest to pass pointer to any structure between ... different components like EXE and DLL. ... Binary Environment) bug. ... EXE, which was compiled in a newer environment, with another ...
    (microsoft.public.win32.programmer.kernel)