Author Topic: My first steps with C++  (Read 48949 times)

0 Members and 1 Guest are viewing this topic.

Offline Mike Stefanik

  • Full Member
  • ***
  • Posts: 126
    • Catalyst Development
Re: My first steps with C++
« Reply #75 on: October 27, 2013, 06:56:54 PM »
I know I *always* get shot down when I mention this, but XP is going end of like in April 2014.  I'm sure there will be a lot of people try to keep using it, but without security updates, it would not be wise.

That's generally the case, but not entirely accurate. Microsoft has left the door open that they may issue hotfixes for what they described as "very critical security flaws", but aside from that, after April 8th, 2014 they will only be providing updates for corporate customers with custom support plans (typically those are support agreements that a company has with Microsoft for their fleet of PCs, where they are charged a certain amount per PC, per year with the costs increasing for each year after the product has reached its end-of-life).

And just so folks are aware, all things XP will not be going dark in April. Windows Update will continue to work, and you'll continue to be able to install and activate XP on new systems. It's just that there won't be any new updates published, normally. I suspect that if there was some particularly nasty zero-day vulnerability that shows up, Microsoft might feel compelled to push out a security update for XP, but you can't count on it.

I know when we've talked to our customers, a lot of them plan on continuing to support XP beyond it's end-of-life and that means we will need to continue to support them. I'm sure it will reach a point where Windows XP becomes like Windows 2000, but with still over 30% marketshare, XP will continue to be around for a while. My concern is with malware that is being kept on the shelf right now because it exploits an as-of-yet undiscovered flaw in XP and they only want to deploy it after April, 2014 for maximum effect.

« Last Edit: October 27, 2013, 06:59:41 PM by Mike Stefanik »
Mike Stefanik
sockettools.com

Offline Jim Dunn

  • Newbie
  • *
  • Posts: 40
Re: My first steps with C++
« Reply #76 on: October 27, 2013, 10:19:38 PM »
Mike, thanks for your reply, well said, and... thanks for going easy on me.

Online Patrice Terrier

  • Global Moderator
  • Hero Member
  • *****
  • Posts: 2009
    • www.zapsolution.com
Problem allocating dynamic memory in a C++ DLL
« Reply #77 on: October 28, 2013, 11:01:33 AM »
I have been faced with a nasty problem while allocating dynamic memory from inside of a DLL.

I am using this to allocate the memory inside of the DLL
BYTE* lpArray = (BYTE*) malloc(Xin * Yin * 4);
then i return the BYTE* to be used outside of the DLL, within the calling EXE,
and after i am done with it, i am freeing back the memory using this:
free (lpArray);

And bim, bam, badaboum, i am throwing up an exception error as soon as free.c is performing
        retval = HeapFree(_crtheap, 0, pBlock);
        if (retval == 0)
        {
            errno = _get_errno_from_oserr(GetLastError());
        }


I have never been faced with this problem with PB (using REDIM to allocate dynamic memory), thus i was taken by surprise by that one.

I have found this article: http://msdn.microsoft.com/en-us/library/ms235460%28v=vs.110%29.aspx
that seems to match this case.
« Last Edit: October 28, 2013, 01:35:16 PM by Patrice Terrier »
Patrice Terrier
GDImage (advanced graphic addon)
http://www.zapsolution.com

Offline James C. Fuller

  • Global Moderator
  • Hero Member
  • *****
  • Posts: 674
Re: My first steps with C++
« Reply #78 on: October 28, 2013, 03:45:10 PM »
Patrice,
It appears you should be using free as you use malloc to allocate the memory?

James


Online Patrice Terrier

  • Global Moderator
  • Hero Member
  • *****
  • Posts: 2009
    • www.zapsolution.com
Re: My first steps with C++
« Reply #79 on: October 28, 2013, 04:04:00 PM »
James--

Yes, I am using free already, what i have posted is the debug code that shows, that the matching C code that does the work under the hood at the time we use free, is causing the havoc.

I know how to solve the problem (allocating memory in the calling application, rather than in the DLL), but it is just to warn those who may allocate dynamic memory from within a DLL, especially when using the "Multithread /MT" flag option with another DLL or main application using a different setting.
Patrice Terrier
GDImage (advanced graphic addon)
http://www.zapsolution.com

Offline Mike Stefanik

  • Full Member
  • ***
  • Posts: 126
    • Catalyst Development
Re: My first steps with C++
« Reply #80 on: October 28, 2013, 05:16:24 PM »
You should never use malloc or new to allocate memory from within a DLL and then free that memory in an external function (i.e.: in the executable or another DLL). If you want to allocate a block of memory that will be released by another function in your executable, then you want to use HeapAlloc:

