Jump to content
  • Advertisement
Sign in to follow this  
marciopd

c++ access violation - trouble with new

This topic is 5416 days old which is more than the 365 day threshold we allow for new replies. Please post a new topic.

If you intended to correct an error in the post then please contact us.

Recommended Posts

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]

Share this post


Link to post
Share on other sites
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 array
for (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

Share this post


Link to post
Share on other sites
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???

Share this post


Link to post
Share on other sites
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

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

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


remove the spaces (aka [ whatever ] -> [whatever]) to use.

Share this post


Link to post
Share on other sites
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).

Share this post


Link to post
Share on other sites
Sign in to follow this  

  • Advertisement
×

Important Information

By using GameDev.net, you agree to our community Guidelines, Terms of Use, and Privacy Policy.

GameDev.net is your game development community. Create an account for your GameDev Portfolio and participate in the largest developer community in the games industry.

Sign me up!