L
lars.uffmann
Hi everyone!
I am debugging a big piece of code on the search for memory leaks,
using g++ under suse 9.3.
Since I'm trying to eliminate ALL memory leaks, I now stumbled upon a
class foo that is not ever instantiated (thus no constructor or
destructor ever called), but instead, all of its member variables are
defined as static in the according .cpp file, and it's methods are
invoked by calling foo::bar(...) to invoke method "bar(...)".
foo.h:
----------------
class foo {
public:
foo() {};
~foo() {};
static void bar (const char *);
private:
static char * myString;
static int myInt;
}
foo.cpp:
-----------------
/* definition of static members */
char *foo::myString;
int foo::myInt;
void foo::bar (const char *someString) {
myString = (char *) someString;
myInt = 1;
}
------------------
main.cpp:
------------
#include "foo.h"
int main() {
foo::bar ("Hello World!");
}
--------------
stores a copy of the pointer to the parameter given to foo::bar in
myString, which can cause undefined behaviour, because the parameter
string is a (const char *) and loses validity after the function exits.
Question no. 1: Is that correct?
Question no. 2: Is the usage of classes without an instance excusable
under ANY circumstances? I totally disapprove of this style, but maybe
it's just my lack of understanding of how objects work?
What I see is that there is no defined point to free the memory pointed
to
by any member variables of class foo - the job normally done by a
destructor.
Question no. 3:
If - in order to solve this - I create an instance myFoo of class foo,
and remove the
declarations of static member variables in the foo.cpp, can I be sure
that
I will get an error at compile time, if I forget to change any access
to the
variables from foo::myString to myFoo->myString?
That last question seems obvious to answer, but I want a 2nd opinion
Regards,
Lars Uffmann
I am debugging a big piece of code on the search for memory leaks,
using g++ under suse 9.3.
Since I'm trying to eliminate ALL memory leaks, I now stumbled upon a
class foo that is not ever instantiated (thus no constructor or
destructor ever called), but instead, all of its member variables are
defined as static in the according .cpp file, and it's methods are
invoked by calling foo::bar(...) to invoke method "bar(...)".
foo.h:
----------------
class foo {
public:
foo() {};
~foo() {};
static void bar (const char *);
private:
static char * myString;
static int myInt;
}
foo.cpp:
-----------------
/* definition of static members */
char *foo::myString;
int foo::myInt;
void foo::bar (const char *someString) {
myString = (char *) someString;
myInt = 1;
}
------------------
main.cpp:
------------
#include "foo.h"
int main() {
foo::bar ("Hello World!");
}
--------------
myString = (char *) someString;From my understanding of C, the line
stores a copy of the pointer to the parameter given to foo::bar in
myString, which can cause undefined behaviour, because the parameter
string is a (const char *) and loses validity after the function exits.
Question no. 1: Is that correct?
Question no. 2: Is the usage of classes without an instance excusable
under ANY circumstances? I totally disapprove of this style, but maybe
it's just my lack of understanding of how objects work?
What I see is that there is no defined point to free the memory pointed
to
by any member variables of class foo - the job normally done by a
destructor.
Question no. 3:
If - in order to solve this - I create an instance myFoo of class foo,
and remove the
declarations of static member variables in the foo.cpp, can I be sure
that
I will get an error at compile time, if I forget to change any access
to the
variables from foo::myString to myFoo->myString?
That last question seems obvious to answer, but I want a 2nd opinion
Regards,
Lars Uffmann