473,586 Members | 2,718 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

memory leak problem.

Hello all..

I am experiencing memory leakes in on of my applications that i am
developing. the % of memory used for the application is increasing as
shown in the top command on linux.

The memory is leaking only when a function is called where i am
allocating memory dynamically to a pointer(char string,i,e c-string).
if i replace the logic with the use of stl string the memory leaking
problem goes off..

but i need to understand why the memory is leaking...

the overall structure is as follows..

void function()
{

char *str=new cahr[100];

///code where this string is manipulated and used....
//delete str;
}

now this function is called numerous times while the application is
running...

i know that i need to deallocate the explicit mem allocation through
free or delete...but after uncommenting the delete command the
application is aborted as soon as this function is invoked when a
specific condition is met durining application execution.
the application is aborted with the following on the command prompt:
*** glibc detected *** ./exe: munmap_chunk(): invalid pointer:
0x000000001457f 85a ***
then a backtrace log is generated...

i am deleting the pointer only after its of no more use...then why is
the application being aborted saying invalid pointer...
and also after each of this function call...a newer activation frame
for the function is generated so a new pointer should exist on each
function call...

How can the memory leaking be fixed in such a scenario?

Kindly help.

Aug 26 '08 #1
6 2081
ashjas wrote:
Hello all..

I am experiencing memory leakes in on of my applications that i am
developing. the % of memory used for the application is increasing as
shown in the top command on linux.

The memory is leaking only when a function is called where i am
allocating memory dynamically to a pointer(char string,i,e c-string).
if i replace the logic with the use of stl string the memory leaking
problem goes off..

but i need to understand why the memory is leaking...

the overall structure is as follows..

void function()
{

char *str=new cahr[100];

///code where this string is manipulated and used....
//delete str;
}

now this function is called numerous times while the application is
running...

i know that i need to deallocate the explicit mem allocation through
free or delete...but after uncommenting the delete command the
application is aborted as soon as this function is invoked when a
specific condition is met durining application execution.
the application is aborted with the following on the command prompt:
*** glibc detected *** ./exe: munmap_chunk(): invalid pointer:
0x000000001457f 85a ***
then a backtrace log is generated...

i am deleting the pointer only after its of no more use...then why is
the application being aborted saying invalid pointer...
and also after each of this function call...a newer activation frame
for the function is generated so a new pointer should exist on each
function call...

How can the memory leaking be fixed in such a scenario?
1. Memory allocated with new[] needs to be deallocated with delete[],
not delete.

2. Why don't you just use a std::vector? It will handle the memory
management for you.

Aug 26 '08 #2
On Aug 26, 12:47 am, ashjas <ash...@gmail.c omwrote:
Hello all..

I am experiencing memory leakes in on of my applications that i am
developing. the % of memory used for the application is increasing as
shown in the top command on linux.

The memory is leaking only when a function is called where i am
allocating memory dynamically to a pointer(char string,i,e c-string).
if i replace the logic with the use of stl string the memory leaking
problem goes off..

but i need to understand why the memory is leaking...

the overall structure is as follows..

void function()
{

char *str=new cahr[100];

///code where this string is manipulated and used....
//delete str;
delete [] str;

i see no reason to use the heap here, what's wrong with
char str[100];
why not use std::string ?
or std::vector< char ?
>
}

now this function is called numerous times while the application is
running...

i know that i need to deallocate the explicit mem allocation through
free or delete...but after uncommenting the delete command the
application is aborted as soon as this function is invoked when a
specific condition is met durining application execution.
the application is aborted with the following on the command prompt:
*** glibc detected *** ./exe: munmap_chunk(): invalid pointer:
0x000000001457f 85a ***
then a backtrace log is generated...
as expected
>
i am deleting the pointer only after its of no more use...then why is
the application being aborted saying invalid pointer...
because a pointer to a single char is not the same as a pointer to an
array.
you allocated an array and tried to delete a single char. Since str is
not a pointer to a char you get the diagnostic.
and also after each of this function call...a newer activation frame
for the function is generated so a new pointer should exist on each
function call...

How can the memory leaking be fixed in such a scenario?

Kindly help.
Aug 26 '08 #3
red floyd wrote:
ashjas wrote:
>Hello all..

I am experiencing memory leakes in on of my applications that i am
developing. the % of memory used for the application is increasing as
shown in the top command on linux.

The memory is leaking only when a function is called where i am
allocating memory dynamically to a pointer(char string,i,e c-string).
if i replace the logic with the use of stl string the memory leaking
problem goes off..

