473,830 Members | 2,036 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

Will there be memory leak ?????

Hi ,
I have created a program using CMapStrintToPtr where I would be
mapping
structure ptr to map string. This is just a sample program which I
thought
to avoid linear search in link list since my actual data is so huge.

I created a Windows console program. Since I am assigning a memory
block to
the structure node and I am not freeing it anyway in the program. Will
there be memory leak. Is it necessary to free the memory in my code.
If I want to free
how should I free it. I am thinking of get cmP.GetCount then get 1st
strcuture ptr by cmP.GetStartPos ition then go through each by
cmP.GetNextAsso c and free
each structure ptr we get. Is this correct. Please tell more.

My code is

#include <iostream.h>
#include <afxcoll.h>
#include <conio.h>

CMapStringToPtr cmP;

struct vj
{
int type;
char name_type[100];
};

void mapFunction(cha r *mapStr, int typ, char *typeName)
{

struct vj *vijay;
vijay = (struct vj *) malloc (sizeof (struct vj));
strcpy(vijay->name_type, typeName);
vijay->type = typ;

cmP.SetAt ( mapStr, vijay);
}

void main()
{

mapFunction("li ", 3, "line");
mapFunction("ls ", 4, "linestring ");
mapFunction("sh ", 5, "shape");

struct vj *temp;
cmP.Lookup ("sh", (void*&)temp);
cout<< temp->type <<" " << temp->name_type << endl;
getch();

}
Thanks vijay.
Jul 22 '05 #1
1 1444
"Vijay" <sa****@yahoo.c om> wrote in message
news:7c******** *************** ***@posting.goo gle.com...
I have created a program using CMapStrintToPtr where I would be
mapping
structure ptr to map string. This is just a sample program which I
thought
to avoid linear search in link list since my actual data is so huge. CMapStringToPtr is not a standard C++ class. Most people here do
not know (or want to know) what it does.
I created a Windows console program. Since I am assigning a memory
block to
the structure node and I am not freeing it anyway in the program. Will
there be memory leak. Is it necessary to free the memory in my code. Yes, Yes.
If I want to free how should I free it.

Really, you should use better designed classes, such as those
available in the standard C++ library, that will save you from
doing manual memory management.
See below how leaner and cleaner your program gets -- not to mention
safer and more portable:

#include <iostream> //NB: iostream.h is non-standard.
#include <map>
#include <string>
//using namespace std; // put this to not repeat std:: below

struct vj // could eventually be replaced by a std::pair ...
{
int type;
std::string name_type;
vj(int t, std::string n) : type(t), name_type(n) {} //added
};

//nb: no mapFunction needed, the constructor replaces it

int main() // void main() is NOT allowed in standard C++
{
std::map< std::string, vj > cmP;

cmP["li"] = vj(3,"line");
cmP["ls"] = vj(4,"linestrin g");
cmP["sh"] = vj(5,"shape");

//NB: cmP.Lookup ("sh", (void*&)temp) --> was undefined behavior
vj& temp = cmP["sh"];
std::cout << temp.type <<" "<<temp.name_ty pe<<std::endl;

return 0;
}
I would recommend getting a real C++ book (e.g. "Accelerate d C++"),
and (or else) posting to a windows-specific forum when using
platform-specific libraries.

Ivan
--
http://ivan.vecerina.com/contact/?subject=NG_POST <- email contact form
Jul 22 '05 #2

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

