Sorry your browser is not supported!

You are using an outdated browser that does not support modern web technologies, in order to use this site please update to a new browser.

Browsers supported include Chrome, FireFox, Safari, Opera, Internet Explorer 10+ or Microsoft Edge.

AppGameKit Classic Chat / Tier 2 vectors of classes problem

Author
Message
kamac
13
Years of Service
User Offline
Joined: 30th Nov 2010
Location: Poland
Posted: 22nd May 2012 20:03
Hey there.

I am having some troubles with vector of classes..

To be exact, when using .push_back, there's called constructor of my class and just after it an destructor, making it almost instantly delete what it has created. Here's what do I mean:



Any way to fix this?

Thanks.

Follow me on twitter! @MotionStruct
Paul Johnston
TGC Developer
22
Years of Service
User Offline
Joined: 16th Nov 2002
Location: United Kingdom
Posted: 22nd May 2012 20:15
You could try



But you'll have to remember to delete the new objects when you remove them from the vector.
kamac
13
Years of Service
User Offline
Joined: 30th Nov 2010
Location: Poland
Posted: 22nd May 2012 20:19
It kind of works, but it still crashes on release mode on startup.
Yet no idea why.

Follow me on twitter! @MotionStruct
polomint
12
Years of Service
User Offline
Joined: 3rd Apr 2012
Location: Lancashire, United Kingdom
Posted: 22nd May 2012 20:22
I hate to interrupt this thread, but I've never used vectors in C++. What are they, and why would I use them? Are there certain uses that are made easier by using vectors?

Thanks,

Blackberry App Development & ZX Spectrum Game Development.
kamac
13
Years of Service
User Offline
Joined: 30th Nov 2010
Location: Poland
Posted: 22nd May 2012 20:25
Quote: "I hate to interrupt this thread, but I've never used vectors in C++. What are they, and why would I use them? Are there certain uses that are made easier by using vectors?

Thanks, "


They are useful when you need unknown amount of objects.
Useful when user is for example creating objects on the screen, then you have vector of those objects. Vectors are just as tables, but with
unlimited size.

Just google it

Yup. Keeping up my problem.

Quote: "It kind of works, but it still crashes on release mode on startup.
Yet no idea why."


Follow me on twitter! @MotionStruct
polomint
12
Years of Service
User Offline
Joined: 3rd Apr 2012
Location: Lancashire, United Kingdom
Posted: 22nd May 2012 20:32
Thanks, found a decent description here >> http://www.cplusplus.com/reference/stl/vector/

Blackberry App Development & ZX Spectrum Game Development.
kamac
13
Years of Service
User Offline
Joined: 30th Nov 2010
Location: Poland
Posted: 22nd May 2012 20:42
I know that it crashes just after constructor now. It doesn't reach destructor it seems.

Follow me on twitter! @MotionStruct
Ancient Lady
Valued Member
20
Years of Service
User Offline
Joined: 17th Mar 2004
Location: Anchorage, Alaska, USA
Posted: 22nd May 2012 21:11
Try this:


It's possible that the compiler is being uber-picky about type, 'int' is not the same as 'UINT', which is what LoadImage and CreateSprite return and what CreateSprite looks for as an input type.

Internally, an int and a UINT are stored somewhat differently. The int type will have the highest order bit as a sign bit. UINT does not.

Cheers,
Ancient Lady
kamac
13
Years of Service
User Offline
Joined: 30th Nov 2010
Location: Poland
Posted: 22nd May 2012 21:57 Edited at: 22nd May 2012 21:58
Bad news, it still crashes.

Maybe more code will help?

Full class:

event.h



event.cpp




inside template.h



Inside void CreateEntities called by template.cpp



As you can see, there's App.map_event.push_back.

I'll add that I have another class, almost the same as event.cpp and it doesn't crash, so it's 100% that it's something wrong with the vector.

Follow me on twitter! @MotionStruct
Ancient Lady
Valued Member
20
Years of Service
User Offline
Joined: 17th Mar 2004
Location: Anchorage, Alaska, USA
Posted: 22nd May 2012 22:06
You probably need to change your constructor definition:


