🚀 go-pugleaf

RetroBBS NetNews Server

Inspired by RockSolid Light RIP Retro Guy

Thread View: borland.public.cppbuilder.nativeapi
12 messages
12 total messages Started by Eyyub Volkan �ektimur Tue, 20 Nov 2007 08:25
malloc versus GlobalAlloc
#1080
Author: Eyyub Volkan �ektimur
Date: Tue, 20 Nov 2007 08:25
16 lines
1732 bytes
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. I can 
understand that. when i read GlobalAlloc/GlobalFree documentation, it says 
that those functions are using default heap (means GetProcessHeap()). 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 ? if the heaps are same, why 
malloc/free giving an error ? does not malloc/free using GlobalAlloc or 
HeapAlloc ? thanks for helping...

P.S. : GMEM_FIXED param. used to create memory with GlobalAlloc.
-- 
_______________________
Eyyub Volkan �ektimur
Proya Software Inc.
Head of Core Development 
Re: malloc versus GlobalAlloc
#1081
Author: 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
Re: malloc versus GlobalAlloc
#1083
Author: Eyyub Volkan �ektimur
Date: Tue, 20 Nov 2007 09:41
38 lines
2973 bytes
First of all, thanks for your plain explanation. I'm tring to understand 
what heap is and you did it very simple to understand.

> 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.

yeap, i know that, not all the time exception is raised because of 
corruption. i wanted to say that an error occurs when you do that... (on 
time or in future)

> 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.

so, heap is not a memory block that is given to users, it's a kind of 
management tool i guess. so, can we say that memory manager uses heap 
structure to give memory chunks from bigger memory allocations and also 
manages free operations etc... Can we say that, heap is a structure to hold 
given allocations,frees,etc... to share bigger allocation chunks taken by 
GlobalAlloc ?

i guess i need to check memory managers of EXE & DLLs not heaps. If all of 
them using the same memory manager, then there will be no heap corruption 
problem. Is there any way in borland to check that loaded DLLs and EXE are 
using the same memory manager ? in borland is the memory manager named as 
RTL ? or is RTL mapped to microsoft's CRT ? does borland compiler have a 
seperate memory manager tool (that is implemented in RTL) ? 
Re: malloc versus GlobalAlloc
#1082
Author: Alan Bellingham
Date: Tue, 20 Nov 2007 09:57
42 lines
2918 bytes
"Eyyub Volkan �ektimur" <evcektimur@prokon.com.tr> wrote:

>so, heap is not a memory block that is given to users, it's a kind of 
>management tool i guess. so, can we say that memory manager uses heap 
>structure to give memory chunks from bigger memory allocations and also 
>manages free operations etc... Can we say that, heap is a structure to hold 
>given allocations,frees,etc... to share bigger allocation chunks taken by 
>GlobalAlloc ?

Close enough.

(Windows will have its own heap managers too - at the top end, there's
just raw memory, that GlobalAlloc will take chunks from.)

>i guess i need to check memory managers of EXE & DLLs not heaps. If all of 
>them using the same memory manager, then there will be no heap corruption 
>problem. 

The RTL provides an exported memory manager, that provides the
new/malloc/... functions from a DLL rather than from within a LIB
whereby each linked executable gets its own copy. 

>         Is there any way in borland to check that loaded DLLs and EXE are 
>using the same memory manager ? in borland is the memory manager named as 
>RTL ? or is RTL mapped to microsoft's CRT ? does borland compiler have a 
>seperate memory manager tool (that is implemented in RTL) ? 

As far as I know, you have to manually check the link options, though
it's probably also possible to use the Dependency Walker tool to see if
all the items use the RTL. I'm not sure the name of that DLL, since the
only bit of my code that does this sort of thing happens to use my own
implementation of the same idea (dating back to 1991)(Yeah, my original
version allowed memory shared between processes - that went west when I
went 32-bit.).

(http://http://www.dependencywalker.com/ for the Dependency Walker
tool.)

Alan Bellingham
-- 
Team Browns
ACCU Conference 2008: 2-5 April 2008 - Oxford, UK
Re: malloc versus GlobalAlloc
#1084
Author: Eyyub Volkan �ektimur
Date: Tue, 20 Nov 2007 10:55
1 lines
1093 bytes
thanks very much, that helped me much... 
Re: malloc versus GlobalAlloc
#1085
Author: Eyyub Volkan �ektimur
Date: Tue, 20 Nov 2007 13:06
17 lines
1719 bytes
one more question;

