Segmentation on calling new() in C++

Segmentation on calling new() in C++

Post by Sabh » Wed, 11 Jul 2001 07:03:53



Hi All,

      I have a class that tries to allocate memory dynamically after
doing some processing in a member variable. The class contains:

2 integers,
2 char[256];
and 1 char* ptr;

     This "ptr" would be allocated later on when I get hold of a valid
input.

During initial Creation of the objects, things go through fine. I am
able to allocate memory dynamically for this class. sizeof(myclass)
turns out to be around 1040 bytes.

But after 2 runs, the third time when I try to allocate a new instance
by calling "new myclass()" - it just fails with a Segmentation. The same
code goes through fine the first 2 times. I am afraid I might have
corrupted the malloc/new memory management scheme.  If I actually remove
the "ptr" as a member variable of the class, it works fine any number of
times.  I tried to allocate using malloc which also failed at the same
point.

  cout << "Size of mapping is " << sizeof(mapping) << endl;
  void* ptr1 = malloc(sizeof(mapping));                    ------> fails

  //void* ptr1 = valloc(1040);
------> fails
  cout << " allocated memory\n";
  cout.flush();
  ptr = new mapping(resourceName, 0, this->count);   ------> fails

Can somebody provide help on this?

thanks in advance,
Sabha

 
 
 

Segmentation on calling new() in C++

Post by Fletcher Glen » Wed, 11 Jul 2001 08:16:22



> Hi All,

>       I have a class that tries to allocate memory dynamically after
> doing some processing in a member variable. The class contains:

> 2 integers,
> 2 char[256];
> and 1 char* ptr;

>      This "ptr" would be allocated later on when I get hold of a valid
> input.

> During initial Creation of the objects, things go through fine. I am
> able to allocate memory dynamically for this class. sizeof(myclass)
> turns out to be around 1040 bytes.

> But after 2 runs, the third time when I try to allocate a new instance
> by calling "new myclass()" - it just fails with a Segmentation. The same
> code goes through fine the first 2 times. I am afraid I might have
> corrupted the malloc/new memory management scheme.  If I actually remove
> the "ptr" as a member variable of the class, it works fine any number of
> times.  I tried to allocate using malloc which also failed at the same
> point.

>   cout << "Size of mapping is " << sizeof(mapping) << endl;
>   void* ptr1 = malloc(sizeof(mapping));                    ------> fails

>   //void* ptr1 = valloc(1040);
> ------> fails
>   cout << " allocated memory\n";
>   cout.flush();
>   ptr = new mapping(resourceName, 0, this->count);   ------> fails

> Can somebody provide help on this?

> thanks in advance,
> Sabha

Problems like this can be damnably hard to find, because the
cause of the problem can seem to be completely unrelated to
the point where the symptom occurs.  This is why programs
like Electric Fence, or Rational's Purify are popular.  I
seriously doubt that the existence of your pointer is the
cause.  It is more likely something that you are doing
somewhere else that is manifesting when you create your pointer.

--
                Fletcher Glenn


 
 
 

Segmentation on calling new() in C++

Post by Al Morga » Wed, 11 Jul 2001 09:13:00


Quote:>   //void* ptr1 = valloc(1040);
> ------> fails
>   cout << " allocated memory\n";
>   cout.flush();
>   ptr = new mapping(resourceName, 0, this->count);   ------> fails

This is damned little to work on.  Can you post more?  Can you post the
whole thing?  Have you tries 'gdb'?


 
 
 

Segmentation on calling new() in C++

Post by Sabh » Wed, 11 Jul 2001 09:50:19


Hi,

       Running with dbx gave the following info:


> >   //void* ptr1 = valloc(1040);
> > ------> fails
> >   cout << " allocated memory\n";
> >   cout.flush();
> >   ptr = new mapping(resourceName, 0, this->count);   ------> fails

> This is damned little to work on.  Can you post more?  Can you post the
> whole thing?  Have you tries 'gdb'?



 
 
 

Segmentation on calling new() in C++

Post by Al Morga » Wed, 11 Jul 2001 12:06:50


Woah...  does that even compile on your box?  What system are you using,
anyway?

I can find at least one error right off the bat:

mapping.cpp:
O_RDONLY is not defined.  Need to include fcntl.h

MyTest.cpp: line 1:
#include<HashMap.h>

should be:

#include "HashMap.h"

or else it will look in the wrong directories.  Unless you have your
system set up to look in the current directory for libraries, that won't
work.  And don't expect all over people to be set up the same way (I'm
sure not).

