By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
429,471 Members | 711 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 429,471 IT Pros & Developers. It's quick & easy.

Issues w/CRT on program exit?

P: 15
Doing some work on the ol' memory manager this evening and recently I've been having read access violations occur after the actual program exit. The problematic line appears as follows, it appears to have to do with the value passed from my main function:
Expand|Select|Wrap|Line Numbers
  1. mainret = _tmain(__argc, _targv, _tenviron);
  2.  
I'm no CRT expert, would anyone be able to give me some guidance as to what's going wrong here? I'm also having some difficulty figuring out a problem like this one would even occur. I can provide parts of my pool code upon request.

BTW I'm using MSVC2005.
Jun 18 '07 #1
Share this Question
Share on Google+
2 Replies


weaknessforcats
Expert Mod 5K+
P: 9,197
mainret = _tmain(__argc, _targv, _tenviron);
If mainret is a return from _tmain(), then you are recursively calling your program. This probably continues until you blow the stack memory withall those _tmain() stack frames.

I need to see your _tmain() to see how you break the infinite recursion.
Jun 18 '07 #2

P: 15
Expand|Select|Wrap|Line Numbers
  1. int main()
  2. {
  3.     CMemoryPool testingPool(65536, 1024);
  4.     return 0;
  5. }
  6.  
Lovely, I know. Basic synopsis of what this is/means:
-I've obviously instantiated an instance of my memory pool class, the constructor there accepts two arguments: size of a block in the pool (64kb here) and the total number of blocks in the pool. (1024, for a grand total size of 64MB)

Constructor contents:
(It kind of butchered some of my formatting, sorry)
Expand|Select|Wrap|Line Numbers
  1. CMemoryPool::CMemoryPool(const size_t &_blockSize, const size_t &_defaultNumBlocksPerPage) : blockSize(_blockSize),                                                         defaultNumBlocksPerPage(_defaultNumBlocksPerPage), 
  2.                                         defaultPageTotalCapacity(blockSize*defaultNumBlocksPerPage),
  3.                                                         pageFirst(NULL), pageLast(NULL)
  4. {
  5.     cursorPtr = AllocateNewPage();
  6. }
  7.  
Essentially what happens there is that (obviously, I hope) cursorPtr, a general-purpose utility pointer used in ordering, searching/return and maintenance tasks gets set to the last pointer in the list of pages as a sort of safeguard and overhead reduction step. A new CMemoryPage class is created and then a new hunk of memory large enough to "contain" the number of blocks that you specified of the given size. (more on the quotation marks in a second)
Then the overall formatting and sectioning off of the physical page in memory begins. New 'block' objects are formed via regular new() commands, their individual constant pointers assigned to respective sections of the external block and finally everything is all set.

Page constructor code:
Expand|Select|Wrap|Line Numbers
  1. CMemoryPool::CMemoryPage::CMemoryPage(const size_t &_blockSize, const size_t &numBlocksInPage) : blockSize(_blockSize), 
  2.                                                                                                         numTotalBlocks(numBlocksInPage),
  3.                                                                                                         numFreeBlocks(numBlocksInPage)
  4. {
  5.     byte* pageLocation = (byte*)::operator new(blockSize*numBlocksInPage);    // allocate the correct amount of space-- size of a block times number of blocks
  6.     blockFirst = new CMemoryBlock(pageLocation);    // take care of the first block here so we don't have to spend cycles on ifs throughout the for loop
  7.     cursorBlock = blockFirst;
  8.     pageLocation+=blockSize;
  9.     for(size_t i=0; i<numBlocksInPage; pageLocation+=blockSize, i++)
  10.     {
  11.         cursorBlock->blockNext = new CMemoryBlock((void*)pageLocation);
  12.         cursorBlock = cursorBlock->blockNext;
  13.     }
  14.     page = (void*)pageLocation;    // and tell the page object where it lives
  15. }
  16.  
I actually think the problem is with the loop at the end. I believe the second block int the page might not ever be initialized XD
Will update that after further examination.

EDIT: well I did find some problems, but actually the second thing did appear to execute correctly. It would seem, however, that pageLocation has flat-out ignored my instruction to increment itself by blockSize, despite the fact that it is *NOT* a constant. Rather baffling.

EDIT 2: Got it working using a fancy setup of address-of operators on pageLocation[0] and all that fun stuff. Highly annoying to have to spend time figuring out how to do that >:\ Still getting the errors though, have moves on to troubleshooting other parts of the pool.
Jun 19 '07 #3

Post your reply

Sign in to post your reply or Sign up for a free account.