๐Ÿš€ go-pugleaf

RetroBBS NetNews Server

Inspired by RockSolid Light RIP Retro Guy

Article View: borland.public.cppbuilder.nativeapi
Article #1081

Re: malloc versus GlobalAlloc

#1081
From: Alan Bellingham
Date: Tue, 20 Nov 2007 09:01
75 lines
4434 bytes
"Eyyub Volkan ๏ฟฝektimur" <evcektimur@prokon.com.tr> wrote:

>When EXE & DLL have different memory managments, if you malloc memory at DLL 
>and free it at EXE, the heaps get mixed and you get an exception. 

No-one said that.

If you're exceptionally lucky, you might get an exception, but usually
you just get a corrupted heap. Which may lead to all sorts of
interesting things, perhaps including two supposedly different bits of
memory actually being the same.

>                                                                    I can 
>understand that. when i read GlobalAlloc/GlobalFree documentation, it says 
>that those functions are using default heap (means GetProcessHeap()).

That's different from normal heap - that's controlled by the OS on a
per-process basis, so to it the fact your code is in multiple
executables is irrelevant.

>                                                                      So, 
>when i create memory with GloballAlloc in DLL and GlobalFree it in EXE, it 
>does not give an exception ? GetProcessHeap()  returns the same value in DLL 
>& EXE. is that mean the heaps are same ? 

It means that Windows itself has one heap (as far as your process is
concerned).

>                                         if the heaps are same, why 
>malloc/free giving an error ? does not malloc/free using GlobalAlloc or 
>HeapAlloc ? thanks for helping...

What you are ignoring is what a heap is.

The major point is that GloballAlloc() is expensive to call, and it also
has a limited number of handles available, so you don't use it for every
little piece of memory. The strategy is for a memory manager to call
GlobalAlloc for big chunks, and to break those chunks down into the
small allocations that your program actually makes.

A heap is a memory structure used to control an area of memory. In a
simple heap (and I simplify, because a fully implemented one is
complicated), the memory manager starts with a piece of static memory.
In this static memory are pointers to memory that the manager has used
GloballAlloc() to go get. When a client asks the manager for a chunk of
memory, it checks through its internal structure seeing if any of the
memory it controls is big enough. If so, it slices off a chunk to return
to the client. It also notes the size left of the free memory.

If it didn't have enough, it GlobalAllocs a new chunk, noting it's now
got it.

When memory is freed, it notes the address, works out where in the
structure it came from, and readjusts the relevant list of free memory.

Now, note the problem - the static memory. It has to have that, just to
be able to start somewhere to find its own memory list. But, a memory
manager in an EXE and one in a DLL will end up using different static
areas. So, when memory is returned that didn't come from it in the first
place, the structures get corrupted.

Yes, a memory manager could be made that did check. But such checking
slows it down considerably - it's easier to write fast code when it is
assumed inputs to it are correct - and would penalise everyone who does
manage memory correctly.

(I've had programs where memory management was taking up 50% of runtime
- trebling that management time would have halved the speed of the
executable!)

Alan Bellingham
-- 
Team Browns
<url:http://www.borland.com/newsgroups/> Borland newsgroup descriptions
<url:http://www.borland.com/newsgroups/netiquette.html>      netiquette
Technical Details
Message-ID: <dh75k3ptds34av2aaj6gkjcov3634j36j0@4ax.com>
Path: arc1-ca.pugleaf.net!g2news1.google.com!news4.google.com!proxad.net!feeder1-2.proxad.net!213.200.89.82.MISMATCH!tiscali!newsfeed1.ip.tiscali.net!newsfeed00.sul.t-online.de!newsfeed01.sul.t-online.de!t-online.de!newsgroups.borland.com!not-for-mail
References: <47429a08$1@newsgroups.borland.com>

Show Full Headers
Complete Article Headers
X-Google-Thread: 10cc67,5bc4a8822b5c2c43
X-Google-Attributes: gid10cc67,public,usenet
X-Google-Language: ENGLISH,ASCII
From: Alan Bellingham <alanb@episys.com>
Newsgroups: borland.public.cppbuilder.nativeapi
Subject: Re: malloc versus GlobalAlloc
Date: Tue, 20 Nov 2007 09:01:40 +0000
Reply-To: alanb@episys.com
Message-ID: <dh75k3ptds34av2aaj6gkjcov3634j36j0@4ax.com>
References: <47429a08$1@newsgroups.borland.com>
X-Newsreader: Forte Agent 4.2/32.1118
MIME-Version: 1.0
Content-Type: text/plain; charset=ISO-8859-1
Content-Transfer-Encoding: 8bit
NNTP-Posting-Host: 62.49.31.105
X-Trace: newsgroups.borland.com 1195549571 62.49.31.105 (20 Nov 2007 01:06:11 -0700)
Lines: 75
Path: arc1-ca.pugleaf.net!g2news1.google.com!news4.google.com!proxad.net!feeder1-2.proxad.net!213.200.89.82.MISMATCH!tiscali!newsfeed1.ip.tiscali.net!newsfeed00.sul.t-online.de!newsfeed01.sul.t-online.de!t-online.de!newsgroups.borland.com!not-for-mail