Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations strongm on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Deleting a class from dll causes error

Status
Not open for further replies.

butthead

Programmer
Feb 24, 2002
545
US
access violation at address xxxxxxxx in module test.dll
write of address xxxxxxxx

and I somtimes get a read address of FFFFFFFF error

I have a class that is instantiated with new from a dll.
when I delete it I get the error.

if I just create the class object as so in a function

Classwidget cw;

when it comes to the end of the function it destroys itself
and there is no problem.

what could be the cause of the error on the delete.

TC
 
I hate access violations. They are a royal pain to track down. Is it possible that something is trying to call that class after it has been deleted? Maybe a scope problem?

James P. Cottingham
-----------------------------------------
[sup]I'm number 1,229!
I'm number 1,229![/sup]
 
I am using this to time the operations of a
database engine I am building. such as load
a table, post, next, etc...

I just cant seem to use new and delete on it.
as the delete goes awry. I have solved some of
the usage problems by declaring

StopWatch sw;

as a global. This solves a lot of usage problems,
but the access violation seems to point to a problem
that should be fixed.

I have used new to create an instance of stopwatch
on a buttonclick and then immediately used delete.
Nothing else. then, bang all heck you know what.

it has been interesting though to watch the results.
if I instantiate the stopwatch object with just the
declaration as above and let the clas destroy itself
by falling through the function I discover that the
construction of this small object is very time consuming.

0.034 seconds as opposed to the quite small amount of
time (usually under a milisecond) to call small functions.

Code:
#include <ctime>
#include <stdio.h>
#include <string.h>

using namespace std;
const int strl = 100;

class __declspec(dllexport) StopWatch
{
    public:

        StopWatch (int Log = 0, char *str = "StopWatch :")
                    : FileName ("Stopwatch.log")
        {
            LogFile = Log;
            if (strlen (str) < strl)
                strcpy (string, str);

            start = clock();
        }

        ~StopWatch ();
        double Stop ();
        void Reset ();
        void LeadString (const char *str);
        
    private:

        clock_t start;
        int LogFile;
        char string [strl];
        const char *FileName;
};

Code:
#include "stopwatch.h"

using namespace std;


__declspec(dllexport) StopWatch::~StopWatch()
{
    clock_t total = clock () - start; //get elapsed time

    if (LogFile >= 2)
    {
        FILE *file;
        file = fopen (FileName, "a");
        fprintf (file, "%s: %s%.3f\n", string, "(destruct) total elapsed time in seconds: ", (total)/CLOCKS_PER_SEC);
        fclose (file);
    }
}

double __declspec(dllexport) StopWatch::Stop()
{
    clock_t total = clock () - start; //get elapsed time

    if ((LogFile == 1) || (LogFile == 3))
    {
        FILE *file;
        file = fopen (FileName, "a");
        fprintf (file, "%s: %s%.3f\n", string, "(stop) total elapsed time in seconds: ", (total)/CLOCKS_PER_SEC);
        fclose (file);
    }

    return total/CLOCKS_PER_SEC;
}

void __declspec(dllexport) StopWatch::Reset ()
{
    start = clock ();   
}

void __declspec(dllexport) StopWatch::LeadString (const char *str)
{
    strcpy (string, str);
}

as stated earlier this class is in a DLL

TC
 
I forgot

the first is the h file
the second is the cpp

though I am sure that is obvious

grins

TC
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top