And this


Being completely consistent in types is very important in Tier2 (C++) programming. Tier1 (Basic) is much more forgiving.

Cheers,
Ancient Lady
kamac
13
Years of Service
User Offline
Joined: 30th Nov 2010
Location: Poland
Posted: 22nd May 2012 22:10
Nope. Still won't work. Have I mentioned it runs all fine under debug mode?

Follow me on twitter! @MotionStruct
Ancient Lady
Valued Member
20
Years of Service
User Offline
Joined: 17th Mar 2004
Location: Anchorage, Alaska, USA
Posted: 22nd May 2012 22:49
First, I am assuming you are working in Windows and using MS Visual C++ (probably Express?).

Are you able to determine where in the constructor it is dying?

Cheers,
Ancient Lady
kamac
13
Years of Service
User Offline
Joined: 30th Nov 2010
Location: Poland
Posted: 22nd May 2012 23:04
Quote: "First, I am assuming you are working in Windows and using MS Visual C++ (probably Express?)."


Yes.

Quote: "Are you able to determine where in the constructor it is dying?

"


Nowhere. I doesn't even die at the end. It dies after the constructor, but before new line (after push_back)

Follow me on twitter! @MotionStruct
Ancient Lady
Valued Member
20
Years of Service
User Offline
Joined: 17th Mar 2004
Location: Anchorage, Alaska, USA
Posted: 22nd May 2012 23:36
Are you sure it is getting out of the constructor without errors?

This code should have a check to make sure a valid value is returned:


Just in case your image file does not exist where it is looking for it.

I'm also curious about the file name. It has no extension and consists only of a number between 1 and 69.

Cheers,
Ancient Lady
kamac
13
Years of Service
User Offline
Joined: 30th Nov 2010
Location: Poland
Posted: 22nd May 2012 23:49 Edited at: 22nd May 2012 23:58
Quote: "I'm also curious about the file name. It has no extension and consists only of a number between 1 and 69."


Yeah, that's how my subimages look like.



Quote: "Just in case your image file does not exist where it is looking for it."


Checked. Everything's valid.


@NEWS

It crashes somewhere in this loop:



which is after push_back. So it's not vector?


@NEWS


I was wrong, it crashes as I said.

Follow me on twitter! @MotionStruct
Ancient Lady
Valued Member
20
Years of Service
User Offline
Joined: 17th Mar 2004
Location: Anchorage, Alaska, USA
Posted: 23rd May 2012 00:10
By any chance, is the directory entered for Configuration Properties->General->Output Directory different between your Debug and Release configurations? If so, the release version might not be able to find your images.

Specifically with regard to checking values, what happens if you add a check that shows you the value returned in this line when built with the release configuration:


What are the dimensions of the parent image? Some discussion seems to indicate that it should be powers of two along each side if you are going to use texture atlases.

I've not worked with SubImages, only with the custom font files (which use an image atlas), I'm just going on basic programming stuff.

Cheers,
Ancient Lady
kamac
13
Years of Service
User Offline
Joined: 30th Nov 2010
Location: Poland
Posted: 23rd May 2012 00:20 Edited at: 23rd May 2012 00:21
Quote: "By any chance, is the directory entered for Configuration Properties->General->Output Directory different between your Debug and Release configurations? If so, the release version might not be able to find your images.
"


They're the same.

Quote: "Specifically with regard to checking values, what happens if you add a check that shows you the value returned in this line when built with the release configuration:"


It returns 10495.

Quote: "What are the dimensions of the parent image? Some discussion seems to indicate that it should be powers of two along each side if you are going to use texture atlases."


288x256 But as I said before, I have another working sprite that uses this image..

I'll try to size it up to 512x512 and see if it works.

@EDIT

Changed to 512x512. Still won't work.

