C++ Memory Issues

Discuss the development of new homebrew software, tools and libraries.

Moderators: cheriff, TyRaNiD

Post Reply
yotryu
Posts: 6
Joined: Tue Sep 30, 2008 10:30 am

C++ Memory Issues

Post by yotryu »

Hey all,
I've been slowly writing a small library for development on PSP (I'm trying to adapt my existing engine which I am very familiar with to the PSP), however it seems there's something I'm missing with c++ and class memory usage.
Basically I've written a bunch of classes (there's only around 5-10) which I have written implementations for all but 1 (1 is also partially done). My problem is as soon as I include even just the header for the classes (implementations are in a .cpp file that is included at the end of the header), my PSP's available memory drops from around 23000KB to around 1000KB...
It stays consistent at the same size every time I run the program (all it does is setup callbacks and show available memory in debug screen) no matter how much is in the header/cpp file of mine - I have tried cutting out all but 2 classes and compiling and the usage is the same.
There are some variables declared, but even removing them doesn't change the usage :S

I've tried a few things that have done absolutely nothing to change the memory usage so I wanted to ask for a bit of help on the matter.
For reference, a typical class of mine looks like this:

Code: Select all

class SpriteInstLE : public BaseLE {
friend class ParticleLE;

private:
  SpriteLE *basesprite;             // Pointer to base sprite
  SPRITEANIMDATA animation; // Animation struct
  unsigned short colour[4];      // Sprite colour
  VECTOR3 position;                // 3D point struct
  VECTOR3 velocity;
  VECTOR3 acceleration;

public:
  // Member functions are declared but not implemented here
};
Any advice on this issue would be much appreciated.
Thanks,
yotryu
User avatar
Torch
Posts: 825
Joined: Wed May 28, 2008 2:50 am

Post by Torch »

If you use malloc then it will allocate a chunk of memory (defined by the PSP_HEAP_SIZE_*) on the first call. PSPSDK's libc will allocate further from this heap. But as far as the firmware is concerned, a whole block has been allocated and it will show only the remaining space as free, but you can continue to malloc till the heap is used up.

If you want to read the free space accurately then only use the sceKernelAlloc functions.
yotryu
Posts: 6
Joined: Tue Sep 30, 2008 10:30 am

Post by yotryu »

Thanks for the info Torch. I checked out malloc with PSPSDK and tried using PSP_HEAP_SIZE_KB(1024) to limit the allocated block so that (I thought) I could free up all the memory that was being taken up by it, but it doesn't seem to have worked. Although it does tell me now that I have more free memory, I still seem to be running out somehow under the same circumstances...
I thought class declarations shouldn't take up much space in memory (if any) once the game starts so I'm very confused as to why I can only create about 8 objects of 96 bytes in size (as told by sizeof()) before I get a crash.
Am I missing something with my included files? Are they likely to be including variables that aren't malloc-ed so I don't see they are using memory? (I myself don't use malloc as I am used to working in c++ so use new/delete, but I have only 10-20 variables globally that shouldn't take up that much memory). I have a fair few includes:

Code: Select all

#include <pspsdk.h>
#include <pspkernel.h>
#include <pspmoduleinfo.h>
#include <psprtc.h>
#include <pspctrl.h>
#include <pspdisplay.h>
#include <graphics.h>
#include <pspdebug.h>
#include <pspgu.h>
#include <pspgum.h>
#include <pspaudio.h>
#include <pspaudiolib.h>
#include <Gamekit/Audio.h>

#include "../common/callbacks.h"
#include "../common/vram.h"

#include <iostream>
#include <fstream>
#include <stdlib.h>
#include <stdio.h>
#include <algorithm>
#include <math.h>
#include <string>
#include <list>
#include <vector>
#include <time.h>
That is the order they are included in too if that makes any difference.
Am I missing something here or should I consider using objective C for PSP?

Any advice is greatly appreciated,
yotryu
User avatar
Torch
Posts: 825
Joined: Wed May 28, 2008 2:50 am

Post by Torch »

Then you might be creating a heap with the sce heap function somewhere if you have set thd pspsdk heap to 1024. Or an accidental large allocation cos youve got a ton of files.
Post Reply