views:

277

answers:

4

Okay, so I have two classes, call them A and B--in that order in the code. Class B instantiates class A as an array, and class B also has an error message char* variable, which class A must set in the event of an error. I created a third class with a pure virtual function to set the errorMessage variable in B, then made B a child of that third class. Class A creates a pointer to the third class, call it C--when B initializes the array of A objects, it loops through them and invokes a function in A to set A's pointer to C-- it passes "this" to that function, and then A sets the pointer to C to "this," and since C is B's parent, A can set C->errorMessage (I had to do all this because A and B couldn't simultaneously be aware of each other at compile time).

Anyways it works fine, however, and when I pass command line parameters to main(int,char**), it works unless I pass seven, eight, or more than twelve parameters to it... I narrowed it down (through commenting out lines) to the line of code, in A, which sets the pointer to C, to the value passed to it by B. This made no sense to me... I suspected a memory leak or something, but it seems wrong and I have no idea how to fix it... Also I don't get why specifically seven, eight, and more than twelve arguments don't work, 1-6 and 9-12 work fine.

Here is my code (stripped down)--

//class C
class errorContainer{

public:
    virtual ~errorContainer(){ }
    virtual void reportError(int,char*)=0;

};

//Class A
class switchObject{
    void reportError(int,char*);
    errorContainer* errorReference;

public:
    void bindErrorContainer(errorContainer*);
};

//Class A member function definitions
void switchObject::reportError(int errorCode,char* errorMessage){
    errorReference->reportError(errorCode,errorMessage);
}

void switchObject::bindErrorContainer(errorContainer* newReference){
    errorReference=newReference; //commenting out this line fixes the problem
}

//Class B
class switchSystem: public errorContainer{
    int errorCode;
    char* errorMessage;

public:
    switchSystem(int); //MUST specify number of switches in this system.
    void reportError(int,char*);
    int errCode();
    char* errMessage();
    switchObject* switchList;
};

//Class B member function definitions
switchSystem::switchSystem(int swLimit){
    int i;
    switchList=new (nothrow) switchObject[swLimit];
    for(i=0;i<swLimit;i++){
     switchList[i].bindErrorContainer(this);
    }
    errorCode=0;
    errorMessage="No errors.";
}

void switchSystem::reportError(int reportErrorCode,char* reportErrorMessage){
    int len=0,i; 
    errorCode=reportErrorCode;
    if(errorMessage){
     delete[] errorMessage;
    }
    while(reportErrorMessage[len]!='\0'){
     len++;
    }
    errorMessage=new char[len];
    for(i=0;i<=len;i++){
     errorMessage[i]=reportErrorMessage[i];
    } 
}

int switchSystem::errCode(){
    return errorCode; 
}

char* switchSystem::errMessage(){
    return errorMessage; 
}

Anyone know what I've done wrong here? It's bugging the crap out of me... I can't seem to fix it.

---EDIT--- okay, I have it set up the way I do so that I can use it like this in main()

int main(int argc,char** argv){
   switchSystem sw (2)
   sw.switchList[0].argumentCount=2;
   sw.switchList[1].argumentCount=0;
   sw.switchList[0].identifier="a";
   sw.switchList[1].identifier="switch";
   sw.init(argc,argv);
   if(sw.errCode()>0){
      cout<< "Error "<< sw.errCode()<< ": "<< sw.errMessage()<< endl;
   }
}

this program is supposed to read the command line arguments and handle user defined "switches"--like how most command line programs handle switches, but instead of testing for all of them at the beginning of main I wanted to try to write a class and some functions to do it for me--create a switchSystem object with the number of switches, set their identifiers, whether or not they take arguments, and then pass the command line arguments to "init()" to sort it out. Then test like,

if(sw.isSet("switch")){ ... }
etc.

+5  A: 

It seems scary that you:

  • Mix dynamic memory with static string constants ("No errors.") in the same pointer.
  • Use an explicit while-loop to compute the string's length; have you not heard of strlen()?
  • Use such low-level C-like string processing, for no good reason ... What's wrong with std::string?
  • Don't properly account for the terminating '\0' in the string, when allocating space for it and copying it. The length is also not stored, leaving the resulting char array rather difficult to interpret.
unwind
What can mixing dynamic memory and static constants cause?what's wrong with a while loop instead of strlen?And what's scary about c-style strings?Also, none of these seem to contribute to the error I'm getting...
winrar
Using a loop instead of strlen is longer to type, harder to understand, much more error prone, less readable, and you basically gain nothing but troubles.
Marc
Thanks Marc, that about sums it up. :)
unwind
+4  A: 
  • reportError() should be declared virtual in switchSystem, as it is in errorContainer.
  • char* should instead be std::string to avoid all of that needless work.
  • Is there some reason that you can't use an std::vector<switchObject> instead of new[]?
  • You shouldn't delete[] errorMessage when it points to a static literal string. This leads to undefined behavior. (Translation: Bad Thing(TM).)
  • Why are you iteratively counting and copying the contents of a char*? This is begging for trouble. You're not doing anything to protect yourself from harm.
  • Why must switchObject pass a string to switchSystem? Wouldn't it be better to simply return an error code or throw some class derived from std::exception? Or perhaps it should send a string to a global logging facility?

I think perhaps you should rethink your design instead of trying to fix this.

greyfade
Thanks--I'm going to start using strings and vectors of strings, as well learn to use the try/catch/throw statements to handle my errors better.
winrar
A: 

I think the memory leak is your minor cocnern in here. You better throw this code and start over again with a new approach. This is a mess.

Marc
+2  A: 

All of the above message contains really good advice, unless this is homework and you can't use STL I'd recommend that you follow them, your problems will be much less.

For example in this snippet

errorMessage=new char[len];
for(i=0;i<=len;i++){
  errorMessage[i]=reportErrorMessage[i];
}

You have allocated len bytes, but you are writing to len+1 bytes, you have not allocated memory for the null terminator '\0', overwriting memory lead to nasty bugs that are difficult to trackdown.

Ismael