473,387 Members | 1,492 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,387 software developers and data experts.

Memory leak :(

110 100+
Hi all,
I have a structure like this..
Struct p
{
int a;
String x;
String y;
};
now,
String is a class.
if i do this..

while()
{
...
.....
Struct p *ptr;
ptr = new ptr;
}

and lets say the loop executes 'n' number of times..
then 'n' number of times delete ptr is executed..
but this is Showing a leak in the String class..
my question whether 'delete' will destroy the string object?

thnx in advance..
xoinki
Apr 12 '07 #1
5 1446
weaknessforcats
9,208 Expert Mod 8TB
A couple of things. This code:

Struct p *ptr;
ptr = new ptr;

won't compile. ptr is declared a pointer. You are allocating a new pointer and "new" returns the address of the newly created pointer. You cannot assign the address of a pointer to a pointer. You need to store it in a pointer declared to cointain the address of another pointer.

Aside from that, deleting a pointer will destroy the object pointed at. Any memory leaks will be caused by faulty destructors.
Apr 12 '07 #2
Banfa
9,065 Expert Mod 8TB
Aside from that, deleting a pointer will destroy the object pointed at. Any memory leaks will be caused by faulty destructors.
Assuming that the rest of the code is correctly written. There are many causes of memory leaks faulty destructors being one of them.
Apr 12 '07 #3
xoinki
110 100+
Hi,
Thnx for the reply..
I cheked the destructors its not faulty..
but delete on a structure pointer will not call destructor of a String class i fear..
Apr 13 '07 #4
Roonie
99
destructors are rigged to execute automatically for the most part. there are very few instances where explicitly calling a destructor is appropriate. if your struct is deleted properly, then any members of that struct will also be deleted at that time. (unless of course, youre doing something funky youre not telling us about)

if youre still having problems, try creating an extra {} block to contain the code youre working with . . . the destructors for eveything you declare inside should be called automatically when things fall out of scope at the close of your new block. move all references to your struct inside this new block with as little else as possible and it should help. (if it doesnt fix your problem, at least itll make it more readable for you.)

also, if by off chance youre using an array, you need to use [] with the delete statement:
Expand|Select|Wrap|Line Numbers
  1. delete [] yourArray;
  2.  
this tells the compiler to delete the entire array, instead of just the first element (which often causes leaks if youre not looking out.)
Apr 13 '07 #5
xoinki
110 100+
Thnx for the valuable insight sir..
I will try this one out and c
Apr 13 '07 #6

Sign in to post your reply or Sign up for a free account.

Similar topics

8
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 ? ...
17
by: José Joye | last post by:
Hi, I have implemented a Service that is responsible for getting messages from a MS MQ located on a remote machine. I'm getting memory leak from time to time (???). In some situation, it is...
4
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";...
20
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...
23
by: James | last post by:
The following code will create memory leaks!!! using System; using System.Diagnostics; using System.Data; using System.Data.SqlClient; namespace MemoryLeak
8
by: Adrian | last post by:
Hi I have a JS program that runs localy (under IE6 only) on a PC but it has a memory leak (probably the known MS one!) What applications are there that I could use to look at the memory usage of...
7
by: Salvador | last post by:
Hi, I am using WMI to gather information about different computers (using win2K and win 2K3), checking common classes and also WMI load balance. My application runs every 1 minute and reports...
3
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". ...
7
by: Ragnar Agustsson | last post by:
Hi all I have been wandering about the best way to sandbox memory leaks in 3rd party libraries when using them from the .Net framework. I have a 3rd party library, written in C++, that leaks a...
22
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...
0
by: taylorcarr | last post by:
A Canon printer is a smart device known for being advanced, efficient, and reliable. It is designed for home, office, and hybrid workspace use and can also be used for a variety of purposes. However,...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: aa123db | last post by:
Variable and constants Use var or let for variables and const fror constants. Var foo ='bar'; Let foo ='bar';const baz ='bar'; Functions function $name$ ($parameters$) { } ...
0
by: ryjfgjl | last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
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,...
0
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...
0
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...

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.