We help IT Professionals succeed at work.

We've partnered with Certified Experts, Carl Webster and Richard Faulkner, to bring you two Citrix podcasts. Learn about 2020 trends and get answers to your biggest Citrix questions!Listen Now

x

GetLastError() message

yarlapati
yarlapati asked
on
Medium Priority
4,074 Views
Last Modified: 2008-01-09
Hi All,

I am trying to find the memory leak in my C++ application and I am using the GetLastError() method which is returning the following.

"The operation completed successfully." as the GetLastError() error message.

Not really sure what it means, but this is coming from my catch block.

Also, can someone suggest how to find the memory leaks and any suggested tools.

This program works fine in the dev environment and fails on the production.

Any ideas would be appreciated.

K.
Comment
Watch Question

CERTIFIED EXPERT
Top Expert 2012
Commented:
I'd suggest to try the technique presented in http:Q_21009673.html - basically, you'd add the following to a central header file:

#ifdef _DEBUG
#ifndef _DBG_NEW

#include <crtdbg.h>

inline void* __operator_new(size_t __n) {
     return ::operator new(__n,_NORMAL_BLOCK,__FILE__,__LINE__);
}
inline void* _cdecl operator new(size_t __n,const char* __fname,int __line) {
     return ::operator new(__n,_NORMAL_BLOCK,__fname,__line);
}
inline void _cdecl operator delete(void* __p,const char*,int) {
     ::operator delete(__p);
}

#define _DBG_NEW new(__FILE__,__LINE__)
#define new _DBG_NEW


#endif // _DBG_NEW
#else

#define __operator_new(__n) operator new(__n)

#endif

Then, add

               int tmpFlag;

               // Get the current state of the flag
               // and store it in a temporary variable
               tmpFlag = _CrtSetDbgFlag( _CRTDBG_REPORT_FLAG );

               // Turn On (OR) - Keep freed memory blocks in the
               // heap’s linked list and mark them as freed
               tmpFlag |= _CRTDBG_LEAK_CHECK_DF;

               // Set the new state for the flag
               _CrtSetDbgFlag( tmpFlag );


to the start of your 'main()'/'WinMain()' and

               _CrtDumpMemoryLeaks ();


to the end and you'll get the line number where the allocation occured. Be sure to locate

#ifdef _DEBUG
#define new DEBUG_NEW
#undef THIS_FILE
static char THIS_FILE[] = __FILE__;
#endif

in every source file at the top - if it is there, comment this out temporarily.

The resulting dump will lead you to every exact line in the source code where a leak was caused by an allocation that was not properly deleted.

Not the solution you were looking for? Getting a personalized solution is easy.

Ask the Experts
>>>> I am using the GetLastError() method

GetLastError returns the last error occured in the Windows API. Error code 0 "The operation completed successfully." means there was no error. If GetLastError would return an error code different from 0 it only would mean that one of the last (internal) calls of the Windows API failed. But the error was handled cause there was no exception.

In any case GetLastError cannot be used to detect memory leaks. Use the method jkr recommended above or check if for any 'new' there is a corresponding 'delete'.

Regards, Alex
   
Commented:
If you see memory leak reported by VC++ which contains allocation number, use _CrtSetBreakAlloc function to find this allocation. For example, leak message has number {300}. Add _CrtSetBreakAlloc(300) to the beginning of the program and run it under debugger. Program breaks in allocation which is not released later.

Author

Commented:
Thank you for the replies.

Alex,

"check if for any 'new' there is a corresponding 'delete'. " :

I made sure that all the Objects created were deleted after every record processing.

JkR,

I used the technique you menitioned above and I got nothing in the Prod environment. But in the Dev env.. I got the following lines..

{214438} normal block at 0x00CA1F58, 33 bytes long.
 Data: < 1965           > 00 31 39 36 35 00 CD CD CD CD CD CD CD CD CD CD
{214437} normal block at 0x00CA1EC0, 84 bytes long.
 Data: < Qh 9650708     > 8C 51 68 00 39 36 35 30 37 30 38 00 00 00 00 00
{214422} normal block at 0x00C9D4C8, 33 bytes long.
 Data: < 24             > 00 32 34 00 CD CD CD CD CD CD CD CD CD CD CD CD
{214420} normal block at 0x00CA1D20, 33 bytes long.
 Data: < 12             > 00 31 32 00 CD CD CD CD CD CD CD CD CD CD CD CD
{214415} normal block at 0x00CA7AE8, 33 bytes long.
 Data: < 1928           > 00 31 39 32 38 00 CD CD CD CD CD CD CD CD CD CD

And it doesn't seem to be of much help to me. Not sure if I am missing anything.

Can you please suggest what more needs to be done.

Regards,
K.
jkr
CERTIFIED EXPERT
Top Expert 2012

Commented:
Yes, that's only displayed whe you run the app under a debugger. In a debug build, you should get the line number information also.

