c++ access violation - trouble with new

Started by
3 comments, last by Qw3r7yU10p! 19 years, 6 months ago
Hi, i´m having a problem when using the operator "new" to instantiate a new object. It sometimes causes a kernel32.dll access violation at 0xC0000005. The error occurs in this method: void InsetoFactory::fabrica(bool ehPredador, long nuInsetos, double probEsq, double probDir, Ponto* pontoInicial){ if( (nuInsetos > 0) && (pontoInicial != 0)){ for (int i=1; i<= nuInsetos; i++){ /* THIS LINE*/ new Inseto(i, ehPredador, pontoInicial, probEsq, probDir); } } } where: Inseto::Inseto(long identidade, bool ehPred,Ponto* posInicial, double probEsq, double probDir){ this->id = identidade; this->ehPredador = ehPred; this->posicao = new Ponto(posInicial->x, posInicial->y, posInicial->z); this->probEsq = probEsq; this->probDir = 1.0 - probDir; if( ehPred){ ambiente->predadores->push_back( this ); ambiente->sitios[posInicial->x].nuPredadores++; ambiente->nuPredadores++; }else{ ambiente->presas->push_back( this); ambiente->sitios[posInicial->x].nuPresas++; ambiente->nuPresas++; } } Debugging the program, you note that it doesn´t even enter in the constructor of "Inseto". I think it´s a problem with "new" to allocate memory. I´m also sure that the arguments in the "Inseto" constructor call aren´t null. Can anyone give me an idea? Point some error? [Edited by - marciopd on September 23, 2004 6:59:00 PM]
Advertisement
can you also post the class definition for Inseto? maybe there's somehting wrong with the class itself or with the constructor of a parent class that it derives from.

As an important aside, though it's not the problem, it's definitely _a_ problem that you're never saving the pointer that your call to new returns. so after iterating your loop you will have created nuInsetos instance of Inseto, but you're leaking the memory b/c you're not keeping track of the pointers so that you can delete them later. your loop should look something more like:

Inseto myInsetos[nuInsetos];if( (nuInsetos > 0) && (pontoInicial != 0)){    //note: i've modified your iterator values slightly    //      to be more friendly with the array    for (int i=0; i< nuInsetos; i++)    {        myInsetos = new Inseto(i, ehPredador, pontoInicial, probEsq, probDir);    }}//the rest of your program//then when you're done with all your Insetos://note: i've modified your iterator values slightly//      to be more friendly with the arrayfor (int i=0; i< nuInsetos; i++){    if ( myInsetos )        delte myInsetos;}


that way you have no memory leak and you can actually make use of the Insetos class instances that you've created.

-me
Hi! Here is the definition of class Inseto:

#ifndef INSETO_H
#define INSETO_H

#include "Ponto.h"

class Ambiente;

class Inseto
{

public:
bool ehPredador;
long int id;
Ponto* posicao;
double probEsq;
double probDir;

static int nuFilhotes;
static double deltaMaxDifusidade;
static Ambiente* ambiente;


// CONSTRUCTORS and DESTRUCTOR
Inseto(long identidade, bool, Ponto*, double, double); //
~Inseto();

// SET
static void setAmbiente(Ambiente*);
static void setDeltaMaxDifusidade(double deltaMax);

//
void mover(double randomico);
void reproduzir(double randomico);

//
bool operator == ( const Inseto & direita) const;
bool operator < ( const Inseto & ) const;
bool operator <= ( const Inseto & ) const;
bool operator > ( const Inseto & ) const;
bool operator >= ( const Inseto & ) const;
};

#endif


IMPLEMENTATION of some parts:
/*#############################################################*/
Ambiente* Inseto::ambiente = 0;
int Inseto::nuFilhotes=1;
double Inseto::deltaMaxDifusidade=0;

/** Função estática para setar o ambiente ***/
void Inseto::setAmbiente(Ambiente* amb){
ambiente = amb;
}

void Inseto::setDeltaMaxDifusidade(double deltaMax){
deltaMaxDifusidade = deltaMax;
}

// ***** CONSTRUTORES
Inseto::Inseto(long identidade, bool ehPred,Ponto* posInicial, double probEsq, double probDir){

this->id = identidade;
this->ehPredador = ehPred;
this->posicao = new Ponto(posInicial->x, posInicial->y, posInicial->z);

this->probEsq = probEsq;
this->probDir = 1.0 - probDir;


if( ehPred){
ambiente->predadores->push_back( this ); ambiente->sitios[posInicial->x].nuPredadores++;
ambiente->nuPredadores++;
}else{
ambiente->presas->push_back( this); ambiente->sitios[posInicial->x].nuPresas++;
ambiente->nuPresas++;
}

}


Inseto::~Inseto(){
delete posicao;
}

/*#########################################################*/


Actually, i´m keeping track of the new Inseto object created, but i´m doing that inside the constructor of Inseto itself by adding "this" to the list "ambiente->presas" or "ambiente->predadores" . I know the idea is weird, but it works.. or used to work..

if you think knowing the class "Ambiente" will help, please let me know. thanks


Obs: I´m new here. How do you guys put your code inside a text area???
Quote:Original post by marciopd
Obs: I´m new here. How do you guys put your code inside a text area???


this is some normal text

 this is some text in [ code ] tags 

[ source ] this is some text in [ source ] tags [ /source ]


remove the spaces (aka [ whatever ] -> [whatever]) to use.
Quote:Original post by marciopd
Obs: I´m new here. How do you guys put your code inside a text area???


If you're new it's probably a good idea to read the faq (at the top right of the page).

This topic is closed to new replies.

Advertisement