GetLastError() message

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.
yarlapatiAsked:
Who is Participating?
I wear a lot of hats...

"The solutions and answers provided on Experts Exchange have been extremely helpful to me over the last few years. I wear a lot of hats - Developer, Database Administrator, Help Desk, etc., so I know a lot of things but not a lot about one thing. Experts Exchange gives me answers from people who do know a lot about one thing, in a easy to use platform." -Todd S.

jkrCommented:
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.
0

Experts Exchange Solution brought to you by

Your issues matter to us.

Facing a tech roadblock? Get the help and guidance you need from experienced professionals who care. Ask your question anytime, anywhere, with no hassle.

Start your 7-day free trial
itsmeandnobodyelseCommented:
>>>> 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
   
0
AlexFMCommented:
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.
0
Keep up with what's happening at Experts Exchange!

Sign up to receive Decoded, a new monthly digest with product updates, feature release info, continuing education opportunities, and more.

yarlapatiAuthor 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.
0
jkrCommented:
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.
0
yarlapatiAuthor 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?
0
yarlapatiAuthor 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???
0
itsmeandnobodyelseCommented:
>>>> 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
0
yarlapatiAuthor 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.

0
It's more than this solution.Get answers and train to solve all your tech problems - anytime, anywhere.Try it for free Edge Out The Competitionfor your dream job with proven skills and certifications.Get started today Stand Outas the employee with proven skills.Start learning today for free Move Your Career Forwardwith certification training in the latest technologies.Start your trial today
C++

From novice to tech pro — start learning today.

Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.