i traversed all modules loaded by EXE (EnumProcessModules) and queried that 
which module including "malloc" 
(GetProcAddress(modules[cx],"malloc")!=NULL). the result is ;

malloc in C:\WINDOWS\system32\msvcrt.dll

i was expecting to find "C:\WINDOWS\system32\CC3260MT.DLL" (i'm using bcb 
6.0)

does not borland have a seperate memory manager that handles malloc ? if 
there is, why malloc is in msvcrt.dll ? as i know CC3260* is the 
VCL-Compliant of CRT, so it's making more sense to include malloc in 
CC3260*.dll. does borland using microsoft's memory management DLL 
(msvcrt.dll) ? (if it's, CC3260* should be a mapper to msvcrt.dll) ... 
thanks again 
Re: malloc versus GlobalAlloc
#1086
Author: Bob Gonder
Date: Tue, 20 Nov 2007 13:18
11 lines
1336 bytes
Alan Bellingham wrote:


>I'm not sure the name of that DLL, since the

Oddly enough, borlndmm.dll

>>  Is there any way in borland to check that loaded DLLs and EXE are 
>>using the same memory manager ?

While finding that name, came across usebormm.h which looks like it might help.
Re: malloc versus GlobalAlloc
#1087
Author: Alan Bellingham
Date: Tue, 20 Nov 2007 14:25
37 lines
2404 bytes
"Eyyub Volkan �ektimur" <evcektimur@prokon.com.tr> wrote:

>one more question;
>
>i traversed all modules loaded by EXE (EnumProcessModules) and queried that 
>which module including "malloc" 
>(GetProcAddress(modules[cx],"malloc")!=NULL). the result is ;
>
>malloc in C:\WINDOWS\system32\msvcrt.dll

Ah, yes.

This would appear to be an exported malloc() - i.e. visible when you
look at the table of symbols exported from an executable.

It's fairly likely (OK, at least 99.9% chance*) that at some point,
you're linking to a Microsoft DLL (e.g making any use of the operating
system ...), and at that point, you're going to find something that uses
MSVCRT.

