1755740 Members
3012 Online
108837 Solutions
New Discussion юеВ

Re: Question on new() !!

 
Mahesh Kurse
Occasional Contributor

Question on new() !!

Compiler being used is aCC -
HP aC++ B3910B A.01.23
HP aC++ B3910B A.01.19.02 Language Support Library

We have a struct like this -

struct Seqof_GSMObjInstance
{
long x;
char *octets;
struct Seqof_GSMObjInstance *next;
};

There is a function which allocated memory for this structure like this -

Seqof_GSMObjInstance * GSMObjInstance_tBin::alloc()
{
Seqof_GSMObjInstance *aPtr = new Seqof_GSMObjInstance();
memset(aPtr, 0, sizeof(SeqObjInst) );

return aPtr;
}

The program was compiled on HP-UX 10.20 but run on HP-UX 11.00.

We had a strange problem. The program crashed and we came to know from gdb through the analysis of the core file that, the program crashed at memset(). A portion of the stack trace is -

(gdb) bt
#0 0xc0f30130 in kill () from /usr/lib/libc.1
#1 0xc0e95b24 in raise () from /usr/lib/libc.1
#2 0xc0e75f38 in _sigaddset () from /usr/lib/libc.1
#3 0xc0e70d64 in abort () from /usr/lib/libc.1
#4 0xc9081958 in skSigMgmt::_OspSignalWrapper (signum=10) at /vob/osp.src/src/libskel/skel/skSigSensorThr.C:571
#5 0xc0d9e6bc in cma__sig_deliver () from /usr/lib/libcma.1
#6 0xc0d9eb78 in cma___sig_sync_term () from /usr/lib/libcma.1
#7
#8 0xc0e7227c in memset () from /usr/lib/libc.1
#9 0x2dd57c in GSMObjInstance_tBin::alloc (this=0x43c1f8dc)

To know, whether the call to new() was succesful, we executed further commands in gdb -
(gdb) f 9
#9 0x2dd57c in GSMObjInstance_tBin::alloc (this=0x43c1f8dc)
(gdb) info locals
aPtr = (struct Seqof_GSMObjInstance *) 0x8
(gdb) p *(struct Seqof_GSMObjInstance *) 0x8
Cannot access memory at address 0x8

From the above output, it is clear that, new() returned a corrupted address (0x8) and that is the reason the program crashed in memset().
The program does not crash every time in the above place but this has crashed atleast 3 times.

We checked the size of core (around 60MB) and it was much less than the kernel parameter - maxdsiz (200MB) set for the process. That means, the program did have enough memory in the heap.

Any idea, what went wrong in the above program ?

Thanks
7 REPLIES 7
harry d brown jr
Honored Contributor

Re: Question on new() !!


I just slapped my developers and sysadmins on Thursday for using an ancient compiler and complaining about the results.

Start here:

http://h21007.www2.hp.com/dev/1,2583,,00.html

and here:

http://h21007.www2.hp.com/dev/technologies/topic/1,2608,10201,00.html

The latest version of the Compiler is A.03.35! (June 2002)

live free or die
harry
Live Free or Die
Jack Tan
Occasional Advisor

Re: Question on new() !!

In your memset() call, you invoke sizeof():

sizeof(SeqObjInst);

Did you mean this instead?

sizeof(Seqof_GSMObjInstance);

Jack
Mahesh Kurse
Occasional Contributor

Re: Question on new() !!

Sorry for the confusion, the parameter passed for memset() is -

memset(aPtr, 0, sizeof(Seqof_GSMObjInstance) );
Jack Tan
Occasional Advisor

Re: Question on new() !!

The code snippet seems okay when placed in a separate program. Maybe there is memory corruption elsewhere that is revealed by this particular code. Have you tried stepping through GSMObjInstance_tBin::alloc() while it executes?

To work around the call to memset(), you can use aggregate initialization:

const Seqof_GSMObjInstance initial = { 0 };
Seqof_GSMObjInstance *aPtr = new Seqof_GSMObjInstance();
*aPtr = initial;

Jack
Vishal Augustine
Frequent Advisor

Re: Question on new() !!

Hi,

Me too faced a similar problem. It goes like this. I was allocating memory to a class-pointer in a function and was assigning it to another pointer (of same class) that was passed as an arg of the function. The interesting aspect is it worked fine but gave me runaways at times.

So the memory allocation was directly performed on the pointer that was passwd as an arg and it worked fine.

I would suggest the following modification to your code.

Seqof_GSMObjInstance * GSMObjInstance_tBin::alloc(Seqof_GSMObjInstance **aPtr)
{
*aPtr = new Seqof_GSMObjInstance();
memset(*aPtr, 0, sizeof(SeqObjInst) );

return *aPtr;
}

Regards
Vishal
Chris De Angelis
Frequent Advisor

Re: Question on new() !!

Seqof_GSMObjInstance is defined as a struct, not a class. So instead of:

= new Seqof_GSMObjInstance();

don't you need:

= new Seqof_GSMObjInstance;

without the "()"? There is no constructor function to invoke here; you just want to allocate memory for some structure.
Adam J Markiewicz
Trusted Contributor

Re: Question on new() !!

Looks like you have general problem with dynamical memory management. Somewhere else in the code you corrupt free memory blocks list and thats why malloc returns sensless value.
I would bet that you are using the object after deallocating memory.

Check it carefully.
Good luck

Adam
I do everything perfectly, except from my mistakes