Solved

Memory Leak

Posted on 1998-05-20
6
409 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
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
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
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!

 
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

Announcing the Most Valuable Experts of 2016

MVEs are more concerned with the satisfaction of those they help than with the considerable points they can earn. They are the types of people you feel privileged to call colleagues. Join us in honoring this amazing group of Experts.

Question has a verified solution.

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

In days of old, returning something by value from a function in C++ was necessarily avoided because it would, invariably, involve one or even two copies of the object being created and potentially costly calls to a copy-constructor and destructor. A…
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…
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 member functions push_back and pop_back of the vector class. The video will teach the difference between the two as well as how to use each one along with its functionality.

691 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