-1

I created a class that represents a packet of information as described on this code:

#ifndef PACKET_H_
#define PACKET_H_

namespace std {

class Packet
{
public:
    Packet();

    virtual ~Packet();

    void initClass();

    void setStartP(char);
    void setAddFrom(char);
    void setAddTo(char);
    void setpDataSize(char);
    void setpNumber(char);
    void setChecksum(char);
    void setEndP(char);
    void LoadData(char);

    char getStartP();
    char getAddFrom();
    char getAddTo();
    char getpDataSize();
    char getChecksum();
    char getEndP();
    char getData();



private:
    char pB[261];
    char pDataMax;
    char pDataIndex;


};

} /* namespace std */

#endif /* PACKET_H_ */


#include "Packet.h"
#include <iostream>

namespace std {


Packet::Packet()
{

    pDataIndex = 0;
    initClass();
}

Packet::~Packet()
{
    delete this;
}

void Packet::setStartP(char startChar)
{

    pB[0] = startChar;
    cout << "in Set!";
}
void Packet::setAddFrom(char fromChar)
{

}
void Packet::setAddTo(char toChar)
{

}
void Packet::setpDataSize(char dataSizeChar)
{

}
void Packet::setpNumber(char packetNumber)
{

}
void Packet::setChecksum(char checksumChar)
{

}
void Packet::setEndP(char endChar)
{

}
void Packet::LoadData(char dataChar)
{


}

char Packet::getStartP()
{

    return pB[0];
    cout << "in Get";
}
char Packet::getAddFrom()
{
    return pB[1];
}
char Packet::getAddTo()
{
    return pB[2];
}
char Packet::getpDataSize()
{
    return pB[3];
}
char Packet::getChecksum()
{
    return pB[4];
}
char Packet::getEndP()
{
    return pB[260];
}

char Packet::getData()
{
    return pB[6 + pDataIndex];
}

void Packet::initClass()
{
    pDataMax = 254;
    pDataIndex = 0;

}

}

At this point i am just testing it so I just implemented two of the methods. When I try to run the program:

#include <iostream>
#include "Packet.h"


using namespace std;

Packet myPacket;

void buildPacket();

int main() {

    buildPacket();

    return 0;
}


void buildPacket( )
{
    char startP = 0x28;
    cout << "Setting startP!" << endl;
    myPacket.setStartP(startP);
    cout << "Getting startP" << endl;
    cout << myPacket.getStartP() << endl;
    cout << "Done";
}

The code is fine a compile/build time no issues there, it is a run time it falls over. This is really thruowing me, it really is making me doubt what I actually know about class creation and use in C++. The program will run up to a certain point and then crashes with a windows message. on the console this is as far as it gets before crashing:

Setting startP! in Set!Getting startP (

As I can see it it seems to be on deletion that it crashes but not sure why. I looked around for similar issues but can't really find a reason why it is coming up with this, I would be grateful for some help on this one.

Paul R
  • 208,748
  • 37
  • 389
  • 560
Enginebeat
  • 23
  • 1
  • 1
  • 7
  • 2
    Welcome to Stack Overflow! It sounds like you may need to learn how to use a debugger to step through your code. With a good debugger, you can execute your program line by line and see where it is deviating from what you expect. This is an essential tool if you are going to do any programming. Further reading: **[How to debug small programs](http://ericlippert.com/2014/03/05/how-to-debug-small-programs/)** – NathanOliver Mar 21 '17 at 12:05
  • 5
    You misunderstand the meaning of the destructor. It's not used to delete `this`. `this` already *is* being "deleted", the destructor call is part of that deletion process. – Some programmer dude Mar 21 '17 at 12:08
  • 1
    You also misunderstand how statements are executed, which (in the absence of loops) is straight top to bottom. If you have two statements in a function, the first one will always happen first. And if that first statement is a `return` statement, then it will be executed immediately. The statements after the `return` will not execute. – Some programmer dude Mar 21 '17 at 12:09
  • 1
    Having 3 data members and 7 setters, you might also want to read [Are getters and setters poor design?](http://stackoverflow.com/questions/565095/are-getters-and-setters-poor-design-contradictory-advice-seen) – Bo Persson Mar 21 '17 at 12:11
  • Hi Guys, thank you for the reality check, sometimes you look at something long enough that you can't see the problem anymore. – Enginebeat Mar 21 '17 at 12:36
  • Hi Bo, hte getters and setters may change along the way but I normally use this kind of setup in my c programs as a union bettween the array of char and a structure of the individual bytes so I can pass the data as required to all the functions that needed it. Here i am just trying to test the same functionality using a class, may not be a good idea in the end. Will read throught the link supplied. thank you. – Enginebeat Mar 21 '17 at 12:41
  • Hi Some Programmer dude, didn't misunderstood how statements are executed, the statement has been left there from when I was trying to do some other testing on it. thank you. – Enginebeat Mar 21 '17 at 12:44
  • Hi Nathan, will read throught your link to give me some new ideas. I do use the debugger but i just started using eclipse and not sure of functionality thank you. – Enginebeat Mar 21 '17 at 12:47

1 Answers1

1

Don't call delete this in the destructor. The object is automatically destructed since it goes out of scope, no need for delete.

You can read more about it here: http://en.cppreference.com/w/cpp/language/scope

Philipp Ludwig
  • 3,758
  • 3
  • 30
  • 48