0

I am trying to make a simple object pool and i cant get it to work . My code crashes when using malloc to allocate space for a dynamic array of template's type

my code:

template <class cl>
class ObjectPool{
public:
    gltAtlasRenderer* br;
    cl* items;
    int SIZE,texid;

    void Innit(int size,int texidi)
    {
        SIZE=size;
        items=(cl*)malloc(sizeof(cl)*SIZE);->>>>>>>>>>HERE MY APP CRASHES
        /*br=new gltAtlasRenderer(SIZE,false,1);
        texid=texidi;
        for(int c=0;c<SIZE;c++)items[c].alive=false;
        */
    }
    void Update(Arena*  arena)
    {
        for(int c=0;c<SIZE;c++)
        {
            if(!items[c].alive)continue;
            items[c].Update(arena);
        }
    }
    void Render()
    {
        br->RenderStart();
        for(int c=0;c<SIZE;c++)
        {
            if(!items[c].alive)continue;
            items[c].Render(br);
        }
        br->RenderStop(texid);
    }
};

All the examples that i found in the web are using fixed arrays and vectors ,but i need the array to be allocated at runtime.

EDIT: This is how i create it:

ObjectPool<Enemie1>* enemies1;
void Innit()
{
         enemies1->Innit(size,texid);
}

I know that is crashing in that line cause if i commend it it doesnt crashes

Deduplicator
  • 44,692
  • 7
  • 66
  • 118
SteveL
  • 3,331
  • 4
  • 32
  • 57
  • How do you instantiate your template? How do you know it crashes at that exact point? You did not provide enough information for us to save your example to file, compile it, run it, and observe the exact behaviour you are desribing. http://sscce.org – DevSolar Aug 16 '12 at 13:32
  • While this is possible, it is extremely unlikely. Please provide the output you get when you program crashes. If you are using Linux, also provide output from `valgrind`. – Šimon Tóth Aug 16 '12 at 13:32
  • Is there any particular reason you're not using a vector or a list? – Cubic Aug 16 '12 at 13:33
  • 5
    `malloc()` will not invoke the constructor for a `class`: use `new[]`. – hmjd Aug 16 '12 at 13:34
  • 2
    Vectors are allocated at runtime. – olivecoder Aug 16 '12 at 13:36
  • 5
    Just use `std::vector` to store your object instances in; it is allocated at runtime and may be resized at will (so you can add/remove instances if necessary). Life will be much easier if you avoid manual memory management and pointer manipulation where it isn't strictly necessary. – Rook Aug 16 '12 at 13:37
  • 1
    @SteveL: 1) See hmjd's comment. 2) Misspellings of identifiers *are* bugs. Innit, texidi, Enemie1 all point to some problems with your grasp of the English language that might be harmles in the context of your problem here, but *should* be addressed, because they *will* make trouble later on. – DevSolar Aug 16 '12 at 13:39
  • 3
    @ahenderson, it's a C-- program. – TheMathemagician Aug 16 '12 at 13:40
  • 1
    Incidentally, you might also consider avoiding the two-step construct-initialise process. Get rid of your `Innit` method (which would appear to be Sarf London style C++) and roll its functionality into a constructor for your `ObjectPool` class. RAII is a good pattern to follow. – Rook Aug 16 '12 at 13:58
  • @Rook: "Sarf London style C++"? I don't get that reference, but it makes me curious... – DevSolar Aug 16 '12 at 14:39

2 Answers2

4

This will not work:

ObjectPool<Enemie1>* enemies1;
void Innit()
{
         enemies1->Innit(size,texid);
}

You need to allocate the memory before using it, like this:

ObjectPool<Enemie1>* enemies1;
void Innit()
{
         enemies1 = new ObjectPool<Enemie1>;
         enemies1->Innit(size,texid);
}
Šimon Tóth
  • 35,456
  • 20
  • 106
  • 151
  • Thank you this fixed the problem.I forgot that since i am creating the pool as a pointer i need to allocate it manually too :) – SteveL Aug 16 '12 at 13:46
0
ObjectPool<Enemie1>* enemies1; //This is uninitialized
void Innit()
{
     enemies1->Innit(size,texid); //This calls a method on an uninitialized pointer
}

In the Innit method when you assign the result of malloc to one of the members you are assigning through an invalid pointer which causes a crash.

Change it to this.

ObjectPool<Enemie1> enemies1;
void Innit()
{
     enemies1.Innit(size,texid);
}
Dirk Holsopple
  • 8,731
  • 1
  • 24
  • 37