 |
|
 |
Consider this case:
class CObj: public CVMemPool { ... };
class CGlobObj { CObj * obj; CGlobObj() { obj = new CObj(); } };
CGlobObj bug;
int main() { ... }
When you run your app, initialization begins with 'CGlobObj bug', thus also calling new() on its 'obj' attribute. But call co CSLock() constructor where you are initializing the critical section comes later actually. So we will probably need to add some static bool to the CVMemPool class, having its default value to false - so it will be initialized before CGlobObj is initialized. Then, a call to CVMemPool::new() appears; that's the second place for change - you will need to put here a check if CSLock constructor was called already. If not, assign m_csLock = CSLock(); and set the flag to true. Finally, in the CSLock constructor, make another check of the flag, not to initialize critical section 2-times.
But overall - I am afraid of usage of this CVMemPool with such kind of static objects. With example above, you have to treat with late initialization of CBitSet as well - in the time, when the bits are already allocated, constructor makes "= NULL" -> memory leak.
modified on Saturday, September 27, 2008 5:35 PM
|
| Sign In·View Thread·PermaLink | 2.00/5 |
|
|
|
 |
|
 |
I have a MFC application with CFormView as the base view class. In this I am creating controls like textbox, button and labels dynamically. Controls are drawn using ->Create function.
When the screen closes I have also destroyed these controls using delete and I have also gone to the level of DestroyWindow of the base class and found that it is returning 1 (successful destroying of windows window).
What I observe is the memory consumed by my application is not coming down when I observe in the Windows task manager whereas the memory goes up to few MBs (2 to 3MBs) while drawing the screen and the same does not come down when I close the screen
Can some one tell me why is the memory not released to my OS from the memory pool of MFC.
Thanks in advance
|
| Sign In·View Thread·PermaLink | 2.00/5 |
|
|
|
 |
 | problem  caishaikedou | 18:35 27 Apr '06 |
|
 |
I found a problem in the case of using your code in creating a ActiveX.You use lots of static vars,so if I do a Activex project,and this ActiveX control is loaded by IE(we know that there maybe one IE process but lots windows load my ActiveX),your code maybe have some abnormality behavior.
|
| Sign In·View Thread·PermaLink | 1.20/5 |
|
|
|
 |
