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

x
  • Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 415
  • Last Modified:

Memory Leak

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
rian
Asked:
rian
1 Solution
 
lucidityCommented:
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
 
nietodCommented:
answer coming.
0
 
nietodCommented:
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
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.

 
alexoCommented:
>> 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
 
nietodCommented:
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
 
rianAuthor Commented:
Thanks for all your patience and help
0

Featured Post

Free Tool: SSL Checker

Scans your site and returns information about your SSL implementation and certificate. Helpful for debugging and validating your SSL configuration.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.

Tackle projects and never again get stuck behind a technical roadblock.
Join Now