Follow me on twitter! @MotionStruct
Ancient Lady
Valued Member
20
Years of Service
User Offline
Joined: 17th Mar 2004
Location: Anchorage, Alaska, USA
Posted: 23rd May 2012 00:42
I'm pretty stumped.

If you want to zip up your project and send it to me, I'll see what I can do. And I absolutely won't share it or use it in any way for myself. Promise.

Cheers,
Ancient Lady
JimHawkins
15
Years of Service
User Offline
Joined: 26th Jul 2009
Location: Hull - UK
Posted: 23rd May 2012 01:25
I'm probably talking cobblers, but should it be:

for(int x=0; x<map_w; x++)

actually be:

for(int x=0; x<(map_w -1); x++)

because you are incrementing x possibly beyond the array bounds.

-- Jim
3d point in space
15
Years of Service
User Offline
Joined: 30th Jun 2009
Location: Idaho
Posted: 23rd May 2012 06:30 Edited at: 23rd May 2012 07:58
i think you have to cast it as a template if your using a class, because I don't think you can do that with classes only structs.

I forgot the syntax to cast it though. I think its <template
> or somthing like that.

another thing you can do is use a struct outside the class then inside the class i would use the struct. Do a google search on template class vectors,

Ok this is not the problem I read further. Down the post some how you are going off the vector so I would set a default width and hieght of the vector then right data inside. I useually do this so that all the row and collumns of the vector are same width-height-depth. Therefore eliminating using push_back of vectors. I usually get errors using vectors in your format.

Developer of Space Chips, pianobasic, zipzapzoom, and vet pinball apps.
Developed the tiled map engine seen on the showcase.
Veterian for the military.
kamac
13
Years of Service
User Offline
Joined: 30th Nov 2010
Location: Poland
Posted: 23rd May 2012 15:17
Quote: "I'm probably talking cobblers, but should it be:

for(int x=0; x<map_w; x++)

actually be:

for(int x=0; x<(map_w -1); x++)

because you are incrementing x possibly beyond the array bounds."


x<map_w will actually reach to map_w-1 only.

Quote: "Ok this is not the problem I read further. Down the post some how you are going off the vector so I would set a default width and hieght of the vector then right data inside. I useually do this so that all the row and collumns of the vector are same width-height-depth. Therefore eliminating using push_back of vectors. I usually get errors using vectors in your format."


Problem is that I have to create my events dynamically, so there cannot be fixed vector size.


Quote: "If you want to zip up your project and send it to me, I'll see what I can do. And I absolutely won't share it or use it in any way for myself. Promise."


I guess that I will have to do that if there is no other solution.

Follow me on twitter! @MotionStruct
Ancient Lady
Valued Member
20
Years of Service
User Offline
Joined: 17th Mar 2004
Location: Anchorage, Alaska, USA
Posted: 23rd May 2012 15:40 Edited at: 23rd May 2012 15:40
I don't promise that I can solve it. But having the whole project lets me look at it more closely.

Once, way back in my beginnings, I sat in the help office and helped all the other students who couldn't get their programs (on punch cards) working. And I've learned a few things since then.

If you send it (using the email link below my post), I'll see what I can do for you. It will need to include all your graphics as well (which I will protect).

Cheers,
Ancient Lady
JimHawkins
15
Years of Service
User Offline
Joined: 26th Jul 2009
Location: Hull - UK
Posted: 23rd May 2012 15:53
Quote: "x<map_w will actually reach to map_w-1 only. "


I don't think so. If x is smaller than map_w the loop increments it, and you then use that value. Is the map 0-based or 1-based?

-- Jim
Ancient Lady
Valued Member
20
Years of Service
User Offline
Joined: 17th Mar 2004
Location: Anchorage, Alaska, USA
Posted: 23rd May 2012 15:57 Edited at: 23rd May 2012 16:00
In C++, all arrays are zero based. Eg. "int xyz[10];" is indexed from 0 to 9 exactly. Unlike Tier1 where "dim xyz[10]" allows indices from 0 to 10.

In a C++ if loop, it stops when the indicated condition ("x<map_w") is no longer true and does not execute the contents of the loop.