but i need to understand why the memory is leaking...

the overall structure is as follows..

void function()
{

char *str=new cahr[100];

///code where this string is manipulated and used....
//delete str;
}

now this function is called numerous times while the application is
running...

i know that i need to deallocate the explicit mem allocation through
free or delete...but after uncommenting the delete command the
application is aborted as soon as this function is invoked when a
specific condition is met durining application execution.
the application is aborted with the following on the command prompt:
*** glibc detected *** ./exe: munmap_chunk(): invalid pointer:
0x000000001457 f85a ***
then a backtrace log is generated...

i am deleting the pointer only after its of no more use...then why is
the application being aborted saying invalid pointer...
and also after each of this function call...a newer activation frame
for the function is generated so a new pointer should exist on each
function call...

How can the memory leaking be fixed in such a scenario?

1. Memory allocated with new[] needs to be deallocated with delete[],
not delete.

2. Why don't you just use a std::vector? It will handle the memory
management for you.
Usually vector<charis bad choice, std::string is better. But or course
it depends whether you are storing characters or binary data.
But "char *str=new char[100];" is usually plain stupid,
char str[100] has same effect but not the overhead of dynamic memory
management.

To OP : if program crashes at delete then you have should be looking
memory corruption much earlier, buffer overruns etc.

ismo
Aug 26 '08 #4
ashjas wrote:
How can the memory leaking be fixed in such a scenario?
Use std::string. There's no need to use an explicitly allocated char
array.
Aug 26 '08 #5
ashjas <as****@gmail.c omwrites:
char *str=new cahr[100];

///code where this string is manipulated and used....
//delete str;
Should be: delete [] str;

-- Alain.
Aug 26 '08 #6
Hello,

ashjas wrote:
I am experiencing memory leakes in on of my applications that i am
developing. the % of memory used for the application is increasing as
shown in the top command on linux.
Since you are on linux, learn to use valgrind. It will tell you about
lots of errors with new, delete and leaks, and it will tell you the
location where errors happen. You should read about libstdc++ as well
to turn off some of its allocation features to improve results of
valgrind.

Bernd Strieder

Aug 26 '08 #7

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

Similar topics

10
8697
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()
3
4656
by: Jeremy Lemaire | last post by:
Hello, I am working on cross platform code that is displaying a huge memory leak when compiled on 11.00 HPUX using the aCC -AA flag. It is not leaking on NT, LINUX, Solaris, or HPUX without the -AA flag. In another news group I came across some interesting (ok scarey) information regarding memory leaks in the STL list<...> container. I...
4
2343
by: Morten Aune Lyrstad | last post by:
Ok, now I'm officially confused. I have a large project going, which uses a win32 ui library I am developing myself. And I'm getting weird memory leaks. I don't know if I can explain what is going on, but I really need some help on this one. Ok, so I have this class defined (written by Randy Charles Morin, www.kbcafe.com) which detects...
17
4786
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 easier to reproduce (e.g.: remote machine not available). After about 1 day, I get a usage of 300MB of memory. I have used .NET Memory Profiler tool to...
13
1531
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...
23
4529
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
7
6923
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 the status of the machines. Upon we follow the .NET object lifetime recommendations the application is constantly consuming more memory! The problem...
18
2923
by: diffuser78 | last post by:
I have a python code which is running on a huge data set. After starting the program the computer becomes unstable and gets very diffucult to even open konsole to kill that process. What I am assuming is that I am running out of memory. What should I do to make sure that my code runs fine without becoming unstable. How should I address the...
3
5303
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?
9
4203
by: jeungster | last post by:
Hello, I'm trying to track down a memory issue with a C++ application that I'm working on: In a nutshell, the resident memory usage of my program continues to grow as the program runs. It starts off at a nice 4% of memory, then slowly grows up to 50% and beyond. This translates to around 2 gigs of physical memory, and that's really way...
0
8200
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. ...
0
8338
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...
1
7954
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows Update option using the Control Panel or Settings app; it automatically checks for updates and installs any it finds, whether you like it or not. For...
0
8215
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...
1
5710
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...
0
3864
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
1
2345
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
1
1448
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.
0
1179
bsmnconsultancy
by: bsmnconsultancy | last post by:
In today's digital era, a well-designed website is crucial for businesses looking to succeed. Whether you're a small business owner or a large corporation in Toronto, having a strong online presence can significantly impact your brand's success. BSMN Consultancy, a leader in Website Development in Toronto offers valuable insights into creating...

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.