Solved

Memory Leak

Posted on 1998-05-20
6
405 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
ID: 1164412
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
ID: 1164413
answer coming.
0
 
LVL 22

Expert Comment

by:nietod
ID: 1164414
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
Courses: Start Training Online With Pros, Today

Brush up on the basics or master the advanced techniques required to earn essential industry certifications, with Courses. Enroll in a course and start learning today. Training topics range from Android App Dev to the Xen Virtualization Platform.

 
LVL 11

Expert Comment

by:alexo
ID: 1164415
>> 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
ID: 1164416
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
ID: 1164417
Thanks for all your patience and help
0

Featured Post

Live: Real-Time Solutions, Start Here

Receive instant 1:1 support from technology experts, using our real-time conversation and whiteboard interface. Your first 5 minutes are always free.

Question has a verified solution.

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

Suggested Solutions

Title # Comments Views Activity
C++ Language error 28 215
Header of docx file 17 102
C++ mouse_event mouse look 7 83
Precision Problem in C++ 7 32
When writing generic code, using template meta-programming techniques, it is sometimes useful to know if a type is convertible to another type. A good example of when this might be is if you are writing diagnostic instrumentation for code to generat…
This article will show you some of the more useful Standard Template Library (STL) algorithms through the use of working examples.  You will learn about how these algorithms fit into the STL architecture, how they work with STL containers, and why t…
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.

776 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