473,399 Members | 3,302 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,399 software developers and data experts.

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:
0x000000001457f85a ***
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 2061
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:
0x000000001457f85a ***
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.comwrote:
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:
0x000000001457f85a ***
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:
0x000000001457f85a ***
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.comwrites:
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
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...
3
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...
4
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...
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...
13
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...
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
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...
18
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...
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". ...
9
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...
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: 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
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
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
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,...
0
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,...
0
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...

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.