Don't you have a Makefile or something?  Or do you compile all the
modules by hand?

What compiler are you using?

And get a different editor :P  The one you have looks like MS-DOS edit;
there are no tabs, just arrays of spaces.

As for the problem you're having, what is the 'ptr1' variable supposed
to do, anyway?  You malloc it, but then don't do anything with it?  That
just makes SegFaulting more mysterious.

And I'm not familiar, I'm sorry, with dbx.  So the output means nothing
to me.

Yeah, I know that wasn't very helpful, but that's as much as I can tell
you at the moment.


 
 
 

Segmentation on calling new() in C++

Post by Sabh » Thu, 12 Jul 2001 02:45:30



> Woah...  does that even compile on your box?  What system are you using,
> anyway?

Solaris 2.7

Quote:

> I can find at least one error right off the bat:

> mapping.cpp:
> O_RDONLY is not defined.  Need to include fcntl.h

Might have got removed when I edited some files.

Quote:

> MyTest.cpp: line 1:
> #include<HashMap.h>

> should be:

> #include "HashMap.h"

> or else it will look in the wrong directories.  Unless you have your
> system set up to look in the current directory for libraries, that won't
> work.  And don't expect all over people to be set up the same way (I'm
> sure not).

Sorry - I had everything header file copied into a common location and
included in INCLUDE.

Quote:

> Don't you have a Makefile or something?  Or do you compile all the
> modules by hand?

I do use a Makefile - but it contains parts that are not associated with
this problem - so didnt sent it over - sorry again  :-(

Quote:

> What compiler are you using?

using CC.

Quote:

> And get a different editor :P  The one you have looks like MS-DOS edit;
> there are no tabs, just arrays of spaces.

I was using just plain vi ;-)

Quote:

> As for the problem you're having, what is the 'ptr1' variable supposed
> to do, anyway?  You malloc it, but then don't do anything with it?  That
> just makes SegFaulting more mysterious.

I just wanted to see whether there was problem with new or malloc. new()
kept failing. Then I tried with malloc - just hoping to see it allocate
memory - but it also fails. Anway, you can comment the malloc and only call
new() - it would still fail.

> And I'm not familiar, I'm sorry, with dbx.  So the output means nothing
> to me.

> Yeah, I know that wasn't very helpful, but that's as much as I can tell
> you at the moment.



thanks,
Sabha
 
 
 

Segmentation on calling new() in C++

Post by Steve Sivie » Sun, 15 Jul 2001 11:03:56




> Problems like this can be damnably hard to find, because the
> cause of the problem can seem to be completely unrelated to
> the point where the symptom occurs.  This is why programs
> like Electric Fence, or Rational's Purify are popular.  I
> seriously doubt that the existence of your pointer is the
> cause.  It is more likely something that you are doing
> somewhere else that is manifesting when you create your pointer.

I second the recommendation to use Electric Fence (or Purify if you've
got it). I've occasionally had problems like this where I (or somebody
else) had pointer problems somewhere else in the code, but the
segmentation fault didn't show up until some random new or malloc.

Steve

 
 
 

1. Segmentation Fault when calling new

 I am having problems with a Segmentation fault when calling new.  My
debugger reports that the fault takes place in __fpu_control.  But, there
is plenty of memory available, so that couldn't be the problem.  However,
this is being called from within a recursive function, that calls itself
many time.  So, I was thinking maybe I had run out of stack space (is
that possible?)  I could not find anything in the man pages for gcc about
a check-stack-frame-overflow option like there is in my DOS compiler.  
Nor was there anything in there about changing stack sizes.  
 Can anyone help me out?  If I'm totally off base about the stack thing
(and I might be, I haven't done extensive programming in Linux before)
what else should I look for that could be causing these faults?

--
/* Daniel */

WWW: http://www.infinet.com/~drk

"Fear is only afraid of the absence of itself."  - Mediocrotes

2. Num Lock on Interactive

3. New multi-platform C++ class lib: call for testers

4. loading shared libs at run time !!

5. Security from outside call-ins

6. request-route and 2.1.90+

7. Plug-Ins using C++

8. TCP/IP Patches

9. How I could add a new system call to linux or modify a system calls

10. How to use open system call in a new system call

11. New HOWTO....HOWTO-Hose-RedHat-Installation (help!)

12. C++ segmentation fault

13. Segmentation Faults when using very large arrays in C++