Cheers,
Ancient Lady
kamac
13
Years of Service
User Offline
Joined: 30th Nov 2010
Location: Poland
Posted: 23rd May 2012 15:58 Edited at: 23rd May 2012 16:05
Quote: "Is the map 0-based or 1-based?"


Not sure what do you mean here.


@EDIT

Worth of adding:

It still crashes on push_back. Even if I make my class' constructor and destructor empty.

Follow me on twitter! @MotionStruct
3d point in space
15
Years of Service
User Offline
Joined: 30th Jun 2009
Location: Idaho
Posted: 23rd May 2012 16:09
0 base starts at 0 while 1 base starts at 1.
must be a school project if you can't use static vectors. Your teacher knows way more then mine. He just wanted the program too work.

Developer of Space Chips, pianobasic, zipzapzoom, and vet pinball apps.
Developed the tiled map engine seen on the showcase.
Veterian for the military.
kamac
13
Years of Service
User Offline
Joined: 30th Nov 2010
Location: Poland
Posted: 23rd May 2012 16:11
Quote: "0 base starts at 0 while 1 base starts at 1."


Oh, well, it starts at 0.

Quote: "must be a school project if you can't use static vectors."


I can, but I don't want. (And it's not a school project)
I don't want to do that because I am going to have undefined amount of elements inside my vector, and that's because the user does some changes manually.

Follow me on twitter! @MotionStruct
Ancient Lady
Valued Member
20
Years of Service
User Offline
Joined: 17th Mar 2004
Location: Anchorage, Alaska, USA
Posted: 23rd May 2012 16:17
Quote: "It still crashes on push_back. Even if I make my class' constructor and destructor empty."


Just for giggles, try this:


Let's make sure that something is actually getting created.

Although, in theory, the push_back should be okay with a null value. It just wants a pointer type value.

Cheers,
Ancient Lady
kamac
13
Years of Service
User Offline
Joined: 30th Nov 2010
Location: Poland
Posted: 23rd May 2012 16:24
Ancient Lady, tested and I see that newEvnt is created.



Follow me on twitter! @MotionStruct
Ancient Lady
Valued Member
20
Years of Service
User Offline
Joined: 17th Mar 2004
Location: Anchorage, Alaska, USA
Posted: 23rd May 2012 16:33
Okay. I'm just looking for things based on what I can see.

Cheers,
Ancient Lady
Ancient Lady
Valued Member
20
Years of Service
User Offline
Joined: 17th Mar 2004
Location: Anchorage, Alaska, USA
Posted: 23rd May 2012 16:35
Okay. Will you show more of your template.h file?

I just got suspicious of this line:


I'd like to see the full context of where map_event is defined.

Cheers,
Ancient Lady
kamac
13
Years of Service
User Offline
Joined: 30th Nov 2010
Location: Poland
Posted: 23rd May 2012 16:58
Template.h:




Follow me on twitter! @MotionStruct
Ancient Lady
Valued Member
20
Years of Service
User Offline
Joined: 17th Mar 2004
Location: Anchorage, Alaska, USA
Posted: 23rd May 2012 17:26
Okay, it looks good. I'm going to try a couple of tests on a simple project and see what I can come up with.

Cheers,
Ancient Lady
kamac
13
Years of Service
User Offline
Joined: 30th Nov 2010
Location: Poland
Posted: 23rd May 2012 17:36
Sure, thanks

Follow me on twitter! @MotionStruct
Ancient Lady
Valued Member
20
Years of Service
User Offline
Joined: 17th Mar 2004
Location: Anchorage, Alaska, USA
Posted: 23rd May 2012 18:35
Okay, starting from basic template:
template.h:


template.cpp:


Based on this and what I've seen of your code. I don't see why you are having a problem.

Note that I didn't use 'App.my_vec', just 'my_vec'. There is no need to specify the global App object for your 'map_event' vector. Because 'my_vec' and 'map_event' are attributes of the app class, they exist within any instantiation of it.

