[Okta Webinar] Learn how to a build a cloud-first strategyRegister Now

x
?
Solved

GetLastError() message

Posted on 2006-03-27
9
Medium Priority
?
3,935 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.
0
Comment
Question by:yarlapati
  • 4
  • 2
  • 2
  • +1
9 Comments
 
LVL 86

Accepted Solution

by:
jkr earned 1600 total points
ID: 16306860
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
 
LVL 39

Expert Comment

by:itsmeandnobodyelse
ID: 16307835
>>>> 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
 
LVL 48

Assisted Solution

by:AlexFM
AlexFM earned 400 total points
ID: 16310507
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
Concerto Cloud for Software Providers & ISVs

Can Concerto Cloud Services help you focus on evolving your application offerings, while delivering the best cloud experience to your customers? From DevOps to revenue models and customer support, the answer is yes!

Learn how Concerto can help you.

 

Author Comment

by:yarlapati
ID: 16314239
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
 
LVL 86

Expert Comment

by:jkr
ID: 16314669
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
 

Author Comment

by:yarlapati
ID: 16315639
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
 

Author Comment

by:yarlapati
ID: 16315727
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
 
LVL 39

Expert Comment

by:itsmeandnobodyelse
ID: 16318360
>>>> 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
 

Author Comment

by:yarlapati
ID: 16325836
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

Featured Post

Independent Software Vendors: We Want Your Opinion

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Many modern programming languages support the concept of a property -- a class member that combines characteristics of both a data member and a method.  These are sometimes called "smart fields" because you can add logic that is applied automaticall…
Basic understanding on "OO- Object Orientation" is needed for designing a logical solution to solve a problem. Basic OOAD is a prerequisite for a coder to ensure that they follow the basic design of OO. This would help developers to understand the b…
The goal of the tutorial is to teach the user how to use functions in C++. The video will cover how to define functions, how to call functions and how to create functions prototypes. Microsoft Visual C++ 2010 Express will be used as a text editor an…
The goal of the video will be to teach the user the concept of local variables and scope. An example of a locally defined variable will be given as well as an explanation of what scope is in C++. The local variable and concept of scope will be relat…
Suggested Courses

834 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question