memory leak problem.


A

ashjas

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.
 
Ad

Advertisements

R

red floyd

ashjas said:
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.
 
S

Salt_Peter

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 said:
}

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.
 
I

Ismo Salonen

red said:
ashjas said:
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<char> is 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
 
Ad

Advertisements

B

Bernd Strieder

Hello,
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
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Top