Re: Memory allocation methods



Stefan Kuhr wrote:
>>
>> Yes you can! Just make sure that all modules are compiled against
>> the same DLL version of the CRT.
>
> I consider this very dangerous therefore I would not recommend this to
> anyone, although you are right: It is technically possible. However,
> not all things that are possible are good practice :-) at the same
> time. Imagine you have parts of your product compiled with VC6 and
> others with VC8, or imagine you deliver parts of your product to
> third parties which you will automatically tie to your compiler and
> runtime version. If you don't use API based allocators and move
> memory ownership from one module to another, you are lost. Or imagine
> someone decides in a hurry that one module should use a different
> form of runtime (e.g. the static runtime instead of the dynamic
> runtime). Such things happen, it occurred more than once in my career as a
> software developer in companies that I
> worked for.

It all depends
- Either you want to be able to use the full C++ power (STL, exceptions,
etc...) across your modules boundaries, and in that case you make sure that
all modules are compiled against the same CRT' DLL with the same compiler. I
do this on a daily basis without a problem.
- Either you can't control the environment of your DLL that much, and in
that case you restrict yourself to a simple C-based API for the DLL.

The third alternative is to use a well defined ABI such as COM or .NET , but
this implies other choices.

>>
>>> - the runtime allocators do not set a thread's last error in case
>>> they fail (at least they don't do so officially), so if your code is
>>> heavily based on propagating last errors back to the caller, you at
>>> least have to set a last error with
>>> SetLastError(ERROR_NOT_ENOUGH_MEMORY) if they fail (hoping it was
>>> really a low memory condition, not a corrupted heap).
>>
>> agreed again, although I wouldn't rely on GetLastError/errno for any
>> serious, process wide, error checking anyway....
>
> My mileage varies. I frequently write API-like functions and I heavily
> rely on the last error scheme on which most APIs of MS rely in one or
> the other form as well (and don't we all then rely on this scheme?).
> At our company we even do this to the extent that we do a
> SetLastError with custom error codes (with bit 29 set) and provide
> message table DLLs that allow the custom last errors to be resolved
> into a human readable form using the FormatMessage API. Works quite
> nicely.

You can't reliably rely on your caller to check the return value / errno /
GetLastError. And if you take the pain to religiously check all and every
error-condition, code soon become quite unreadeable.

I prefer to use either exceptions (but writing exception-safe code is quite
difficult and ony a few people know about exception-safety altogether, so it
has it's own drawbacks), or another approach I found here :
http://www.cuj.com/documents/s=8188/cuj0512parker/0512parker.html

Arnaud
MVP - VC


.



Relevant Pages

  • Re: Exposing bool to non-MS, non-C++ callers
    ... the functions in this DLL accept a structure as an argument. ... The compiler doesn't help catch places where I neglect ... the C++ bool type is defined as one byte. ... depends on whether the Exe caller will push a value larger than 1 byte. ...
    (microsoft.public.dotnet.languages.vc)
  • COBOL Compiler options
    ... calls to CICS. ... we do not document this DLL problem nor have compiler options ... use of DLL's with the integrated translator (for cases where it is valid ...
    (bit.listserv.ibm-main)
  • Re: Export class from dll
    ... Remember that C++ classes describe not only the general characteristics of ... Only when the DLL ... about the compiler not knowing the details of a CBox, ... How does the compiler know what CBox is? ...
    (microsoft.public.windowsce.embedded.vc)
  • Re: ASP.NET application load time after some change
    ... This utility combines multiple .NET dll's into one dll file. ... The speed can also be affected by how much stuff is in viewstate. ... controls such as Labels or Controls who have their value set in the Load ... because the compiler only needs to compile it once. ...
    (microsoft.public.dotnet.framework.aspnet)
  • Re: unexpected exception handler
    ... Do you mean COM/ATL API does not and should not throw any exception? ... COM defines its own exceptions, which are not C++ exceptions and don't use ... I keep my solution into separate DLLs and a single EXE which utilizes ... DLL and EXE are using different compiler, it is not safe to let the ...
    (microsoft.public.vc.language)

Quantcast