Author

Commented:
Alright.

When I run the app on my machine it runs fine with out any exception but when I run in different env( Prod), I get the memory problem and there is no way that I can run the debbuger on the other environment.

Any ideas?

Author

Commented:
Also, I just saw that I got the following lines when  ran in the debug mode on my machine..

Loaded 'ntdll.dll', no matching symbolic information found.
Loaded 'C:\WINDOWS\system32\kernel32.dll', no matching symbolic information found.
Loaded 'C:\WINDOWS\system32\EntAPI.dll', no matching symbolic information found.
Loaded 'C:\WINDOWS\system32\advapi32.dll', no matching symbolic information found.
Loaded 'C:\WINDOWS\system32\rpcrt4.dll', no matching symbolic information found.
Loaded 'C:\WINDOWS\system32\msvcrt.dll', no matching symbolic information found.
Loaded 'C:\WINDOWS\system32\psapi.dll', no matching symbolic information found.
Loaded 'C:\WINDOWS\system32\netapi32.dll', no matching symbolic information found.
Detected memory leaks!
Dumping objects ->
{44208931} normal block at 0x0498E3D8, 160 bytes long.
 Data: <<Tabdef,"-$,,.00> 3C 54 61 62 64 65 66 2C 22 2D 24 2C 2C 2E 30 30
{44208649} normal block at 0x12236E60, 160 bytes long.
 Data: <<Tabdef,"-$,,.00> 3C 54 61 62 64 65 66 2C 22 2D 24 2C 2C 2E 30 30
{44204859} normal block at 0x0498D630, 33 bytes long.
 Data: < 2000           > 00 32 30 30 30 00 CD CD CD CD CD CD CD CD CD CD
{44204858} normal block at 0x0498D700, 84 bytes long.
 Data: < Qh 0000000     > 8C 51 68 00 30 30 30 30 30 30 30 00 00 00 00 00
{44204857} normal block at 0x0498D5C8, 33 bytes long.
 Data: < 8              > 00 38 00 CD CD CD CD CD CD CD CD CD CD CD CD CD
{44204855} normal block at 0x0498D698, 33 bytes long.
 Data: < 3              > 00 33 00 CD CD CD CD CD CD CD CD CD CD CD CD CD
{44204850} normal block at 0x04995840, 33 bytes long.
 Data: < 2006           > 00 32 30 30 36 00 CD CD CD CD CD CD CD CD CD CD
{44204849} normal block at 0x0498D530, 84 bytes long.
 Data: < Qh 0060308     > 8C 51 68 00 30 30 36 30 33 30 38 00 00 00 00 00
{44204843} normal block at 0x0498CEA0, 33 bytes long.
 Data: < 2000           > 00 32 30 30 30 00 CD CD CD CD CD CD CD CD CD CD
{44204842} normal block at 0x0498D498, 84 bytes long.
 Data: < Qh 000000000000> 8C 51 68 00 30 30 30 30 30 30 30 30 30 30 30 30
{44204836} normal block at 0x0498D430, 33 bytes long.
 Data: < 2000           > 00 32 30 30 30 00 CD CD CD CD CD CD CD CD CD CD
{44204835} normal block at 0x0498CF08, 84 bytes long.
 Data: < Qh 0000000     > 8C 51 68 00 30 30 30 30 30 30 30 00 00 00 00 00
{44204817} normal block at 0x04994148, 33 bytes long.
 Data: < 2000           > 00 32 30 30 30 00 CD CD CD CD CD CD CD CD CD CD
{44204816} normal block at 0x0498CE08, 84 bytes long.

But I dont see any line numbers in them???
>>>> But I dont see any line numbers in them???

The leaks you posted were from modules where no C++ source code is available, e. g. from functions of the WINAPI that are written in C (or Assembler) or where the allocation is made by malloc rather than by new.

Look at the bottom of that list. Normally, there are a few lines where line numbers and source file names can be double-clicked. All other leaks were subsequent to these, e. g. because of class members.

Did you use MFC?

Regards, Alex

Author

Commented:
All,

Thank you very for your posts.

I found the memory leak using the above techniques and was able to correct it.

Also just wanted to paste the article which was also helpful in finding the memory leak.

http://www.codeproject.com/tools/visualleakdetector.asp

Regards,
K.

Access more of Experts Exchange with a free account
Thanks for using Experts Exchange.

Create a free account to continue.

Limited access with a free account allows you to:

  • View three pieces of content (articles, solutions, posts, and videos)
  • Ask the experts questions (counted toward content limit)
  • Customize your dashboard and profile

*This site is protected by reCAPTCHA and the Google Privacy Policy and Terms of Service apply.

OR

Please enter a first name

Please enter a last name

8+ characters (letters, numbers, and a symbol)

By clicking, you agree to the Terms of Use and Privacy Policy.