1

I declare the following two function pointers in my class:

void (*ptrFunc)(void *);
bool (*ptrValid)(char *);

Now for some reason, the second pointer (ptrValid) causes the program to crash on exit. When I comment out the declaration the program exits fine, but when I un-comment it, it crashes.

Nothing is being assigned to it, it isn't being called, just declared.

Am I missing something here?

Marcin
  • 1,236
  • 2
  • 15
  • 28
  • 4
    A declaration alone doesn't lead to crashes - we need to see more context. – Georg Fritzsche Jul 03 '10 at 23:11
  • Turns out it crashes when I declare more than one function pointer in the class. When only one is declared, it exits fine. Could I possibly be declaring them wrong? Edit: Ok, it is something to do with my assignment operator overload and cloning. Thanks george – Marcin Jul 03 '10 at 23:14
  • Then it's probably not related to the function pointer declaration, but the size of the object. Replace a function pointer with a dummy `void*` variable in the class and see what happens. If there are more crashes, then it's an object size issue. If not, then it's probably something about how you're using the function pointers, in which case you need to show us how you're using the function pointers. – Mike DeSimone Jul 04 '10 at 00:32
  • 2
    What you're missing is your towel operator on line 42. – Edward Strange Jul 04 '10 at 00:41
  • 2
    That's true - towel operators tell the OS "don't panic". :) – holtavolt Jul 04 '10 at 00:48
  • These are __definitions__, not declarations, dammit. I really don't see why people get this wrong all the time. It's not like it is hard to tell one from the other. (See http://stackoverflow.com/questions/1410563/1410632#1410632.) – sbi Jul 04 '10 at 09:56
  • @sbi: they're declarations, too. It's quite unclear how they contribute to the crash. Two possible explanations are (1) adding the name hides another name and (2) adding the member affects the class size. In case (1) the declaration aspect matters, in case (2) the definition aspect matters. – MSalters Jul 05 '10 at 08:58
  • @MSalters: Yes, every definition is a declaration, too. So? C++ also is a "programming language", yet this is tagged `C++`, no `programming language`. The fact that it is also the latter can be inferred by everyone reading the tag `C++`. – sbi Jul 06 '10 at 07:15
  • @sbi: I make the distinction explicit because it's an introduction to the rest of my comment where that is critical. – MSalters Jul 07 '10 at 08:32

1 Answers1

6

What you describe doesn't make sense, that a declaration alone will cause your program to crash. But it might still be true if adding a variable to some class causes the memory usage of the program to differ in a way that causes the crash, if at some other, maybe unrelated, point you are accessing an invalid memory address or causing a memory overrun, etc. Maybe it is just unmasking a problem you had all along.

Try using a memory profiles like Valgrind or DUMA to figure out what's going on with your memory.

Gianni
  • 3,982
  • 16
  • 22