IWETHEY v. 0.3.0 | TODO
1,095 registered users | 0 active users | 0 LpH | Statistics
Login | Create New User
IWETHEY Banner

Welcome to IWETHEY!

New 2 of the "magic 4" methods
Copy constructor:

LockFile(const LockFile&) {}

is generated by the compiler if you don't write it. The compiler version does a memberwise copy (calling copy ctors of all embedded objects). Consider this code:

\nclass LockFile\n{\npublic: \n    LockFile(int fileId) : _lock(primitiveCreateLockFileFunction(fileId)) {}\n    ~LockFile() { primitiveReleaseLock(_lock); }\nprivate: \n   int _lock;\n};\n\nfunc(LockFile lock) {}\n....\nLockFile lock(aFileId);\n\nfunc(lock);  // object is copied via the copy constructor - at end of function it is destroyed - releasing the lock\n


See the problem? A for the duration of func, there are two copies of the lock object - one passed to func, the other in the calling stack frame. They end up sharing the same lock and your lock gets free'd early. There are two fixes to this

1) Always pass lock by reference.
2) Make FileLock's copy ctor private so the compiler will prevent unintentional copying since you haven't made the object copy safe.

The second method is similar, but used in assignment and once again, the compiler will generate it for you if you don't.

You should always define the "big 4" methods and, if you can't make some of them safe, make them private so nothing can call them.




[link|http://www.blackbagops.net|Black Bag Operations Log]

[link|http://www.objectiveclips.com|Artificial Intelligence]

[link|http://www.badpage.info/seaside/html|Scrutinizer]
New Ahh, thanks
My original code used a new and passed the pointer around, but had a bunch of work for no reason. My new "simplified" code only did the single allocation, didn't copy anything around, so was bug free by accident.

I would have noticed the accidental destroy since I was printing a message when called, trying to understand it. But again, pure luck.

I've got the 3rd edition of the C++ Programming Language that I've been reading, but it is slow going.

And 'ctor'? That took a bit of googling to figure out it was shorthand for constructor. You must be used to it.
New If you are going to continue on this path
You really ought to get (and read) Scott Meyer's [link|http://www.amazon.com/Effective-C%2b%2b-Addison-Wesley-Professional-Computing/dp/0321334876/sr=8-1/qid=1157991837/ref=pd_bbs_1/104-0140589-9124705?ie=UTF8&s=books|Book] "Effective C++".

It explains all this stuff in nice easy to read bite sized chunks.




[link|http://www.blackbagops.net|Black Bag Operations Log]

[link|http://www.objectiveclips.com|Artificial Intelligence]

[link|http://www.badpage.info/seaside/html|Scrutinizer]
New Order request sent
Thanks

Note: One of my favorite Perl book is Effective Perl programming, which I believe is patterned after this book (or maybe the C one).
New Ordered from big river today.
     C++ destructer question - (broomberg) - (26)
         delete or free the resource? - (Yendor) - (2)
             Zip it, whipper-snapper -NT - (drewk)
             Yeah, but - (broomberg)
         <handwave>Smart Pointers and Exceptions</handwave> -NT - (altmann) - (5)
             These are not the droids you're looking for... -NT - (admin) - (4)
                 Um - (broomberg) - (3)
                     You can go about your business... move along... -NT - (admin) - (2)
                         You can go about your business... move along...move along - (altmann) - (1)
                             It'll be fine - (broomberg)
         Exceptions are key - (tuberculosis) - (16)
             Thanks -NT - (broomberg)
             How's this: - (broomberg) - (14)
                 A Mantra in C++ - Construction is Resource Acquisition - (tuberculosis) - (13)
                     Thanks - (broomberg) - (5)
                         2 of the "magic 4" methods - (tuberculosis) - (4)
                             Ahh, thanks - (broomberg) - (3)
                                 If you are going to continue on this path - (tuberculosis) - (2)
                                     Order request sent - (broomberg)
                                     Ordered from big river today. -NT - (broomberg)
                     Oh, and as far as using it for a sequence at a time, - (broomberg) - (1)
                         OK - but I would be likely to model seq as iterator -NT - (tuberculosis)
                     What. He. Said. - (jb4) - (4)
                         You can't properly hate something you don't really know -NT - (drewk) - (2)
                             Disagree. - (jb4) - (1)
                                 Disagree with your disagree. - (broomberg)
                         That's just the problem - (tuberculosis)

I'll be back on you like dumb on Dan Quayle.
85 ms