From: callsen@cs.uiuc.edu (Christian J. Callsen)
Subject: C++ new/delete problems
Organization: Department of Computer Science, UIUC
Distribution: comp
Date: Fri, 11 Sep 1992 19:48:19 GMT


Hi Netters,

I an currently undertaking a medium programming project, where I am
building an interpreter for a small distributed programming language.
I am using C++ for the main interpreter. My NeXTstation TURBO is
running NeXTstep 2.2 Extended.

The program compiles fine with just a single, but harmless, warning.

My problem occurs when I run the program, and seems to be the
following: during the parsing of an input file, I allocate and
construct a parsetree matching the parse of the program. Allocation is
strictly done with "new". I can then perform an operation on the
entire parsetree, and when I want to discard the parsetree (i.e.
delete it), I get the following error:

------------------------------------ oOo -----------------------------------
rivendell% ./interpreter <arguments>

.. lots of output ..

TypeValue::~TypeValue: deleting id "test2"
memory allocation error: attempt to free or realloc space not in heap
bad zone
IOT trap (core dumped)
rivendell%

Now, if I run 'gdb' on the core:

rivendell% gdb interpreter core
GDB -- NeXT Release 2.0 (v19) -- GNU version 3.1
Copyright (C) 1988 Free Software Foundation, Inc.
There is ABSOLUTELY NO WARRANTY for GDB; type "info warranty" for details.
GDB is free software and you are welcome to distribute copies of it
under certain conditions; type "info copying" to see the conditions.
Reading symbol data from interpreter...
Reading symbol data from /usr/shlib/libsys_s.B.shlib...
Reading Objective-C data...
done.
Type "help" for a list of commands.
(gdb) bt
#0  0x50078e4 in kill ()
#1  0x5 in ?? ()
#2  0x50479e2 in abort ()
#3  0x504b912 in DefaultMallocError ()
#4  0x500ee34 in free ()
#5  0x505ebbc in __builtin_delete ()
#6  0x11c46 in _$_TypeValue ($this=(struct TypeValue *) 0xc8f24, __in$charge__=1) (TypeValue.cc line 310)
(gdb) <jump to stackframe #6>
(gdb) print this
$1 = (struct TypeValue *) 0xc8f24
(gdb) print *this
$2 = {
  info = (union : v_long, v_ulong, v_float, v_double, v_string, v_name, v_pattern, v_cap, v_methodname, Array, Struct, Pointer);
  Tag = TypeId;
}
(gdb) print this->info.v_string
$3 = (char *) 0xc8e3c "test2"
(gdb) quit
rivendell%

So it would seem by comparing the pointer to "this" in #6 and the
pointer to the string ($3) that they are allocated from the same area.
This is indeed the case. The source code looks like this:

------------------------------------ oOo -----------------------------------
This is where the call to delete fails:

~TypeValue::TypeValue() {
	switch (this->Tag) {
   	/* other cases deleted */
        case TypeId:
	    if (this->info.v_string) {
	        Debug->Putf(DebugTypesValues,"TypeValue::~TypeValue: deleting id \"%s\"\n",this->info.v_string);
	        delete this->info.v_string;/* <--- here! */
	    }
        }
}


The memory is allocated with another method (in the same file) like this:

int TypeValue::SetIdValue (char *identifier)
{
    if (this->Tag != TypeId)
	return 0;

    // Copy the string that is the string value
    this->info.v_string = new char [ strlen(identifier)+1 ];
    strcpy(this->info.v_string,identifier);

    return 1;
}

------------------------------------ oOo -----------------------------------

The member 'v_string' is a member of a union.
I have tried to reproduce the error by making a very small version of
the "class" in question, and trying to delete a member in a union, but
that worked just fine, no problemo.

So, could anyone point to:
	a) What does the reported error *mean exactly*? Why is it there?
	b) Can I get rid of it?
	c) How?

Thank you in advance for any pointers. Oh, yeah, you might want to use
Email to save some bandwidth on the netnews. And, please: No NeXTmail
-- my NeXT is not connected to the net -- all I got there is a
DECstation (sigh).

Chris
   __               | Christian J. Callsen  [ callsen@biobio.cs.uiuc.edu ]
  /  ) /            | Currently visiting: U of Illinois, Urbana-Champaign,
 /    /_  __  o _   | Dept. of Comp. Sci., 1304 W. Springfield Avenue,
(__/ / /_/ (_<_/_)_ | Urbana, IL 61801, USA     { Sm:^>le and be Happy Today! }
--
   __               | Christian J. Callsen  [ callsen@biobio.cs.uiuc.edu ]
  /  ) /            | Currently visiting: U of Illinois, Urbana-Champaign,
 /    /_  __  o _   | Dept. of Comp. Sci., 1304 W. Springfield Avenue,
(__/ / /_/ (_<_/_)_ | Urbana, IL 61801, USA     { Sm:^>le and be Happy Today! }
Newsgroups: comp.sys.next.programmer