Code: [Select]
LPBYTE lpArray = (LPBYTE)HeapAlloc(GetProcessHeap(), HEAP_ZERO_MEMORY, (Xin * Yin * 4));

And you would free that memory by calling:

Code: [Select]
HeapFree(GetProcessHeap(), 0, lpArray);

Another option would be to provide your own function that explicitly frees the memory with your DLL. The reason is that when you statically link to the CRT, your DLL has its own instance of the runtime heap which is different than the heap that is allocated for your executable.
Mike Stefanik
sockettools.com

Online Patrice Terrier

  • Global Moderator
  • Hero Member
  • *****
  • Posts: 2009
    • www.zapsolution.com
Re: My first steps with C++
« Reply #81 on: October 28, 2013, 05:27:54 PM »
I thougth that C++ malloc was an encapsulation for HeapAlloc, my mistake.

Thanks Mike!
Patrice Terrier
GDImage (advanced graphic addon)
http://www.zapsolution.com

Offline Mike Stefanik

  • Full Member
  • ***
  • Posts: 126
    • Catalyst Development
Re: My first steps with C++
« Reply #82 on: October 28, 2013, 05:47:14 PM »
Both new and malloc do eventually call HeapAlloc, but the runtime allocates it's own heap rather than using the default process heap. It also does things a bit differently if you have a debug build to make it easier for it to track allocations, etc. Note that this also impacts allocations that go the other way (i.e.: your DLL should never attempt to free a block of memory that has been allocated by your main executable using malloc or new).
Mike Stefanik
sockettools.com

Online Patrice Terrier

  • Global Moderator
  • Hero Member
  • *****
  • Posts: 2009
    • www.zapsolution.com
Re: My first steps with C++
« Reply #83 on: October 28, 2013, 05:57:14 PM »
Mike--

Quote
Actually, malloc() (and other C runtime heap functions) are module dependant, which means that if you call malloc() in code from one module (i.e. a DLL), then you should call free() within code of the same module or you could suffer some pretty bad heap corruption (and this has been well documented). Using HeapAlloc() with GetProcessHeap() instead of malloc(), including overloading new and delete operators to make use of such, allow you to pass dynamically allocated objects between modules and not have to worry about memory corruption if memory is allocated in code of one module and freed in code of another module once the pointer to a block of memory has been passed across to an external module.

I found it there: http://stackoverflow.com/questions/8224347/malloc-vs-heapalloc

a confirmation of your previous post, thanks!
« Last Edit: October 28, 2013, 05:58:57 PM by Patrice Terrier »
Patrice Terrier
GDImage (advanced graphic addon)
http://www.zapsolution.com

Online Patrice Terrier

  • Global Moderator
  • Hero Member
  • *****
  • Posts: 2009
    • www.zapsolution.com
After using C++ for several monthes
« Reply #84 on: December 05, 2013, 05:48:12 PM »
After using C++ for several monthes now, i can share this feedback with you...

Once the initial learning phase is over:
the first thing that really make a big difference is that you don't have to mess with includes and headers anymore,
second you can type in ALL the Windows API without restriction,
third you can use the latest technology and produce 32-bit and 64-bit from the same source code.

Switching to UNICODE is the thing that you must do as soon as possible,
then learn how to use VECTOR (dynamic array replacement, and much more),
and move to wstring (just as easy as BASIC strings).

Then try to keep as close as possible to the syntax indentation you are using in PB, to ease the reading of the source code.
Use Hungarian notation to easily detect the variable type.

Learn how to cast one variable type to another, because C++ is using strong typing.
ALWAYS assign your variables, your arrays, your type structures, before using them.

Be careful on the / (divide) optimization, always use (float) to avoid rounding errors.

Never try to compare strings with == (because C++ does a pointer comparison), instead use the string.compare() function.

After a while, you will realize that for a SDK programmer, C++ is not that harder to use than PB, as long as you stay away of the C++ encapsulation, that is just another kind of DDT. :)

...


Patrice Terrier
GDImage (advanced graphic addon)
http://www.zapsolution.com

Online Patrice Terrier

  • Global Moderator
  • Hero Member
  • *****
  • Posts: 2009
    • www.zapsolution.com
64-bit version of BASS.dll ?
« Reply #85 on: December 11, 2013, 10:36:25 AM »
Does anyone has used the 64-bit version of BASS.dll ?

...
Patrice Terrier
GDImage (advanced graphic addon)
http://www.zapsolution.com

Offline Brice Manuel

  • Full Member
  • ***
  • Posts: 161
Re: 64-bit version of BASS.dll ?
« Reply #86 on: December 12, 2013, 02:34:03 PM »
Does anyone has used the 64-bit version of BASS.dll ?
No, but I was planning to very soon.