Solved

Memory Leak

Posted on 1998-05-20
6
401 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
Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

 
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

Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

Question has a verified solution.

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

Suggested Solutions

Title # Comments Views Activity
C++ - Loading Managed Assembly From Memory in Unmanaged Process 25 426
Visual c++ and text files 7 60
mixing C++ & C# in Vis Studio 2013 7 141
Unresolved External Symbols 3 68
Templates For Beginners Or How To Encourage The Compiler To Work For You Introduction This tutorial is targeted at the reader who is, perhaps, familiar with the basics of C++ but would prefer a little slower introduction to the more ad…
C++ Properties One feature missing from standard C++ that you will find in many other Object Oriented Programming languages is something called a Property (http://www.experts-exchange.com/Programming/Languages/CPP/A_3912-Object-Properties-in-C.ht…
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…

911 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

18 Experts available now in Live!

Get 1:1 Help Now