Problem with de-allocating memory

Hi guys,
I'm totally new to C++ and trying to program something using the Marmalade API (www.madewithmarmalade.com. The problem I'm facing is de-allocating the memory I've used at the end of the code doesn't seem to work... and no matter what I read/try I can't get it to work sadly :(

My code looks something like this:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
// Marmalade headers
#include "s3e.h"
#include "Iw2D.h"
#include "IwGx.h"
#include "vector"


int main()
{
	// Initialise Marmalade graphics system and Iw2D module
	Iw2DInit();

	//create array
	std::vector<CIw2DImage*> images;

	// Create an image for the app to use
	//CIw2DImage* image1 = Iw2DCreateImage("lol1.png");
	//CIw2DImage* image2 = Iw2DCreateImage("lol2.png");
	//CIw2DImage* image3 = Iw2DCreateImage("lol3.png");

	//Add actual images to the array 'images'
	images.push_back(Iw2DCreateImage("lol1.png"));
	images.push_back(Iw2DCreateImage("lol2.png"));
	images.push_back(Iw2DCreateImage("lol3.png"));

	// Main Game Loop
	while (!s3eDeviceCheckQuitRequest())
	{	
		//Clear the Screen
		Iw2DSurfaceClear(0xff000000);

		//Draw an image
		Iw2DDrawImage(images[IwRandMinMax(0, images.size())], CIwSVec2(10,10));

		// Show the surface
		Iw2DSurfaceShow();

		//yield to the system
		s3eDeviceYield(1);
	}

	//de-allocate pointers from vector array
	for(int i = 0; i < images.size(); ++i)
	delete images[i];

	// Shut down Marmalade graphics system and the Iw2D module
	Iw2DTerminate();
	
    return 0;
}


The Iw2D and s3e calls are in ref to the marmalade system.

Problem I'm facing is I'm getting this error:

1
2
3
4
5
6
7
8
IwAssert failure (MEMORY, 2356).
Message: Bucket 0(System) is still in use (first allocation ID=185); deleting it is dangerous

Callstack:
IwUtilTerminate
IwGxTerminate
Iw2DTerminate
_IwGxInitPipeline


From what I read this is a 'still in use' memory error and I haven't de-allocated properly so I suspect this is a general c++ coding error rather than a marmalade specific error.

So my question is... how am I supposed to properly remove the points from a vector array if not how I've done above? Any help would be greatly appreciated as I am absolutely a complete c++ noob... but I would like to learn more!
you use delete, but you never used new to allocate space...
@ Jikax
Thanks! Ok so when I call this:

std::vector<CIw2DImage*> images;

This doesn't automatically create the space it needs? I have to allocate space manually?

How much is needed for a vector, obviously images are being put 'into' this array so it's not a straight forward integer right?
obviously images are being put 'into' this array


actually, no... pointers to images are put into the array...and pointers take 4 bytes;)... But you don't have to worry about that...
The iwd2createimage allocated the space for you... Maybe there is a iwd2deleteimage or something...
Last edited on
The example on this page seems to pair 'Iw2DCreateImage' with 'delete'.

I think that's more or less what you do too, so it doesn't help much.

http://docs.madewithmarmalade.com/native/api_reference/api/ExampleIw2DImages.html
Hmmm... Indeed... It looks like your doing the right thing...
I found this: https://www.madewithmarmalade.com/devnet/forum/memory-issue-when-using-stl-vector-3

It looks like vectors and marmalade don't go well together...
Last edited on
Hmm.. this does not bode well.
delete does work when it's just a regular display image command but not with the array it seems.
Thanks for the replies guys...

Any suggestions as to what I could use otherwise?
My end result is to create something that will display a random image from a list of images.
I thought a vector array would be the best way to do this but perhaps not....
Unless the documentation specifically mentions that Iw2DDrawImage keeps pointers to the images that you pass and that you may not delete them before calling Iw2DTerminate(), this looks like a bug in that library. You probably should file a bug report.
@Chevril
That's awesome! Ok so I use CIwArray instead of vector, no problem. However I can no longer use Iw2DCreateImage with this it seems... how do I put images into the CIwArray?
I can no longer use Iw2DCreateImage
- is this from your experience, or something you read? I didn't notice it mentioned.
This was from my experience, I was getting errors until I changed to

CIwArray<void> images;

and now IW2D works, however I still get this error:

1
2
3
4
5
6
7
8
IwAssert failure (MEMORY, 2356).
Message: Bucket 0(System) is still in use (first allocation ID=185); deleting it is dangerous

Callstack:
IwUtilTerminate
IwGxTerminate
Iw2DTerminate
_IwGxInitPipeline



This is what my code looks like now that I've changed to CIwArray:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
// Marmalade headers
#include "s3e.h"
#include "Iw2D.h"
#include "IwGx.h"
#include "vector"


int main()
{
	// Initialise Marmalade graphics system and Iw2D module
	Iw2DInit();

	//create array
	CIwArray<void> images;

	// Create an image for the app to use
	//CIw2DImage* image1 = Iw2DCreateImage("lol1.png");
	//CIw2DImage* image2 = Iw2DCreateImage("lol2.png");
	//CIw2DImage* image3 = Iw2DCreateImage("lol3.png");

	//Add actual images to the array 'images'
	images.push_back(Iw2DCreateImage("lol1.png"));
	images.push_back(Iw2DCreateImage("lol2.png"));
	images.push_back(Iw2DCreateImage("lol3.png"));

	// Main Game Loop
	while (!s3eDeviceCheckQuitRequest())
	{	
		//Clear the Screen
		Iw2DSurfaceClear(0xff000000);

		//Draw an image
		Iw2DDrawImage(images[IwRandMinMax(0, images.size())], CIwSVec2(10,10));

		// Show the surface
		Iw2DSurfaceShow();

		//yield to the system
		s3eDeviceYield(1);
	}
	
	images.clear();

	// Shut down Marmalade graphics system and the Iw2D module
	Iw2DTerminate();
	
    return 0;
}
Last edited on
I noticed this comment on one of the pages: "Try to move the vector operations (push_back etc.) before Iw2DInit() or IwGxInit() functions."
http://www.madewithmarmalade.com/devnet/forum/memory-issue-when-using-stl-vector-3

I'm wondering whether you could first create the array, and add three (or however many you need) null pointers to it.

Then after calling Iw2DInit(); you could create your images. Copy your proper image pointers to replace the null values already in the array.

I'm guessing of course, as I don't have the files to test this myself.

Topic archived. No new replies allowed.