Re: Memory allocation methods
- From: "Arnaud Debaene" <adebaene@xxxxxxxxxxxxxxxx>
- Date: Fri, 18 Nov 2005 18:42:38 +0100
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
.
- Follow-Ups:
- Re: Memory allocation methods
- From: Stefan Kuhr
- Re: Memory allocation methods
- From: Stefan Kuhr
- Re: Memory allocation methods
- References:
- Memory allocation methods
- From: Martina Burger
- Re: Memory allocation methods
- From: Stefan Kuhr
- Re: Memory allocation methods
- From: Arnaud Debaene
- Re: Memory allocation methods
- From: Stefan Kuhr
- Memory allocation methods
- Prev by Date: Re: Memory allocation methods
- Next by Date: Re: 2 questions about a filter registered with SetUnhandledExceptionFilter?
- Previous by thread: Re: Memory allocation methods
- Next by thread: Re: Memory allocation methods
- Index(es):
Relevant Pages
|