I have been assuming that all the setup code you initially showed in template.cpp is in the 'app::Begin' function.

Cheers,
Ancient Lady
kamac
13
Years of Service
User Offline
Joined: 30th Nov 2010
Location: Poland
Posted: 23rd May 2012 18:43
Quote: "Note that I didn't use 'App.my_vec', just 'my_vec'. There is no need to specify the global App object for your 'map_event' vector. Because 'my_vec' and 'map_event' are attributes of the app class, they exist within any instantiation of it.

I have been assuming that all the setup code you initially showed in template.cpp is in the 'app::Begin' function."



Well, actually some code I've showed was from
void CreateEntities(void)

that's why I have to use App.

What's important is - I have also placed push_back inside app::Begin to check if it crashes, and it does..

I must analyse some of your code.

Follow me on twitter! @MotionStruct
kamac
13
Years of Service
User Offline
Joined: 30th Nov 2010
Location: Poland
Posted: 23rd May 2012 18:58
I've used your code Ancient Lady and it crashes.. Uh..

Follow me on twitter! @MotionStruct
Paul Johnston
TGC Developer
22
Years of Service
User Offline
Joined: 16th Nov 2002
Location: United Kingdom
Posted: 23rd May 2012 19:01
Could it be
in the app constructor clearing out something in the vector variable that it doesn't like?
kamac
13
Years of Service
User Offline
Joined: 30th Nov 2010
Location: Poland
Posted: 23rd May 2012 19:04 Edited at: 23rd May 2012 19:05
Paul Johnston, you're some sort of god?

It works now!

Thanks so much !

Thanks to you too Ancient Lady

Follow me on twitter! @MotionStruct
Ancient Lady
Valued Member
20
Years of Service
User Offline
Joined: 17th Mar 2004
Location: Anchorage, Alaska, USA
Posted: 23rd May 2012 19:13


This is part of the standard template and shouldn't cause a problem. And doesn't cause a problem as far as I can see.

I forgot to remove one 'App.' in the code I posted, but it still worked when I built it with that code in.

The program I posted worked fine without a crash in a Release build.

I just tried a Debug build and it crashed. And it showed up a bunch of link warnings.

Okay. Let's go see why this is happening.


Cheers,
Ancient Lady
Ancient Lady
Valued Member
20
Years of Service
User Offline
Joined: 17th Mar 2004
Location: Anchorage, Alaska, USA
Posted: 23rd May 2012 20:35 Edited at: 23rd May 2012 21:34
Edit: I discovered and fixed the issue (see other post)

Cheers,
Ancient Lady
Ancient Lady
Valued Member
20
Years of Service
User Offline
Joined: 17th Mar 2004
Location: Anchorage, Alaska, USA
Posted: 23rd May 2012 20:43 Edited at: 23rd May 2012 21:33
Edit: Never mind

Cheers,
Ancient Lady
Ancient Lady
Valued Member
20
Years of Service
User Offline
Joined: 17th Mar 2004
Location: Anchorage, Alaska, USA
Posted: 23rd May 2012 21:07
Ignore my posts (should they show up) with the attachment.

I just saw Kamac's response to Paul (which I somehow missed when I posted right after).

I removed the default 'app' constructor behavior and all problems disappeared.

Strange code to put as default in a template?

Cheers,
Ancient Lady
Neuro Fuzzy
17
Years of Service
User Offline
Joined: 11th Jun 2007
Location:
Posted: 23rd May 2012 21:17
Just posting a tip: it's bad practice to delete a passed in image like you're doing in your original snippet


because then something like this would crash:

even though it looks fine. (because it deletes img twice)

If you're using it by saying 'main_event.push_back(new Event(agk::LoadImage("abc.png"),1,1));' then maybe you should shorten the constructor to 'Event(const char *image, int x, int y)', so you can just create 'new Event("abc.png",1,1)'.

Login to post a reply

Server time is: 2024-11-23 14:11:50
Your offset time is: 2024-11-23 14:11:50