-2

Possible Duplicate:
What is The Rule of Three?

I just "finished" my AVL tree implementation and went to test what previously worked with a normal binary search tree. But now I am getting these assertion errors when the bsTree constructor is called.

_BLOCK_TYPE_IS_VALID(pHead->nBlockUse) is first and if I continue windows spits the next one out. _CrtIsValidHeapPointer(pUserdata)

TYPE is defined in a type.h as Signal* I plan to change this to use templates or whatever for a polymorphic implementation but this seemed simple for initial setup.

main execution:

#include<string>
#include<iostream>
#include<fstream>
#include"binSearchTree.h"
;
using namespace std;

int main(){

string word;
int i = 0;
ifstream book ("AV1611Bible.txt");

if(book.is_open()){

    book >> word;

    bsTree* tree = new bsTree(new Signal(word));

    while( book.good()){
        book >> word;
        tree->addValue(new Signal(word));
        //cout << word;
        cout << i++ << "\n";
    }
    book.close();
}


return 0;
}

bsTree Constructors:

#include"binSearchTree.h"

;
using namespace std;

bsTree::bsTree(){

root = new Node();
size = 0;
}

bsTree::bsTree(TYPE v){
root = new Node(v);
size = 0;
}

bsTree::~bsTree(){
delete root;
}

Signal constructors:

#include"signal.h"


using namespace std;


Signal::Signal(){
signal = "";
count = 0;
prob = 0;
}

Signal::Signal(string s){
Signal(s,0);
}

Signal::Signal(string s, double p){
signal = s;
count = 0;
prob = p;
}

Signal::Signal(string s, int n, double p){
signal = s;
count = n;
prob = p;
 }

Signal::~Signal(){
delete(&signal);
delete(&count);
delete(&prob);
}
Community
  • 1
  • 1
Glen Nicol
  • 265
  • 1
  • 3
  • 15
  • Aside from the [missing copy ctor and copy assignment operator](http://stackoverflow.com/q/4172722), there's just so much *wrong* in this code... First, stop [`using namespace std;`](http://stackoverflow.com/q/1452721). Second, don't `new` everything, this is not Java or C#. Third, never ever `delete` non-pointers! (You didn't show the header, but I'm pretty sure `signal`, `count` and `prob` are *not* pointers. Fourth, use [smart pointers](http://stackoverflow.com/q/106508) *if* you have to use dynamic allocation (and [use the right one](http://stackoverflow.com/q/8706192)). – Xeo May 26 '12 at 04:29
  • You also didn't show the definition of `Node`, which I can almost see also not abiding the rule of three. – Xeo May 26 '12 at 04:30
  • About namespace, I didn't have them everywhere and then Visual studio complained. And stopped when i put them in. I figured out the problem a little while after I posted. XEO is right, my deconstructor was causing the problem with the deletes. I'll look into the rule of three. I originally learned on java and dont have any formal training with C++. Just trying to get a feel for things. – Glen Nicol May 26 '12 at 05:21
  • Is there a way to mark Xeo's comment as the answer? The option below doesn't seem exactly correct. – Glen Nicol May 26 '12 at 05:23

2 Answers2

0

The code shown above has Undefined Behavior, due to e.g. delete of something that was not allocated with new, and then anything can happen, including whatever behavior it is that you see (which you forgot to describe)

ChrisF
  • 127,439
  • 29
  • 243
  • 315
Cheers and hth. - Alf
  • 135,616
  • 15
  • 192
  • 304
-4

Xeo was correct. The destructors were accessing memory that it didn't have access to.

Glen Nicol
  • 265
  • 1
  • 3
  • 15