C++ smart pointer usage

Started by
21 comments, last by Misantes 9 years, 9 months ago

Huh, I tried that earlier and it didn't work. I just tried again and it does tongue.png Go figure smile.png

Perhaps I wasn't doing it program-wide or something.

But, it's working now! Thanks a ton for your help and patience smile.png

(for anyone's reference who sees this later, -std=c++1y, is the one that worked for me)

Edit*

so, on the advice of everyone here, I got both a version working where I don't use pointers at all, and a version where a vector of unique pointers is used, where the vector is filled by emplace_back with make_unique, and the functions just take a reference of the vector.

Performance seems like a wash between the two, though I'm getting some weird bugs when not using the pointers. Like, I can't move forward at all, unless I go up a ways in the game first, and some weird things with the UI. I'll try to parse those out, as Alvaro's method seems like it makes the most sense, and unless I can find a good reason why I ought to be using pointers for those, it appears he may be correct. Figuring out what is causing those bugs will likely determine which version I use, but I truly appreciate everyone's help here smile.png

Here's the pointer version.

Here's the non-pointer version.

Ignore all the other problems. I fixed them once, but then when implementing this stuff, I reverted to an older copy, and just haven't fixed the hard-coding, mixed use casing, etc, etc tongue.png

Edit*

This is a little off-topic, but semi related. A problem with my version that doesn't use pointers, is the first 4095 objects in my vector all seem to be at location 0,0,0 in world coordinates (they don't show in game, but report their location there). If I shrink the vector size enough, then the first 2047, or 1023, or 511, etc, etc all show their location there. Any object after that in the vector show up fine. That number makes me feel like it's some sort of size or memory limit or something along those lines for the vector. Or, I'm doing something wrong in creating them with emplace_back. The same problem doesn't exist with the pointer version, and they're largely the same code, minus the pointers.

A quick read over emplace_back shows that it reallocates memory if it goes over the max size. But, I show a capacity of 2048 or whathaveyou, and a huuuuge max_size that I'm absolutely not hitting.

But, If anyone has an insight to that, I'd be open to hearing it smile.png

Edit*

After some digging, it looks like the vector gets resized as I add elements. I imagine that is where the problem may be stemming from. Calling "vector.reserve(amountToReserve)" beforehand seems to fix the problem. But, I'd still like to know what's really going on. As, eventually, I'm going to want to create a dynamic vector where I don't know the size beforehand :P

Beginner here <- please take any opinions with grain of salt

Advertisement
I didn't look at your code, but it sounds like your objects don't have the right semantics to be copied or moved around.

EDIT: This looks very suspicious:
Object::Object(const Object& orig) {
}
How is that copy constructor supposed to make a copy of anything?

Removing that does indeed fix the issue. Now, for the life of me I'm trying to remember why I included that to begin with. I think when I first started learning OpenGL I was still using netbeans, and I built up a little template to open a window a render a couple objects. later, I moved to code::blocks, and I think I just copied the template over. But, I have a feeling netbeans includes that automatically when making a new class. I generally delete it, but for whatever reason had left it in, and just got so used to it being there it went unnoticed until now >.< How embarrassing.

But, thanks yet again Alvaro, you've truly been helpful. :)

Beginner here <- please take any opinions with grain of salt

This topic is closed to new replies.

Advertisement