0

I have a class:

class SendData
{
public:
    SendData(int SendAMsg(int foo, unsigned char *bar, int length), int number)
    {
        m_nDefinePos = 0;
        m_nOtherStuffDefinedAs =0;
    }
    void somestuffhere();
    void ClearDefinition();
private:
    int aLotOfVariableshere;
    int m_nDefinePos;
};

This is the class itself. Then some stuff is called:

SendData* m_pData;
m_pData->ClearDefinition();

Which now calls this one:

void SendData::ClearDefinition()
{
    printf("Welcome to Clear Definition Script\n");
    m_nDefinePos = 0;
    // Some more stuff here
}

Here the code breaks somehow. I get the "Welcome to Clear Definition Script" message in my console, but that's all. It breaks on m_nDefinePos = 0;. (I did put in another printf command after it, never showed in the console.)

I just don't know why it breaks there and i cant find any error.

pmr
  • 58,701
  • 10
  • 113
  • 156
Zett
  • 43
  • 5
  • 1
    1) What do you mean with *the code breaks*. Do you get an unexpected result, a compiler error, or a runtime exception? 2) It's quite hard to tell what could be wrong with as little code as you provided. Try to post a bit more. – Max Truxa Dec 12 '13 at 17:28
  • oh yeah sorry, i forgot to mention: it compiles perfectly, then the program runs to this exact location spills out "Welcome to ClearDefinition Script" and then error message pops up ....exe isnt working anymore When i hit the debug button: "0x0000005: Access Violation" – Zett Dec 12 '13 at 17:29
  • Initialize your pointer before using it: `SendData* m_pData = new SendData(...);` Also your constructor definition looks a bit weird. – πάντα ῥεῖ Dec 12 '13 at 17:30
  • WOOOOO πάντα ῥεῖ! That did it! Man i knew it was something stupid and simple but well.. i am a noob :D – Zett Dec 12 '13 at 17:33
  • 3
    Welcome to SO. Please take a second and accept an answer if your problem is solved, by clicking on the checkmark next to it. – Christopher Creutzig Dec 12 '13 at 17:39
  • 1
    @Zett, please do not add `[Solved]` to your title. If your problem is solved, accept an answer, whether that be your own answer or another's. – gunr2171 Dec 12 '13 at 17:41
  • @Zett: No, don't use `new` unless you really want a dynamic object; and if you do, use a smart pointer to manage it (or, at the very least, remember to `delete` it once you've finished with it). You've just replaced the error with a memory leak. – Mike Seymour Dec 12 '13 at 17:43
  • 1
    Hi, welcome to SO. I removed all the cruft from your question. No one really needs to know all this. Try to be clear and concise. Provide error messages or state clearly what you mean by "breaks here". Don't add [solved] or similar. Click the green check-mark on the answer that solves your problem. – pmr Dec 12 '13 at 17:43
  • Ok thanks for all the hints, I can't answer to my own post somehow, so I checked the other answer. As mentioned above, this is not my own code, im just working on it. It was a MFC written application before and i have to change it to a "normal" console application. I checked up the original code and in fact there was this `SendData* m_pData = new SendData(...);` hidden somewhere. Thanks a lot for all the help :) – Zett Dec 12 '13 at 17:47

1 Answers1

1
SendData* m_pData;
m_pData->ClearDefinition();

This declares a pointer, but doesn't create an object or initialise the pointer to point to anything, so calling a member function via the pointer will go wrong. Perhaps you wanted to create an object:

SendData data(arguments);
data.ClearDefinition();

or perhaps you wanted to initialise the pointer to point an object that already exists:

SendData* m_pData = whatever;
Mike Seymour
  • 249,747
  • 28
  • 448
  • 644
  • It's also worth mentioning allocationg `SomeData` on the heap: `SomeData* m_pData = new SomeData;` – Raxvan Dec 12 '13 at 17:36
  • 3
    @Raxvan: No, using raw pointers to manage dynamic resources is a bad idea, and a lecture on how to deal with dynamic resources properly is beyond the scope of this question. – Mike Seymour Dec 12 '13 at 17:38