10
8721
by: Debian User | last post by:
Hi, I'm trying to discover a memory leak on a program of mine. I've taken several approaches, but the leak still resists to appear. First of all, I've tried to use the garbage collector to look for uncollectable objects. I've used the next: # at the beginning of code gc.enable()
8
3419
by: ranjeet.gupta | last post by:
Dear All Is the Root Cause of the Memory corruption is the Memory leak, ?? suppose If in the code there is Memory leak, Do this may lead to the Memory Corruption while executing the program ? In nut shell, what is/are the realtion/s between the Memory Leak and Memory Corruption. Juts Theoritical Assumtion below:
4
6095
by: Don Nell | last post by:
Hello Why is there a memory leak when this code is executed. for(;;) { ManagementScope scope = new ManagementScope(); scope.Options.Username="username"; scope.Options.Password="password"; scope.Path.Path=@"\\pc\root\cimv2";
20
8124
by: jeevankodali | last post by:
Hi I have an .Net application which processes thousands of Xml nodes each day and for each node I am using around 30-40 Regex matches to see if they satisfy some conditions are not. These Regex matches are called within a loop (like if or for). E.g. for(int i = 0; i < 10; i++) { Regex r = new Regex();
13
1553
by: Boni | last post by:
I use 3-d party component. In this component I must pass a reference to my object. The problem is that this component has an ugly bug.When this component is disposed, it incorrectly don't delete the reference to my object from one of its shared lists.And since the operation repeats many times the leak is huge. Is there a way to kill my object anyway? Thanks a lot, Boni
3
5332
by: Jim Land | last post by:
Jack Slocum claims here http://www.jackslocum.com/yui/2006/10/02/3-easy-steps-to-avoid-javascript- memory-leaks/ that "almost every site you visit that uses JavaScript is leaking memory". Anybody know anything about this? Does *Javascript* leak memeory, or does the *browser* leak memory?
0
1677
by: nejucomo | last post by:
Hi folks, Quick Synopsis: A test script demonstrates a memory leak when I use pythonic extensions of my builtin types, but if I use the builtin types themselves there is no memory leak. If you are interested in how builtin/pure-python inheritance interacts
9
311
by: Joakim Hove | last post by:
Hello, I have the following code: foo_ptr * alloc_foo(int size, ...) { /* This function allocates an instance of the foo_ptr type, and returns a pointer to the newly allocated storage. */ }
20
1825
by: gNash | last post by:
Hi all, void main() { char *fp; fp=malloc(26); strcpy(fp,"ABCDEFGHIJKLMNOPQRSTUVWXYZ"); fp='\0'; free(fp); }
22
9371
by: Peter | last post by:
I am using VS2008. I have a Windows Service application which creates Crystal Reports. This is a multi theaded application which can run several reports at one time. My problem - there is a memory leak someplace. I can not detect the memory leak by running several reports by hand, but when I run tha app as a servrice and process few hundred reports there is significant memory leak. The application can consume over 1GB of memory where it...
0
9793
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However, people are often confused as to whether an ONU can Work As a Router. In this blog post, we’ll explore What is ONU, What Is Router, ONU & Router’s main usage, and What is the difference between ONU and Router. Let’s take a closer look ! Part I. Meaning of...
0
9642
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can effortlessly switch the default language on Windows 10 without reinstalling. I'll walk you through it. First, let's disable language synchronization. With a Microsoft account, language settings sync across devices. To prevent any complications,...
0
10777
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers, it seems that the internal comparison operator "<=>" tries to promote arguments from unsigned to signed. This is as boiled down as I can make it. Here is my compilation command: g++-12 -std=c++20 -Wnarrowing bit_field.cpp Here is the code in...
0
10493
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven tapestry of website design and digital marketing. It's not merely about having a website; it's about crafting an immersive digital experience that captivates audiences and drives business growth. The Art of Business Website Design Your website is...
0
10206
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each protocol has its own unique characteristics and advantages, but as a user who is planning to build a smart home system, I am a bit confused by the choice of these technologies. I'm particularly interested in Zigbee because I've heard it does some...
0
9315
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing, and deployment—without human intervention. Imagine an AI that can take a project description, break it down, write the code, debug it, and then launch it, all on its own.... Now, this would greatly impact the work of software developers. The idea...
1
7747
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new presenter, Adolph Dupré who will be discussing some powerful techniques for using class modules. He will explain when you may want to use classes instead of User Defined Types (UDT). For example, to manage the data in unbound forms. Adolph will...
1
4416
by: 6302768590 | last post by:
Hai team i want code for transfer the data from one system to another through IP address by using C# our system has to for every 5mins then we have to update the data what the data is updated we have to send another system
2
3960
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.