|
 |
 First of all, Thank you for good lesson. I am beginner for programming.(for English too. ㅡㅡ;
I have two questions now.
1. Why do you align with 4 bytes In 'CBitSet::sCalcUsedBytes'? Maybe I think that it is Optimization for Speed. then Do you have the other reasons?
2. I looked at the VMemPool Header. But I didn't understand your naming concept For '~size'. For example, below
static DWORD ms_dwFreeQueueSize; static DWORD ms_dwTotalSize;
static DWORD ms_dwPoolSize; // Total Pool object counter static DWORD ms_dwObjSize; // one object size (bytes) static DWORD ms_dwAllocObjCount; // current allocated counts
size,counter,counts... What is different with them?
Thanks a lot. Have you nice day!
Let's conquer the world with my power.
-- modified at 23:11 Thursday 12th January, 2006
|
| Sign In·View Thread·PermaLink | 1.50/5 |
|
|
|
 |
|
 |
Aligning memory along natural machine word boundaries (for most CPUs, this is still 32 bits or 4 bytes) provides optimal performance. Many (if not all) CPUs generate fault interrupts for non-word-aligned access, and although CPUs in the x86 architecture trap these automatically to provide the reqeuested access, this takes more time than access on word-aligned addresses.
I couldn't answer the question regarding the specifics of the VMemPool header, but I have this one covered.
jbm!
|
| Sign In·View Thread·PermaLink | |
|
|
|
 |
|
 |
When compiling the demo project with VS.NET 2003 the following errors occur! Any ideas? 
VMemPool_demo\VMemPool.h(328) : warning C4346: 'CVMemPool::CSLock': dependent name is not a type prefix with 'typename' to indicate a type VMemPool_demo\VMemPool.h(328) : error C2143: syntax error : missing ';' before 'CVMemPool::ms_csLock' VMemPool_demo\VMemPool.h(328) : error C2501: 'CVMemPool::CSLock': missing storage-class or type specifiers I was using this class for several years in VS6 and it worked great! Thank you Thilo Wawrzik
|
| Sign In·View Thread·PermaLink | 2.00/5 |
|
|
|
 |
|
|
 |
|
 |
I tested your code with VMemPool_demo project. You used timeGetTime function in multimedia library. But, it seemed that the function does not provide exact time. How about using QueryPerformanceCounter function?
|
| Sign In·View Thread·PermaLink | |
|
|
|
 |
|
|
 |
|
 |
Hi Bro, Great code indeed, but I have a question regarding it...
I was looking at the code and I was wandering if the bit set is absolutely necessary. If I understand the code correctly the use of the bit set is to validate the memory blocks and to count the used blocks. Are there any more critical used for the bit set or is it possible to remove it and use the data and the mem pool only?
Ron
|
| Sign In·View Thread·PermaLink | |
|
|
|
 |
|
 |
Hi Ronma, Thanks for regards. in the first, sorry for late response. ( for busy .. ) Yes, bitset is used to manage allocation status for memory layout. you can use it for graphical allocation table on server side helper things. and in source code, bitset is used to check validate memory block. for my job, it was important. Thanks. and happy new year!!
Thank you all. No one know oneself all. My idea from you, book, all others. already we are shared brains.
|
| Sign In·View Thread·PermaLink | |
|
|
|
 |
|
 |
First, thanks so much. This template was what I was looking for. I had a (poorly written) memory pool template that I wanted to revise but looking over CVMemPool, I like yours better.
I did find a small bug: In the new operator, if you run out of memory (id from PopFree returns 0xFFFFFFFF), you return a NULL, which is fine. I think you forgot to unlock ms_csLock before the return, though.
Also, I was going to make an enhancement for a special case I have. I was going to change: template <class objT,DWORD _dwPoolSizeT = 1000> to template <class objT,DWORD _dwPoolSizeT = 1000,DWORD _dwGrowBySizeT = 0> and set the GrowBy parameter to 500 for a class I have. This would allow the pool to grow if necessary, which in my case would happen very infrequently. I was curious if you had already given this some thought, and if so, what ideas you might have towards implementing this.
Thanks, Paul
|
| Sign In·View Thread·PermaLink | 2.00/5 |
|
|
|
 |
|
 |
that's good idea, good job, paul  if you can share our enhanced code to me or others, then you can send to me your vmclass for updating this article, or redistribute your own vmclass with small my info  right now, i have no plan to enhance vmpool. but i haved some ideas  - supports memory allocation table with GUI. ( for my project, server administrator may wanna see that ) etc. i will enhance someday. thanks you!!
Thank you all. No one know oneself all. My idea from you, book, all others. already we are shared brains.
|
| Sign In·View Thread·PermaLink | 2.00/5 |
|
|
|
 |
|
 |
First, really good article. I learned a lot.
I thought about adding a grow feature as well, but then I was scared away by the amount of work.
Maybe I'm misreading the code, but since the entire pool is allocated at once with VirtualAlloc and a NULL address parameter, doesn't that mean growing the pool would require an additional call to VirtualAlloc, and wouldn't that invalidate all current pointers to objects in the pool? Please correct me if I am mistaken. I haven't used VirtualAlloc before but I think I am understanding the docs correctly.
If so, a grow feature would require either the ability to store/index more than one pool block, or a different approach to the allocation like per object (meaning a bunch at one time, not literally one per request obviously). I haven't completely thought through how to make that work yet.
Is there any way with VirtualAlloc to have the existing block stay valid and simply add to it for the grow feature?
Thank you.
|
| Sign In·View Thread·PermaLink | 5.00/5 |
|
|
|
 |
|
 |
I started the "grow" feature but quickly abandoned it for the exact reason you highlighted.
I did do some coding with VirtualAlloc though. I used the CreateFileMapping and MapViewOfFile to get a map handle and a pointer to a very large chunk of memory respectively. The pointer from MapViewOfFile was passed into the VirtualAlloc funtions. This may not be necessary for your app as the primary reason for this was the memory was a shared access area for different apps. However, I used the PAGE_READWRITE | SEC_RESERVE flags on the CreateFileMapping. The system, as I understood it, reserved the large space in memory but it was not available (yet) for me to use and also the system did not consider it used (yet). Subsequent calls to VirtualAlloc used the MEM_COMMIT flag as I needed chunks of this space for application use. I believe you can use the MEM_RESERVE flag with VirtualAlloc instead of the map approach.
Perhaps this method could be used as an alternative to the "grow" feature....
Good luck, Paul
|
| Sign In·View Thread·PermaLink | 1.50/5 |
|
|
|
 |
|
 |
I like the idea of the class, specially the transparency to the user of the CObj class. However, I have some issues with the design of this class (or the way the article explains how to use the class):
1. The is-a relation for CObj and VMemPool fails, i.e. CObj is not a memory pool. For example, CEmployee is not a VMemPool.
2. The need for inheritance will most probably cause Multiple Inheritance issues, since the CEmployee will have to inherit from CPerson and VMemPool, where CPerson might also have inherited from VMemPool. Assuming that CPerson is not an abstract class.
3. To make use of this memory pool, you will need to change the code of the class being allocated. In some cases this is not desirable. For example, if your class is a container which is allocating 100000 instances of a class that is implemented in a 3rd party library. The design should be flexible enough to allow using this techique for the class under development or for a class that is used by the class under development.
My suggestion is to be able to use the class in this way:
class CEmployee { private: static VMemPool m_MemPool; }
class CMyList { private: VMemPool m_3rdPartyMemPoolA; VMemPool m_3rdPartyMemPoolB; }
These are just some thoughts that I wanted to share after reading the article and having a quick look at the code. I have to admit that I didn't get a chance yet to actually use the code.
http://webnotes.sf.net[^]
|
| Sign In·View Thread·PermaLink | |
|
|
|
 |
|
 |
Look at the two posts I made about the Lea allocator and some research done at Texas U of something or another.
Basically, excluding very very very special instances, there is no point to using custom allocators. I was a bit shocked by the results of their tests too. Even with the bugs in this custom allocator, the Lea is faster and it is a malloc replacement.
Tim Smith
I'm going to patent thought. I have yet to see any prior art.
|
| Sign In·View Thread·PermaLink | |
|
|
|
 |
|
 |
Hi Victor Boctor. I can understand what you wanna say to me  and it's important thingy, and thanks !
In my side, I thought my library's client developers want to use very easily. and they might doesn't know class design pattern and more. and I wrote in my article as 'I assumed the client would want a similar usage as new/delete'. so I deal with easiness. that's all.
Thank you all. No one know oneself all. My idea from you, book, all others. already we are shared brains.
|
| Sign In·View Thread·PermaLink | |
|
|
|
 |
|
 |
I just tried this software. If I modify it so that after a VMemPoll object is allocated, the first byte of data is set, then the program crashes.
for( int i = 0 ; i < n; i++) { p[i] = new CVMObj; p[i] ->data [0] = 0; }
The reason this allocator seems to be fast is that no pages of memory are actually allocated.
I tried the Lea allocator and it was actually faster than this allocator and it actually allocated the memory.
Tim Smith
I'm going to patent thought. I have yet to see any prior art.
|
| Sign In·View Thread·PermaLink | |
|
|
|
 |
|
|
 |
|
 |
Hi Tim Smith. Thanks for your interesting. but, I don't understand your practices. I did test like you.
class CObj : public CVMemPool { public: int data[4]; };
void CTestVMDlg::OnButton1() { // TODO: Add your control notification handler code here CObj* obj = new CObj; obj->data[0] = 1; obj->data[1] = 2; }
but, I couldn't see any crash.. ( surely, I thought this simple test will be ok ) and, actually upper code occur memory allocation in Virtual Memory page, not in Win32 process default heap. I'm puzzled when you said, it doesn't make any memory.. and I think that sounds fault.
anyway, thank your test and regards! have a good day!!
Thank you all. No one know oneself all. My idea from you, book, all others. already we are shared brains.
|
| Sign In·View Thread·PermaLink | |
|
|
|
 |
|
 |
Hi bro,
I am a newbie in visual c and my "hello world" program in vc is in 2002 !! I desperately needed some help from big bro in vc regarding memory allocation.
I have created a algorithm using extensive recusive functions. I needed 3 dynamic array inside my functions and so i happily used the new operator to help me. ( the array size is ard 100 integers) To my dismay , the array address was allocated so close together that changing some elements in one array will affect my the other array !! I am at a loss of wat to do to avoid this (and in the first place why this happen !!)
being a mem expert, can you explain to me why such thing happen and what alternative do i have ??
Thank you.
A desperate newbie 
|
| Sign In·View Thread·PermaLink | |
|
|
|
 |
|
 |
hi, bk. I don't understand what you say. (sorry  you'd be better show me your problem code. that is easy to me to understand  Thanks .
Thank you all. No one know oneself all. My idea from you, book, all others. already we are shared brains.
|
| Sign In·View Thread·PermaLink | |
|
|
|
 |
|
 |
We use a product (not free ) called SmartHeap[^]. When we linked it in with our app, we got a 10x speed increase  Just goes to show that the Microsoft heap manager isn't that great with large numbers of heap operations.
|
| Sign In·View Thread·PermaLink | 1.00/5 |
|
|
|
 |
|
|
 |