memory management in dll 
Author Message
 memory management in dll

Is there a article about memory management in dll? For example,

Class Data{
    int size;
    char *pdate;
    Data(int num) : size(num), pdata(new char[num]){}
    ~Data(){delete[] pdata;}

Quote:
};

Class Proc{
  ...
   Data *GetData(){
    return new Data(100);
   }

Quote:
};

I have these classes inside a dll.

Here is my main:

DataProc proc;
Data* pData = Proc.GetData();
Delete pData;

Is this a problem? Since the memory is allocated in dll, it should be
freeed in dll. How can I handle this situation?

Thanks,

qq



Wed, 14 Sep 2005 17:31:31 GMT  
 memory management in dll

Quote:
>Is this a problem? Since the memory is allocated in dll, it should be
>freeed in dll. How can I handle this situation?

You'd need to call a method in your DLL to have it delete the
allocated data.

Alternatively, if you can ensure that your DLL & EXE are built with
the same version of the compiler, and both use the DLL version of the
run-time library, they'll both share the same heap, so the problem
disappears.

Oh, and don't mix debug & release builds as they have different memory
managers.

Dave
--
MVP VC++ FAQ: http://www.mvps.org/vcfaq



Thu, 15 Sep 2005 01:35:34 GMT  
 memory management in dll
Quote:

> >Is this a problem? Since the memory is allocated in dll, it should be
> >freeed in dll. How can I handle this situation?

> You'd need to call a method in your DLL to have it delete the
> allocated data.

Is the call "delete pData" work?

Thanks,

qq

Quote:
> Alternatively, if you can ensure that your DLL & EXE are built with
> the same version of the compiler, and both use the DLL version of the
> run-time library, they'll both share the same heap, so the problem
> disappears.

> Oh, and don't mix debug & release builds as they have different memory
> managers.

> Dave



Thu, 15 Sep 2005 16:37:56 GMT  
 memory management in dll

Quote:


> > >Is this a problem? Since the memory is allocated in dll, it should be
> > >freeed in dll. How can I handle this situation?

> > You'd need to call a method in your DLL to have it delete the
> > allocated data.

> Is the call "delete pData" work?

> Thanks,

> qq

Your "delete pData" calls the exe memory manager, but the corresponding
"new" called the DLL memory manager.  So if the exe and the DLL have a
different memory manager the delete will not work.  There are two
solutions, both already described in the answers to your question.

--
Scott McPhillips [VC++ MVP]



Thu, 15 Sep 2005 19:49:54 GMT  
 memory management in dll

Quote:



> > > >Is this a problem? Since the memory is allocated in dll, it should be
> > > >freeed in dll. How can I handle this situation?

> > > You'd need to call a method in your DLL to have it delete the
> > > allocated data.

> > Is the call "delete pData" work?

> > Thanks,

> > qq

> Your "delete pData" calls the exe memory manager, but the corresponding
> "new" called the DLL memory manager.  So if the exe and the DLL have a
> different memory manager the delete will not work.  There are two
> solutions, both already described in the answers to your question.

But delete pData should call the destructor, which is indisde the dll.

Tony



Fri, 16 Sep 2005 17:46:53 GMT  
 memory management in dll

Quote:




> > > > >Is this a problem? Since the memory is allocated in dll, it should be
> > > > >freeed in dll. How can I handle this situation?

> > > > You'd need to call a method in your DLL to have it delete the
> > > > allocated data.

> > > Is the call "delete pData" work?

> > > Thanks,

> > > qq

> > Your "delete pData" calls the exe memory manager, but the corresponding
> > "new" called the DLL memory manager.  So if the exe and the DLL have a
> > different memory manager the delete will not work.  There are two
> > solutions, both already described in the answers to your question.

> But delete pData should call the destructor, which is indisde the dll.

> Tony

Yes, that's right. But it's not relevant to the problem.  After calling
the destructor it calls the heap memory allocator runtime library to
free the memory.  If you do not use one of the techniques previously
discussed the memory allocator will assert or crash because it cannot
free memory that it did not allocate.

--
Scott McPhillips [VC++ MVP]



Sat, 17 Sep 2005 02:54:29 GMT  
 
 [ 6 post ] 

 Relevant Pages 

1. dll and memory management

2. Memory management for 16 bits applications and DLL

3. Memory Management between exe's and dll's

4. DLL's and memory management in W95

5. Memory management for 16 bits applications and DLL

6. VC 1.52 DLL memory management problem

7. memory management

8. Memory Management

9. "C Pointers and Dynamic Memory Management"

10. memory management w/ little RAM

11. Memory Management

12. Memory management

 

 
Powered by phpBB® Forum Software