>does not borland have a seperate memory manager that handles malloc ? if 
>there is, why malloc is in msvcrt.dll ? as i know CC3260* is the 
>VCL-Compliant of CRT, so it's making more sense to include malloc in 
>CC3260*.dll. does borland using microsoft's memory management DLL 
>(msvcrt.dll) ? (if it's, CC3260* should be a mapper to msvcrt.dll) ... 

The malloc() that Borland code uses will not be the one you've found.
The Borland libraries will be using a non-exported version, which won't
show up in your explorations. Well, not unless you use borlndmm.dll (and
I've not looked to see what it actually exports).

Alan Bellingham

* Note that this statistic is 100% likely to be made up
-- 
Team Browns
ACCU Conference 2008: 2-5 April 2008 - Oxford, UK
Re: malloc versus GlobalAlloc
#1088
Author: Eyyub Volkan �ektimur
Date: Tue, 20 Nov 2007 14:56
45 lines
3028 bytes
i was just realized that they are not the same malloc borland used , as you 
said. I'was creating a message to fix myself, and saw yours. thanks alot 
again. (i also tried to create memory with msvcrt.malloc and to free it with 
normal free, it raised an exception , hehe, i'm extremely luck today i gues)

"Alan Bellingham" <alan@lspace.org> wrote in message 
news:j8r5k3tnu3aa63mdjfc3tni7mtqasr4lh1@4ax.com...
> "Eyyub Volkan �ektimur" <evcektimur@prokon.com.tr> wrote:
>
>>one more question;
>>
>>i traversed all modules loaded by EXE (EnumProcessModules) and queried 
>>that
>>which module including "malloc"
>>(GetProcAddress(modules[cx],"malloc")!=NULL). the result is ;
>>
>>malloc in C:\WINDOWS\system32\msvcrt.dll
>
> Ah, yes.
>
> This would appear to be an exported malloc() - i.e. visible when you
> look at the table of symbols exported from an executable.
>
> It's fairly likely (OK, at least 99.9% chance*) that at some point,
> you're linking to a Microsoft DLL (e.g making any use of the operating
> system ...), and at that point, you're going to find something that uses
> MSVCRT.
>
>>does not borland have a seperate memory manager that handles malloc ? if
>>there is, why malloc is in msvcrt.dll ? as i know CC3260* is the
>>VCL-Compliant of CRT, so it's making more sense to include malloc in
>>CC3260*.dll. does borland using microsoft's memory management DLL
>>(msvcrt.dll) ? (if it's, CC3260* should be a mapper to msvcrt.dll) ...
>
> The malloc() that Borland code uses will not be the one you've found.
> The Borland libraries will be using a non-exported version, which won't
> show up in your explorations. Well, not unless you use borlndmm.dll (and
> I've not looked to see what it actually exports).
>
> Alan Bellingham
>
> * Note that this statistic is 100% likely to be made up
> -- 
> Team Browns
> ACCU Conference 2008: 2-5 April 2008 - Oxford, UK 
Re: malloc versus GlobalAlloc
#1089
Author: dhoke
Date: Tue, 20 Nov 2007 18:07
44 lines
2949 bytes
"Eyyub Volkan �ektimur" <evcektimur@prokon.com.tr> wrote in message 
news:4742dbe3@newsgroups.borland.com...
> one more question;
>
> i traversed all modules loaded by EXE (EnumProcessModules) and queried 
> that which module including "malloc" 
> (GetProcAddress(modules[cx],"malloc")!=NULL). the result is ;
>
> malloc in C:\WINDOWS\system32\msvcrt.dll
>
> i was expecting to find "C:\WINDOWS\system32\CC3260MT.DLL" (i'm using bcb 
> 6.0)
>
> does not borland have a seperate memory manager that handles malloc ? if 
> there is, why malloc is in msvcrt.dll ? as i know CC3260* is the 
> VCL-Compliant of CRT, so it's making more sense to include malloc in 
> CC3260*.dll. does borland using microsoft's memory management DLL 
> (msvcrt.dll) ? (if it's, CC3260* should be a mapper to msvcrt.dll) ... 
> thanks again
>

At least for bds2006 (don't know about BCB6), and appears to for the 
CB2007trial and radstudio2007trial versions:

Borland(/CodeGear) does have its own memory management.  (Not sure what 
you've found with your search.)

Borland has a plug-in memory management architecture, whereby you can (well 
some people can) replace the memory manager functionality.

A replacement memory manager (borlndmm.dll), is part of product 
installation, installed in the ...\bin directory of the installation tree.

Borland's memory manager (since at least bds2006) is apparently a copy or 
derivative of the FastMM memory manager 
(http://sourceforge.net/projects/fastmm/).

Borland's libraries are implemented such that a plugin replacement memory 
manager will _not_ be used if an application/dll is linked with static 
libraries (at least with bds2006.)

You might want to redirect this topic to the .rtl newsgroup, wrt/ B/CG's 
runtime library implementation. 
Re: malloc versus GlobalAlloc
#1090
Author: Eyyub Volkan �ektimur
Date: Wed, 21 Nov 2007 07:04
54 lines
3501 bytes
thanks for your answer. i don't think that borlndMM.dll is the one i'm 
searching for, because, when i try to set my memory managment 
(SetMemoryManager(...)) it does not handle C type malloc & free and also it 
does not handle new, new[], delete, delete[] on regular types... i guess 
it's used only in VCL, when i tried to create a VCL object, memory manager 
functions are triggered, but nothing else. thanks anyway,...

"dhoke" <dhoke.nojunk@east-shore.com> wrote in message 
news:4743226d$1@newsgroups.borland.com...
>
> "Eyyub Volkan �ektimur" <evcektimur@prokon.com.tr> wrote in message 
> news:4742dbe3@newsgroups.borland.com...
>> one more question;
>>
>> i traversed all modules loaded by EXE (EnumProcessModules) and queried 
>> that which module including "malloc" 
>> (GetProcAddress(modules[cx],"malloc")!=NULL). the result is ;
>>
>> malloc in C:\WINDOWS\system32\msvcrt.dll
>>
>> i was expecting to find "C:\WINDOWS\system32\CC3260MT.DLL" (i'm using bcb 
>> 6.0)
>>
>> does not borland have a seperate memory manager that handles malloc ? if 
>> there is, why malloc is in msvcrt.dll ? as i know CC3260* is the 
>> VCL-Compliant of CRT, so it's making more sense to include malloc in 
>> CC3260*.dll. does borland using microsoft's memory management DLL 
>> (msvcrt.dll) ? (if it's, CC3260* should be a mapper to msvcrt.dll) ... 
>> thanks again
>>
>
> At least for bds2006 (don't know about BCB6), and appears to for the 
> CB2007trial and radstudio2007trial versions:
>
> Borland(/CodeGear) does have its own memory management.  (Not sure what 
> you've found with your search.)
>
> Borland has a plug-in memory management architecture, whereby you can 
> (well some people can) replace the memory manager functionality.
>
> A replacement memory manager (borlndmm.dll), is part of product 
> installation, installed in the ...\bin directory of the installation tree.
>
> Borland's memory manager (since at least bds2006) is apparently a copy or 
> derivative of the FastMM memory manager 
> (http://sourceforge.net/projects/fastmm/).
>
> Borland's libraries are implemented such that a plugin replacement memory 
> manager will _not_ be used if an application/dll is linked with static 
> libraries (at least with bds2006.)
>
> You might want to redirect this topic to the .rtl newsgroup, wrt/ B/CG's 
> runtime library implementation.
> 
Re: malloc versus GlobalAlloc
#1091
Author: dhoke
Date: Wed, 21 Nov 2007 07:34
68 lines
4207 bytes
It does not directly contain the code for new/delete/malloc/free/whatever, 
but I'm pretty sure that _if_ you're linking with the dynamic runtime 
libraries (_not_ the static libraries), new/delete/malloc/etc will 
eventually call into it.

in bds2006, in the installation tree (with library source), the 
implementations of the various items are located in 
<bdsinstalldir>\source\cpprtl\Source\memory\.

"Eyyub Volkan �ektimur" <evcektimur@prokon.com.tr> wrote in message 
news:4743d86b@newsgroups.borland.com...
> thanks for your answer. i don't think that borlndMM.dll is the one i'm 
> searching for, because, when i try to set my memory managment 
> (SetMemoryManager(...)) it does not handle C type malloc & free and also 
> it does not handle new, new[], delete, delete[] on regular types... i 
> guess it's used only in VCL, when i tried to create a VCL object, memory 
> manager functions are triggered, but nothing else. thanks anyway,...
>
> "dhoke" <dhoke.nojunk@east-shore.com> wrote in message 
> news:4743226d$1@newsgroups.borland.com...
>>
>> "Eyyub Volkan �ektimur" <evcektimur@prokon.com.tr> wrote in message 
>> news:4742dbe3@newsgroups.borland.com...
>>> one more question;
>>>
>>> i traversed all modules loaded by EXE (EnumProcessModules) and queried 
>>> that which module including "malloc" 
>>> (GetProcAddress(modules[cx],"malloc")!=NULL). the result is ;
>>>
>>> malloc in C:\WINDOWS\system32\msvcrt.dll
>>>
>>> i was expecting to find "C:\WINDOWS\system32\CC3260MT.DLL" (i'm using 
>>> bcb 6.0)
>>>
>>> does not borland have a seperate memory manager that handles malloc ? if 
>>> there is, why malloc is in msvcrt.dll ? as i know CC3260* is the 
>>> VCL-Compliant of CRT, so it's making more sense to include malloc in 
>>> CC3260*.dll. does borland using microsoft's memory management DLL 
>>> (msvcrt.dll) ? (if it's, CC3260* should be a mapper to msvcrt.dll) ... 
>>> thanks again
>>>
>>
>> At least for bds2006 (don't know about BCB6), and appears to for the 
>> CB2007trial and radstudio2007trial versions:
>>
>> Borland(/CodeGear) does have its own memory management.  (Not sure what 
>> you've found with your search.)
>>
>> Borland has a plug-in memory management architecture, whereby you can 
>> (well some people can) replace the memory manager functionality.
>>
>> A replacement memory manager (borlndmm.dll), is part of product 
>> installation, installed in the ...\bin directory of the installation 
>> tree.
>>
>> Borland's memory manager (since at least bds2006) is apparently a copy or 
>> derivative of the FastMM memory manager 
>> (http://sourceforge.net/projects/fastmm/).
>>
>> Borland's libraries are implemented such that a plugin replacement memory 
>> manager will _not_ be used if an application/dll is linked with static 
>> libraries (at least with bds2006.)
>>
>> You might want to redirect this topic to the .rtl newsgroup, wrt/ B/CG's 
>> runtime library implementation.
>>
>
> 
Thread Navigation

This is a paginated view of messages in the thread with full content displayed inline.

Messages are displayed in chronological order, with the original post highlighted in green.

Use pagination controls to navigate through all messages in large threads.

Back to All Threads