home *** CD-ROM | disk | FTP | other *** search
/ NetNews Usenet Archive 1992 #20 / NN_1992_20.iso / spool / comp / sys / next / programm / 6115 < prev    next >
Encoding:
Text File  |  1992-09-11  |  4.7 KB  |  134 lines

  1. Newsgroups: comp.sys.next.programmer
  2. Path: sparky!uunet!europa.asd.contel.com!darwin.sura.net!wupost!m.cs.uiuc.edu!m.cs.uiuc.edu!callsen
  3. From: callsen@cs.uiuc.edu (Christian J. Callsen)
  4. Subject: C++ new/delete problems
  5. Message-ID: <CALLSEN.92Sep11134819@ganges.cs.uiuc.edu>
  6. Sender: news@m.cs.uiuc.edu (News Database (admin-Mike Schwager))
  7. Organization: Department of Computer Science, UIUC
  8. Distribution: comp
  9. Date: Fri, 11 Sep 1992 19:48:19 GMT
  10. Lines: 122
  11.  
  12.  
  13. Hi Netters,
  14.  
  15. I an currently undertaking a medium programming project, where I am
  16. building an interpreter for a small distributed programming language.
  17. I am using C++ for the main interpreter. My NeXTstation TURBO is
  18. running NeXTstep 2.2 Extended.
  19.  
  20. The program compiles fine with just a single, but harmless, warning.
  21.  
  22. My problem occurs when I run the program, and seems to be the
  23. following: during the parsing of an input file, I allocate and
  24. construct a parsetree matching the parse of the program. Allocation is
  25. strictly done with "new". I can then perform an operation on the
  26. entire parsetree, and when I want to discard the parsetree (i.e.
  27. delete it), I get the following error:
  28.  
  29. ------------------------------------ oOo -----------------------------------
  30. rivendell% ./interpreter <arguments>
  31.  
  32. .. lots of output ..
  33.  
  34. TypeValue::~TypeValue: deleting id "test2"
  35. memory allocation error: attempt to free or realloc space not in heap
  36. bad zone
  37. IOT trap (core dumped)
  38. rivendell%
  39.  
  40. Now, if I run 'gdb' on the core:
  41.  
  42. rivendell% gdb interpreter core
  43. GDB -- NeXT Release 2.0 (v19) -- GNU version 3.1
  44. Copyright (C) 1988 Free Software Foundation, Inc.
  45. There is ABSOLUTELY NO WARRANTY for GDB; type "info warranty" for details.
  46. GDB is free software and you are welcome to distribute copies of it
  47. under certain conditions; type "info copying" to see the conditions.
  48. Reading symbol data from interpreter...
  49. Reading symbol data from /usr/shlib/libsys_s.B.shlib...
  50. Reading Objective-C data...
  51. done.
  52. Type "help" for a list of commands.
  53. (gdb) bt
  54. #0  0x50078e4 in kill ()
  55. #1  0x5 in ?? ()
  56. #2  0x50479e2 in abort ()
  57. #3  0x504b912 in DefaultMallocError ()
  58. #4  0x500ee34 in free ()
  59. #5  0x505ebbc in __builtin_delete ()
  60. #6  0x11c46 in _$_TypeValue ($this=(struct TypeValue *) 0xc8f24, __in$charge__=1) (TypeValue.cc line 310)
  61. (gdb) <jump to stackframe #6>
  62. (gdb) print this
  63. $1 = (struct TypeValue *) 0xc8f24
  64. (gdb) print *this
  65. $2 = {
  66.   info = (union : v_long, v_ulong, v_float, v_double, v_string, v_name, v_pattern, v_cap, v_methodname, Array, Struct, Pointer);
  67.   Tag = TypeId;
  68. }
  69. (gdb) print this->info.v_string
  70. $3 = (char *) 0xc8e3c "test2"
  71. (gdb) quit
  72. rivendell%
  73.  
  74. So it would seem by comparing the pointer to "this" in #6 and the
  75. pointer to the string ($3) that they are allocated from the same area.
  76. This is indeed the case. The source code looks like this:
  77.  
  78. ------------------------------------ oOo -----------------------------------
  79. This is where the call to delete fails:
  80.  
  81. ~TypeValue::TypeValue() {
  82.     switch (this->Tag) {
  83.        /* other cases deleted */
  84.         case TypeId:
  85.         if (this->info.v_string) {
  86.             Debug->Putf(DebugTypesValues,"TypeValue::~TypeValue: deleting id \"%s\"\n",this->info.v_string);
  87.             delete this->info.v_string;/* <--- here! */
  88.         }
  89.         }
  90. }
  91.  
  92.  
  93. The memory is allocated with another method (in the same file) like this:
  94.  
  95. int TypeValue::SetIdValue (char *identifier)
  96. {
  97.     if (this->Tag != TypeId)
  98.     return 0;
  99.  
  100.     // Copy the string that is the string value
  101.     this->info.v_string = new char [ strlen(identifier)+1 ];
  102.     strcpy(this->info.v_string,identifier);
  103.  
  104.     return 1;
  105. }
  106.  
  107. ------------------------------------ oOo -----------------------------------
  108.  
  109. The member 'v_string' is a member of a union.
  110. I have tried to reproduce the error by making a very small version of
  111. the "class" in question, and trying to delete a member in a union, but
  112. that worked just fine, no problemo.
  113.  
  114. So, could anyone point to:
  115.     a) What does the reported error *mean exactly*? Why is it there?
  116.     b) Can I get rid of it?
  117.     c) How?
  118.  
  119. Thank you in advance for any pointers. Oh, yeah, you might want to use
  120. Email to save some bandwidth on the netnews. And, please: No NeXTmail
  121. -- my NeXT is not connected to the net -- all I got there is a
  122. DECstation (sigh).
  123.  
  124. Chris
  125.    __               | Christian J. Callsen  [ callsen@biobio.cs.uiuc.edu ]
  126.   /  ) /            | Currently visiting: U of Illinois, Urbana-Champaign,
  127.  /    /_  __  o _   | Dept. of Comp. Sci., 1304 W. Springfield Avenue,
  128. (__/ / /_/ (_<_/_)_ | Urbana, IL 61801, USA     { Sm:^>le and be Happy Today! }
  129. --
  130.    __               | Christian J. Callsen  [ callsen@biobio.cs.uiuc.edu ]
  131.   /  ) /            | Currently visiting: U of Illinois, Urbana-Champaign,
  132.  /    /_  __  o _   | Dept. of Comp. Sci., 1304 W. Springfield Avenue,
  133. (__/ / /_/ (_<_/_)_ | Urbana, IL 61801, USA     { Sm:^>le and be Happy Today! }
  134.