Solved

Memory Leak

Posted on 1998-05-20
6
398 Views
Last Modified: 2008-02-01
Oh Great Knowlegable ones,

 I need some help with my CPP program running under WIndows NT. I seem to
 be have a memory snafu with my code. I get different results in the
 release mode as well as debug mode.

 Here is a sample of my code

 {UPDVARS.H}
#ifndef _UPDVARS_H
#define _UPDVARS_H

 typedef struct tagUpdate
 {
   char SYS_PATH[MAX_PATH];
   char SKY_PATH[MAX_PATH];
   .... so on and so forth
 }
#endif

 {READSYSF.CPP}
#include "readsysf.h"

 function

 void InitializeBaseline(tagUpdate* BaseGlobal)
 {}
 void GetSysParameters(tagUpdate* pBase);
 {}

 void GetUpdateSetting(tagUpdate* SPSGlobal)
 {
   which calls two functions
       InitializeBaseline(SPSGlobal);
       GetSysParameters(SPSGlobal);
 }

 I also have three function which are called by another cpp file

 void Get_Day_Code(char* dcode);
 {
   tagUpdate* pUpdate;
   GetUpdateSetting(pUpdate);
   strcpy(spath, pUpdate->SYS_PATH)
  { do some string manipulation, set a char value and exit }
 }

 void Get_YDY_Code(char* dcode);
 {
   tagUpdate* pUpdate;
   GetUpdateSetting(pUpdate);
   strcpy(spath, pUpdate->SYS_PATH)
  { do some string manipulation, set a char value and exit }
 }

 void Get_ODY_Code(char* dcode);
 {
   tagUpdate* pUpdate;  
   GetUpdateSetting(pUpdate);
   strcpy(spath, pUpdate->SYS_PATH)
  { do some string manipulation, set a char value and exit }
 }

 {READSYSF.H}
   #include "updvars.h"
  + all the prototypes of the functions in the cpp file

 {CHEKTIME.CPP}

 #include "readsysf.h"
 I call the Get_DAY_Code(), Get_YDY_Code() and Get_ODY_Code();

 I have also tried the following setup in the last three functions in
 readsysf.cpp

 CASE II

 tagUpdate* pUpdate = new (tagUpdate)
 delete (pUpdate);

 CASE III

 tagUpdate * pUpdate;
 pUpdate = (tagUpdate*) malloc(sizeof(tagUpdate);
 free(pUpdate);

 The original case works fine in release mode. But in debug mode, i get  a debug assertion failure in dbgheap.c in expression  CrtlsValidHeapPointer(pUserData) in all three cases

 In case II and Case III, the release mode gives me a "memory could not  be written " error.

 I know i am doing something wrong. Can someone please enlighten me.

Also, When I compile CHEKTIME i get a LINK2005 error. Hence  I have to turn force output on in the customize, link property.

Is there a good article on the net which explains the memory setup with setup.

Thanks
 Thanks.
0
Comment
Question by:rian
6 Comments
 
LVL 2

Expert Comment

by:lucidity
Comment Utility
try using windbg.exe from the microsoft site. it will usually tell you where the leak is comming from, make sure to use a debug comiled version of your program.

And watch out for string or pointer overflows, they make errors pop up in places there are no syntaxtual or memory problems (if not for the overflow).
0
 
LVL 22

Accepted Solution

by:
nietod earned 100 total points
Comment Utility
answer coming.
0
 
LVL 22

Expert Comment

by:nietod
Comment Utility
I was about to point out places where you were using pointers that were not initialized, but I'm beginning to suspect that this is a skeleton of your code, not the actual code.  If it is not a skeleton, let me know  the problem is obvious (well to me, not to you).  If it is not a skeleton, why don't you post the entire code or e-mail it to me at nietod@theshop.net.

One thing though.  your new and delete syntax is kinda weird

tagUpdate* pUpdate = new (tagUpdate)
delete (pUpdate);

seem like overloaded new and delete functions (placement new and placement delete?)  You should use

tagUpdate* pUpdate = new tagUpdate
delete pUpdate;

instead.


0
IT, Stop Being Called Into Every Meeting

Highfive is so simple that setting up every meeting room takes just minutes and every employee will be able to start or join a call from any room with ease. Never be called into a meeting just to get it started again. This is how video conferencing should work!

 
LVL 11

Expert Comment

by:alexo
Comment Utility
>> tagUpdate* pUpdate = new (tagUpdate)
>> delete (pUpdate);
>>
>> seem like overloaded new and delete functions (placement new and placement delete?)

Just some superflous but harmless parentheses.
Cf. return x; vs. return (x);

0
 
LVL 22

Expert Comment

by:nietod
Comment Utility
I realized they were harmless, but I had to think about it for a while.  I thought it would be a good idea to get rid of them for everyone's sake.  
0
 

Author Comment

by:rian
Comment Utility
Thanks for all your patience and help
0

Featured Post

How to improve team productivity

Quip adds documents, spreadsheets, and tasklists to your Slack experience
- Elevate ideas to Quip docs
- Share Quip docs in Slack
- Get notified of changes to your docs
- Available on iOS/Android/Desktop/Web
- Online/Offline

Join & Write a Comment

IntroductionThis article is the second in a three part article series on the Visual Studio 2008 Debugger.  It provides tips in setting and using breakpoints. If not familiar with this debugger, you can find a basic introduction in the EE article loc…
Go is an acronym of golang, is a programming language developed Google in 2007. Go is a new language that is mostly in the C family, with significant input from Pascal/Modula/Oberon family. Hence Go arisen as low-level language with fast compilation…
The goal of the video will be to teach the user the difference and consequence of passing data by value vs passing data by reference in C++. An example of passing data by value as well as an example of passing data by reference will be be given. Bot…
The viewer will be introduced to the technique of using vectors in C++. The video will cover how to define a vector, store values in the vector and retrieve data from the values stored in the vector.

744 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

Need Help in Real-Time?

Connect with top rated Experts

15 Experts available now in Live!

Get 1:1 Help Now